Java實現郵箱發送

[java] view plain copy
  1. package com.forenms.ycczj.email;  
  2.   
  3. import javax.mail.Authenticator;  
  4. import javax.mail.PasswordAuthentication;  
  5.   
  6. /** 
  7.  * 進行郵箱認證 
  8.  *  
  9.  * 
  10.  */  
  11. public class EmailAutherticator extends Authenticator{  
  12.       
  13.     private String userName;  
  14.       
  15.     private String passWord;  
  16.       
  17.     public EmailAutherticator(String userName, String passWord) {  
  18.         super();  
  19.         this.setUserName(userName);  
  20.         this.setPassWord(passWord);  
  21.   
  22.     }  
  23.   
  24.     public PasswordAuthentication getPasswordAuthentication() {  
  25.         return new PasswordAuthentication(userName, passWord);  
  26.     }  
  27.   
  28.     public String getUserName() {  
  29.         return userName;  
  30.     }  
  31.   
  32.     public void setUserName(String userName) {  
  33.         this.userName = userName;  
  34.     }  
  35.   
  36.     public String getPassWord() {  
  37.         return passWord;  
  38.     }  
  39.   
  40.     public void setPassWord(String passWord) {  
  41.         this.passWord = passWord;  
  42.     }  
  43.   
  44. }  
[java] view plain copy
  1. package com.forenms.ycczj.email;  
  2.   
  3. import java.util.Date;  
  4. import java.util.Properties;  
  5. import java.util.ResourceBundle;  
  6.   
  7. import javax.activation.DataHandler;  
  8. import javax.activation.FileDataSource;  
  9. import javax.mail.Authenticator;  
  10. import javax.mail.Multipart;  
  11. import javax.mail.Session;  
  12. import javax.mail.Transport;  
  13. import javax.mail.internet.InternetAddress;  
  14. import javax.mail.internet.MimeBodyPart;  
  15. import javax.mail.internet.MimeMessage;  
  16. import javax.mail.internet.MimeMultipart;  
  17. import javax.mail.internet.MimeUtility;  
  18.   
  19.   
  20. public class SendMail {  
  21.     private static ResourceBundle bunder=ResourceBundle.getBundle("email");  
  22.     public static void main(String args[])throws Exception{  
  23.         String smsMob[]=new String[]{"[email protected]"};  
  24.         System.out.println(SendMail.sendMail("title 郵件的標題", smsMob, "content 郵件的內容"));  
  25.           
  26.     }  
  27.       
  28.     /** 
  29.      * 發送郵件 
  30.      * @param subject  標題 
  31.      * @param to  接收方 
  32.      * @param text  內容 
  33.      * @param mimeType  類型("text/html;charset=gb2312") 
  34.      * @throws Exception 
  35.      */  
  36.     public static boolean sendMail(String subject, String[] to,String text) {  
  37.         boolean result=false;  
  38.         String smtp = bunder.getString("mail.smtp.host");   
  39.         String serverName = bunder.getString("mail.qq");  
  40.         String serverPaswd = bunder.getString("mail.from.password");  
  41.         String userMail=bunder.getString("mail.from");  
  42.   
  43.         //設置連接郵件服務器屬性  
  44.         Properties properties = System.getProperties();   
  45.         properties.put("mail.smtp.host", smtp);   
  46.         properties.put("mail.smtp.auth""true");  
  47.   
  48.         //進行郵件服務器認證  
  49.         EmailAutherticator emailAuther = new EmailAutherticator(serverName, serverPaswd);  
  50.         //獲取連接郵件服務器會話  
  51.         Session session = Session.getInstance(properties,(Authenticator)emailAuther);  
  52.         //設置郵件正文  
  53.         MimeMessage mimeMessage = new MimeMessage(session);  
  54.         Multipart multipart = new MimeMultipart();    
  55.         try {  
  56.             //設置發送郵件人地址  
  57.             InternetAddress sentFrom = new InternetAddress(userMail);  
  58.             mimeMessage.setFrom(sentFrom);        
  59.             //設置接收郵件人地址  
  60.             InternetAddress[] sendTo = new InternetAddress[to.length];  
  61.             for (int i = 0; i < to.length; i++) {  
  62.                 sendTo[i] = new InternetAddress(to[i]);  
  63.             }  
  64.             //mimeMessage.setRecipients(MimeMessage.RecipientType.TO,   sendTo);  
  65.               
  66.             mimeMessage.setRecipients(MimeMessage.RecipientType.BCC,sendTo);                  
  67.             //設置郵件標題  
  68.             mimeMessage.setSubject(subject, "UTF-8");         
  69.             //加入郵件正文信息  
  70.             MimeBodyPart mimeBodyPartHtml = new MimeBodyPart();       
  71.             //加入html  
  72.             mimeBodyPartHtml.setContent(text, "text/html;charset=gb2312");        
  73.             multipart.addBodyPart(mimeBodyPartHtml);      
  74.             //加入郵件正文  
  75.             mimeMessage.setContent(multipart);  
  76.             //加入發送時間  
  77.             mimeMessage.setSentDate(new Date());          
  78.             //保存信息  
  79.             mimeMessage.saveChanges();  
  80.             //發送郵件  
  81.             Transport.send(mimeMessage);  
  82.             result=true;  
  83.         } catch (Exception e) {           
  84.             //e.printStackTrace();  
  85.             return false;  
  86.         }  
  87.         return result;  
  88.     }  
  89.   
  90.     /** 
  91.      * 發送郵件(加入附件) 
  92.      * @param subject  標題 
  93.      * @param to  接收方 
  94.      * @param text  內容 
  95.      * @param fileName  發送文件 
  96.      * @param mimeType  類型("text/html;charset=gb2312") 
  97.      * @throws Exception 
  98.      */  
  99.     public static void sendMailToFile(String subject, String[] to,String text, String fileName) throws Exception {  
  100.   
  101.         String smtp = "smtp.163.com";   
  102.         String serverName = "xxxxxx";  
  103.         String serverPaswd = "xxxxxxxxx";  
  104.         String userMail="[email protected]";  
  105.   
  106.         //設置連接郵件服務器屬性  
  107.         Properties properties = System.getProperties();   
  108.         properties.put("mail.smtp.host", smtp);   
  109.         properties.put("mail.smtp.auth""true");  
  110.   
  111.         //進行郵件服務器認證  
  112.         EmailAutherticator emailAuther = new EmailAutherticator(serverName, serverPaswd);  
  113.         //獲取連接郵件服務器會話  
  114.         Session session = Session.getInstance(properties,(Authenticator)emailAuther);  
  115.         //設置郵件正文  
  116.         MimeMessage mimeMessage = new MimeMessage(session);  
  117.         Multipart multipart = new MimeMultipart();        
  118.         //設置發送郵件人地址  
  119.         InternetAddress sentFrom = new InternetAddress(userMail);  
  120.         mimeMessage.setFrom(sentFrom);        
  121.         //設置接收郵件人地址  
  122.         InternetAddress[] sendTo = new InternetAddress[to.length];  
  123.         for (int i = 0; i < to.length; i++) {  
  124.             sendTo[i] = new InternetAddress(to[i]);  
  125.         }  
  126.         //mimeMessage.setRecipients(MimeMessage.RecipientType.TO,   sendTo);  
  127.           
  128.         mimeMessage.setRecipients(MimeMessage.RecipientType.BCC,sendTo);          
  129.           
  130.           
  131.         //設置郵件標題  
  132.         mimeMessage.setSubject(subject, "UTF-8");         
  133.         //加入郵件正文信息  
  134.         MimeBodyPart mimeBodyPartHtml = new MimeBodyPart();       
  135.         //加入html  
  136.         mimeBodyPartHtml.setContent(text, "text/html;charset=gb2312");        
  137.         multipart.addBodyPart(mimeBodyPartHtml);  
  138.         //加入附件    
  139.         MimeBodyPart mimeBodyPartFile = new MimeBodyPart();  
  140.         //附件文件路徑  
  141.         String filename = fileName.split(",")[0];  
  142.         //附件文件名稱  
  143.         String displayname = fileName.split(",")[1];  
  144.         //獲取文件流  
  145.         FileDataSource fds = new FileDataSource(filename);  
  146.         mimeBodyPartFile.setDataHandler(new DataHandler(fds));  
  147.         mimeBodyPartFile.setFileName(MimeUtility.encodeText(displayname));  
  148.         multipart.addBodyPart(mimeBodyPartFile);          
  149.         //加入郵件正文  
  150.         mimeMessage.setContent(multipart);  
  151.         //加入發送時間  
  152.         mimeMessage.setSentDate(new Date());          
  153.         //保存信息  
  154.         mimeMessage.saveChanges();  
  155.   
  156.         //發送郵件  
  157.         Transport.send(mimeMessage);  
  158.     }  
  159.       
  160. }  

email.properties

[plain] view plain copy
  1. mail.smtp.host=smtp.qq.com  
  2. mail.smtp.auth=true  
  3. [email protected]  
  4. mail.qq=xxxxxx  
  5. mail.from.password=******  
  6. mail.smtp.timeout=25000  
  7. mail.smtp.starttls.enable=true  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章