1.在編程中,我們可以使用JavaMail來進行郵件傳輸。
我們可以在自己的電腦上安裝安裝郵件服務器apache-james-2.3.2,或者直接使用門戶網站的郵件服務器來進行郵件傳輸。
本篇講述使用門戶網站的郵件服務器,用java程序來進行郵件傳輸。
2.郵件傳輸的協議有SMTP(簡單郵件傳輸協議)和POP3(郵局協議)。
並不是所有的提供郵箱的網站都會對用戶開通SMTP服務器。可以使用的SMTP服務器都需要身份驗證的,當然是不可以匿名發郵件的啦。下面給出了騰訊和網易公布的SMTP和POP3服務器地址。
2.1QQ郵箱服務器
2.2網易郵箱服務器
3.編程使用SMTP發送郵件(不帶附件的)
3.1 MailSenderInfo.java給出要發送郵件的基本信息(類似JavaBean文件),該文件不需要改動

1 package src; 2 /** 3 * 發送郵件需要使用的基本信息 4 */ 5 import java.util.Properties; 6 public class MailSenderInfo { 7 // 發送郵件的服務器的IP和端口 8 private String mailServerHost; 9 private String mailServerPort = "25"; 10 // 郵件發送者的地址 11 private String fromAddress; 12 // 郵件接收者的地址 13 private String toAddress; 14 // 登陸郵件發送服務器的用戶名和密碼 15 private String userName; 16 private String password; 17 // 是否需要身份驗證 18 private boolean validate = false; 19 // 郵件主題 20 private String subject; 21 // 郵件的文本內容 22 private String content; 23 // 郵件附件的文件名 24 private String[] attachFileNames; 25 /** 26 * 獲得郵件會話屬性 27 */ 28 public Properties getProperties(){ 29 Properties p = new Properties(); 30 p.put("mail.smtp.host", this.mailServerHost); 31 p.put("mail.smtp.port", this.mailServerPort); 32 p.put("mail.smtp.auth", validate ? "true" : "false"); 33 return p; 34 } 35 public String getMailServerHost() { 36 return mailServerHost; 37 } 38 public void setMailServerHost(String mailServerHost) { 39 this.mailServerHost = mailServerHost; 40 } 41 public String getMailServerPort() { 42 return mailServerPort; 43 } 44 public void setMailServerPort(String mailServerPort) { 45 this.mailServerPort = mailServerPort; 46 } 47 public boolean isValidate() { 48 return validate; 49 } 50 public void setValidate(boolean validate) { 51 this.validate = validate; 52 } 53 public String[] getAttachFileNames() { 54 return attachFileNames; 55 } 56 public void setAttachFileNames(String[] fileNames) { 57 this.attachFileNames = fileNames; 58 } 59 public String getFromAddress() { 60 return fromAddress; 61 } 62 public void setFromAddress(String fromAddress) { 63 this.fromAddress = fromAddress; 64 } 65 public String getPassword() { 66 return password; 67 } 68 public void setPassword(String password) { 69 this.password = password; 70 } 71 public String getToAddress() { 72 return toAddress; 73 } 74 public void setToAddress(String toAddress) { 75 this.toAddress = toAddress; 76 } 77 public String getUserName() { 78 return userName; 79 } 80 public void setUserName(String userName) { 81 this.userName = userName; 82 } 83 public String getSubject() { 84 return subject; 85 } 86 public void setSubject(String subject) { 87 this.subject = subject; 88 } 89 public String getContent() { 90 return content; 91 } 92 public void setContent(String textContent) { 93 this.content = textContent; 94 } 95 }
3.2 SimpleMailSender.java,設置郵件的通常數據。不需要改動

1 package src; 2 3 import java.util.Date; 4 import java.util.Properties; 5 import javax.mail.Address; 6 import javax.mail.BodyPart; 7 import javax.mail.Message; 8 import javax.mail.MessagingException; 9 import javax.mail.Multipart; 10 import javax.mail.Session; 11 import javax.mail.Transport; 12 import javax.mail.internet.InternetAddress; 13 import javax.mail.internet.MimeBodyPart; 14 import javax.mail.internet.MimeMessage; 15 import javax.mail.internet.MimeMultipart; 16 17 /** 18 * 簡單郵件(不帶附件的郵件)發送器 19 */ 20 public class SimpleMailSender { 21 /** 22 * 以文本格式發送郵件 23 * @param mailInfo 待發送的郵件的信息 24 */ 25 public boolean sendTextMail(MailSenderInfo mailInfo) { 26 // 判斷是否需要身份認證 27 MyAuthenticator authenticator = null; 28 Properties pro = mailInfo.getProperties(); 29 if (mailInfo.isValidate()) { 30 // 如果需要身份認證,則創建一個密碼驗證器 31 authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 32 } 33 // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session 34 Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 35 try { 36 // 根據session創建一個郵件消息 37 Message mailMessage = new MimeMessage(sendMailSession); 38 // 創建郵件發送者地址 39 Address from = new InternetAddress(mailInfo.getFromAddress()); 40 // 設置郵件消息的發送者 41 mailMessage.setFrom(from); 42 // 創建郵件的接收者地址,並設置到郵件消息中 43 Address to = new InternetAddress(mailInfo.getToAddress()); 44 mailMessage.setRecipient(Message.RecipientType.TO,to); 45 // 設置郵件消息的主題 46 mailMessage.setSubject(mailInfo.getSubject()); 47 // 設置郵件消息發送的時間 48 mailMessage.setSentDate(new Date()); 49 // 設置郵件消息的主要內容 50 String mailContent = mailInfo.getContent(); 51 mailMessage.setText(mailContent); 52 // 發送郵件 53 Transport.send(mailMessage); 54 return true; 55 } catch (MessagingException ex) { 56 ex.printStackTrace(); 57 } 58 return false; 59 } 60 61 /** 62 * 以HTML格式發送郵件 63 * @param mailInfo 待發送的郵件信息 64 */ 65 public static boolean sendHtmlMail(MailSenderInfo mailInfo){ 66 // 判斷是否需要身份認證 67 MyAuthenticator authenticator = null; 68 Properties pro = mailInfo.getProperties(); 69 //如果需要身份認證,則創建一個密碼驗證器 70 if (mailInfo.isValidate()) { 71 authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 72 } 73 // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session 74 Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 75 try { 76 // 根據session創建一個郵件消息 77 Message mailMessage = new MimeMessage(sendMailSession); 78 // 創建郵件發送者地址 79 Address from = new InternetAddress(mailInfo.getFromAddress()); 80 // 設置郵件消息的發送者 81 mailMessage.setFrom(from); 82 // 創建郵件的接收者地址,並設置到郵件消息中 83 Address to = new InternetAddress(mailInfo.getToAddress()); 84 // Message.RecipientType.TO屬性表示接收者的類型為TO 85 mailMessage.setRecipient(Message.RecipientType.TO,to); 86 // 設置郵件消息的主題 87 mailMessage.setSubject(mailInfo.getSubject()); 88 // 設置郵件消息發送的時間 89 mailMessage.setSentDate(new Date()); 90 // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象 91 Multipart mainPart = new MimeMultipart(); 92 // 創建一個包含HTML內容的MimeBodyPart 93 BodyPart html = new MimeBodyPart(); 94 // 設置HTML內容 95 html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); 96 mainPart.addBodyPart(html); 97 // 將MiniMultipart對象設置為郵件內容 98 mailMessage.setContent(mainPart); 99 // 發送郵件 100 Transport.send(mailMessage); 101 return true; 102 } catch (MessagingException ex) { 103 ex.printStackTrace(); 104 } 105 return false; 106 } 107 }
3.3 MyAuthenticator.java,需要根據自己的郵箱地址,發送的內容,發送地址,使用的服務器的SMTP網址改動

1 package src; 2 3 import javax.mail.*; 4 5 public class MyAuthenticator extends Authenticator{ 6 String userName=null; 7 String password=null; 8 9 public MyAuthenticator(){ 10 } 11 public MyAuthenticator(String username, String password) { 12 this.userName = username; 13 this.password = password; 14 } 15 protected PasswordAuthentication getPasswordAuthentication(){ 16 return new PasswordAuthentication(userName, password); 17 } 18 19 public static void main(String[] args){ 20 //這個類主要是設置郵件 21 MailSenderInfo mailInfo = new MailSenderInfo(); 22 mailInfo.setMailServerHost("smtp.qq.com"); 23 mailInfo.setMailServerPort("25"); 24 mailInfo.setValidate(true); 25 mailInfo.setUserName("1111111@qq.com"); //自己的郵箱 26 mailInfo.setPassword("QQ88888");//自己的郵箱密碼,用於驗證 27 28 mailInfo.setFromAddress("1111111@qq.com"); ///自己的郵箱 29 mailInfo.setToAddress("66666666@qq.com"); ///對方的郵箱 30 mailInfo.setSubject("設置郵箱標題"); 31 mailInfo.setContent("設置郵箱內容"); 32 33 //這個類主要來發送郵件 34 SimpleMailSender sms = new SimpleMailSender(); 35 sms.sendTextMail(mailInfo);//發送文體格式 36 sms.sendHtmlMail(mailInfo);//發送html格式 37 38 } 39 } 40
4.使用注意情況
4.1 發送方的郵箱(自己的)必須與程序中使用的服務器的SMTP是一致的。郵件接收方的郵件可以任意。
4.2 因為這只是最簡單的郵件傳輸,不包附件,只有正文部分,並且只是負責郵件的發送,因此只需要SMTP(簡單郵件傳輸協議)。當要讀取郵件的時候就必須要使用POP3(郵局協議)。
4.3 程序發郵件可以減少大量的體力勞動。我們也要合理使用,不能搗亂,給其他人大量的發送重復郵件。例如,當用程序給一個郵箱發大量郵件時,可能會被相應的服務器認為是外界攻擊。
我給自己的QQ郵箱,用上面的程序做了一個40次的發送循環。在我自己去QQ郵箱手動刪除那些郵件的時候,QQ郵箱可能懷疑這是通過惡意程序發來的。下面是QQ郵箱的提示:
4.4 通過程序自己的郵件給多個郵箱發信息,應該能行吧,自己也沒有嘗試,沒有那么多郵箱了。有興趣的大家可以試試,呵呵。
參考:
http://www.iteye.com/topic/352753