java郵件發送的簡單實現,使用javamail通過smtp協議發信

1.通過javamail實現
[java] view plain copy
  1. import javax.mail.*;  
  2. import javax.mail.internet.InternetAddress;  
  3. import javax.mail.internet.MimeMessage;  
  4. import java.util.Properties;  
  5.   
  6. public class SimpleAliDMSendMail {  
  7.     private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";  
  8.     private static final int ALIDM_SMTP_PORT = 25;  
  9.   
  10.     public static void main(String[] args) throws MessagingException {  
  11.         // 配置發送郵件的環境屬性  
  12.         final Properties props = new Properties();  
  13.         // 表示SMTP發送郵件,需要進行身份驗證  
  14.         props.put("mail.smtp.auth""true");  
  15.         props.put("mail.smtp.host", ALIDM_SMTP_HOST);  
  16.         props.put("mail.smtp.port", ALIDM_SMTP_PORT);     
  17.         // 如果使用ssl,則去掉使用25端口的配置,進行如下配置,   
  18.         // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
  19.         // props.put("mail.smtp.socketFactory.port", "465");  
  20.         // props.put("mail.smtp.port", "465");  
  21.   
  22.   
  23.         // 發件人的賬號  
  24.         props.put("mail.user""***");  
  25.         // 訪問SMTP服務時需要提供的密碼  
  26.         props.put("mail.password""***");  
  27.   
  28.         // 構建授權信息,用於進行SMTP進行身份驗證  
  29.         Authenticator authenticator = new Authenticator() {  
  30.             @Override  
  31.             protected PasswordAuthentication getPasswordAuthentication() {  
  32.                 // 用戶名、密碼  
  33.                 String userName = props.getProperty("mail.user");  
  34.                 String password = props.getProperty("mail.password");  
  35.                 return new PasswordAuthentication(userName, password);  
  36.             }  
  37.         };  
  38.         // 使用環境屬性和授權信息,創建郵件會話  
  39.         Session mailSession = Session.getInstance(props, authenticator);  
  40.         // 創建郵件消息  
  41.         MimeMessage message = new MimeMessage(mailSession);  
  42.         // 設置發件人  
  43.         InternetAddress form = new InternetAddress(  
  44.                 props.getProperty("mail.user"));  
  45.         message.setFrom(form);  
  46.   
  47.         // 設置收件人  
  48.         InternetAddress to = new InternetAddress("***");  
  49.         message.setRecipient(MimeMessage.RecipientType.TO, to);  
  50.   
  51.         // 設置郵件標題  
  52.         message.setSubject("測試郵件");  
  53.         // 設置郵件的內容體  
  54.         message.setContent("測試的HTML郵件""text/html;charset=UTF-8");  
  55.   
  56.         // 發送郵件  
  57.         Transport.send(message);  
  58.     }  
  59. }  


2.使用Apache Commons-email組件發送郵件
commons-email是apache提供的一個開源的API,是對javamail的封裝,
因此在使用時要將javamail.jar加到 class path中,
主要包括SimpleEmail,MultiPartEmail,HtmlEmail,EmailAttachment四個類。
 
SimpleEmail:發送簡單的email,不能添加附件
MultiPartEmail:文本郵件,可以添加多個附件
HtmlEmail:HTML格式郵件,同時具有MultiPartEmail類所有“功能”
EmailAttchment:附件類,可以添加本地資源,也可以指定網絡上資源,在發送時自動將網絡上資源下載發送。

 
發送基本文本格式郵件:
[java] view plain copy
  1. SimpleEmail email = new SimpleEmail();  
  2. //smtp host   
  3. email.setHostName("mail.test.com");  
  4. //登陸郵件服務器的用戶名和密碼  
  5. email.setAuthentication("test","testpassword");  
  6. //接收人  
  7. email.addTo("[email protected]""John Doe");  
  8. //發送人  
  9. email.setFrom("[email protected]""Me");  
  10. //標題  
  11. email.setSubject("Test message");  
  12. //郵件內容  
  13. email.setMsg("This is a simple test of commons-email");  
  14. //發送  
  15. email.send();  



發送文本格式,帶附件郵件:
[java] view plain copy
  1. //附件,可以定義多個附件對象  
  2. EmailAttachment attachment = new EmailAttachment();  
  3. attachment.setPath("e:\\1.pdf");  
  4. attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  5. attachment.setDescription("Picture of John");  
  6. //  
  7. MultiPartEmail email = new MultiPartEmail();  
  8. //smtp host   
  9. email.setHostName("mail.test.com");  
  10. //登陸郵件服務器的用戶名和密碼  
  11. email.setAuthentication("test","testpassword");  
  12. //接收人  
  13. email.addTo("[email protected]""John Doe");  
  14. //發送人  
  15. email.setFrom("[email protected]""Me");  
  16. //標題  
  17. email.setSubject("Test message");  
  18. //郵件內容  
  19. email.setMsg("This is a simple test of commons-email");  
  20. //添加附件  
  21. email.attach(attachment);  
  22. //發送  
  23. email.send();  


 
發送HTML格式帶附件郵件:
[java] view plain copy
  1. //附件,可以定義多個附件對象  
  2. EmailAttachment attachment = new EmailAttachment();  
  3. attachment.setPath("e:\\1.pdf");  
  4. attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  5. attachment.setDescription("Picture of John");  
  6. //  
  7. HtmlEmail email = new HtmlEmail ();  
  8. //smtp host   
  9. email.setHostName("mail.test.com");  
  10. //登陸郵件服務器的用戶名和密碼  
  11. email.setAuthentication("test","testpassword");  
  12. //接收人  
  13. email.addTo("[email protected]""John Doe");  
  14. //發送人  
  15. email.setFrom("[email protected]""Me");  
  16. //標題  
  17. email.setSubject("Test message");  
  18. //郵件內容  
  19. email.setHtmlMsg("This is a simple test of commons-email");  
  20. //添加附件  
  21. email.attach(attachment);  
  22. //發送  
  23. email.send();  



下面提供一個完整的程序示例:
  1. package test  
  2.   
  3. import org.apache.commons.mail.*;  
  4.   
  5. public class SendEMail  
  6. {  
  7.     public static void main ( String[] arg ) throws Exception  
  8.     {  
  9.         SimpleEmail email = new SimpleEmail ( );  
  10.   
  11.         // smtp host  
  12.         email.setHostName ( "smtp.163.com" );  
  13.         // 登陸郵件服務器的用戶名和密碼  
  14.         email.setAuthentication ( "test""123456" );  
  15.         // 接收人  
  16.         email.addTo ( "[email protected]""test" );  
  17.         // 發送人  
  18.         email.setFrom ( "[email protected]""測試者" );  
  19.         // 標題  
  20.         email.setSubject ( "Test message" );  
  21.         // 郵件內容  
  22.         email.setMsg ( "This is a simple test of commons-email 測試......" );  
  23.         // 發送  
  24.         email.send ( );  
  25.           
  26.         System.out.println ( "發送成功 Send email successful!" );  
  27.   
  28.     }  
  29. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章