收发邮件

阅读更多
1, 发邮件能用126来发送,并带附件
2, 收邮件只能看到标题, 而且无法判断是新是旧
import java.io.File;
import java.util.Properties;  
  
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;  
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;  
import javax.mail.Store;
import javax.mail.Transport;  
import javax.mail.internet.InternetAddress;  
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;  
import javax.mail.internet.MimeMultipart;
  
public class TestJavaMail {  
  
    private static final String MAIL_USER = "[email protected]";   //邮件服务器登录用户名  
    private static final String MAIL_PASSWORD = "password654321";   //邮件服务器登录密码  
    private static final String MAIL_SERVER =  "smtp.126.com"; //邮件服务器
    private static final String FILE_PATH ="c:/test.xls"; //附件路径
    
    private static final String MAIL_TO = "[email protected]"; //发送给对方

    private static final String USER_NAME = "abcde15";
    private static final String MAIL_RECEIVER = "pop3.126.com";
    
    public static void sendMail(String mail,String subject,String text) {  
  
        try {  
            Properties props = new Properties();  
            props.put("mail.smtp.host", MAIL_SERVER);  
            props.put("mail.smtp.auth", "true");  
            Session ssn = Session.getInstance(props, null);  
            MimeMessage message = new MimeMessage(ssn);  
            InternetAddress fromAddress = new InternetAddress(MAIL_USER);  
            message.setFrom(fromAddress);  
            InternetAddress toAddress = new InternetAddress(mail);  
            message.addRecipient(Message.RecipientType.TO, toAddress);  
            message.setSubject(subject);  
            message.setText(text);  
            
            /**
             * 增加附件功能
             */
            Multipart mp = new MimeMultipart(); 
            MimeBodyPart mbp=new MimeBodyPart(); 
            FileDataSource fds=new FileDataSource(FILE_PATH); 
            mbp.setDataHandler(new DataHandler(fds)); 
            mbp.setFileName(fds.getName()); 
            mp.addBodyPart(mbp); 
            message.setContent(mp); 
            
            Transport transport = ssn.getTransport("smtp");  
            transport.connect(MAIL_SERVER, MAIL_USER, MAIL_PASSWORD);  
            transport.sendMessage(message, message  
                    .getRecipients(Message.RecipientType.TO));  
            transport.close();  
            System.out.println("你的邮件已发出");  
        } catch (Exception m) {  
            //System.out.println(m.toString());
        	m.printStackTrace();
        }  
    }  
      
    
    public static void receiveMail() {

		try {
			// 1.创建会话
			Properties properties = new Properties();
			properties.put("mail.pop3.host", MAIL_RECEIVER);
			properties.put("mail.pop3.auth", String.valueOf(true));
			Session session = Session.getDefaultInstance(properties,
					new Authenticator() {
						@Override
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(USER_NAME,
									MAIL_PASSWORD);
						}
					});

			// 2.连接pop3服务器
			Store store = session.getStore("pop3");
			store.connect();
			Folder folder = store.getFolder("INBOX");
			folder.open(Folder.READ_ONLY);
			// 3.收取邮件
			Message[] mail = folder.getMessages();
			// 所有邮件个数
			int totalNum = folder.getMessageCount();
			System.out.println("邮件数量:"+totalNum);
			
			for(int i = 0 ; i 
 

你可能感兴趣的:(收发邮件)