Java郵件傳送帶附件

雲驛站發表於2013-05-19

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qingfeng812/article/details/8948281

 

package Test;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/*
需要的jar 包 activation.jar   mail.jar
*/
public class Send2{  
    // 郵箱伺服器  
    private String host = “smtp.qq.com”;// 或者是 smtp.163(sina).com  
    private String username = “784602719@qq.com“;   //郵箱名稱
    private String password = ” “;   //郵箱密碼
    private String mail_to = “784602719@qq.com“;   //傳送至 人郵箱
    private String mail_from = “784602719@qq.com“;   //傳送人郵箱username
    private String mail_subject = “送你的幸福!湘竹集美伊客!”;   //郵箱主題
    private String mail_body = “測試資訊”;   //郵箱內容
    private String personalName = “784602719@qq.com“; //傳送人郵箱username
    public Send2(){}  
    public void send() throws Exception{  
        try{  
           Properties props=new Properties();  
 Authenticator auth = new Email_Autherticator(); // 進行郵件伺服器使用者認證  
           props.put(“mail.smtp.host”, host);  
           props.put(“mail.smtp.auth”, “true”);  
           Session session = Session.getDefaultInstance(props, auth);  

          
           //設定session,和郵件伺服器進行通訊。  
           MimeMessage message = new MimeMessage(session);  
           message.setSubject(mail_subject); // 設定郵件主題  
          
           //新增附件資訊
           Multipart   mp   =   new   MimeMultipart(); 
           BodyPart mbp1 = new MimeBodyPart();
           FileDataSource fds = new FileDataSource(“d:\測試.doc”);
           mbp1.setDataHandler(new DataHandler(fds));
            //設定附件名稱
           mbp1.setFileName(MimeUtility.encodeText(“d:\測試.doc”, “utf-8”, null));
           mp.addBodyPart(mbp1);
         
          
          
           BodyPart mbp = new MimeBodyPart();
           mbp.setContent(mail_body, “text/html;charset=utf-8”);
           mp.addBodyPart(mbp);

           message.setContent(mp);
         
           
           message.setSentDate(new Date()); // 設定郵件傳送日期  
          
        
           Address address = new InternetAddress(mail_from, personalName);  
           message.setFrom(address); // 設定郵件傳送者的地址  
           Address toAddress = new InternetAddress(mail_to); // 設定郵件接收方的地址  
           message.addRecipient(Message.RecipientType.TO, toAddress);  
           Transport.send(message); // 傳送郵件  
           System.out.println(“郵件傳送成功!”);  
        }catch (Exception ex){  
            //ex.printStackTrace();  
            if(ex instanceof javax.mail.AuthenticationFailedException){
             System.out.println(“認證錯誤”);
            }
           throw new Exception(ex.getMessage());  
        }  
   }  
 
    /** 
     * 用來進行伺服器對使用者的認證 
     */ 
    public class Email_Autherticator extends Authenticator{  
        public Email_Autherticator(){  
            super();  
        }  
 
        public Email_Autherticator(String user, String pwd){  
           super();  
           username = user;  
           password = pwd;  
        }  
 
        public PasswordAuthentication getPasswordAuthentication(){  
            return new PasswordAuthentication(username, password);  
        }  
    }  
 
    public static void main(String[] args){  
     Send2 sendmail = new Send2();  
         try{  
             sendmail.send();  
        }catch (Exception ex){  
             ex.printStackTrace();  
        }  
   }  

/*注意: 當傳送人使用QQ 郵箱時,QQ 郵箱必須開啟PoP3/SMTP 協議
如何啟用PoP3/SMTP 協議
登陸QQ郵箱 https://mail.qq.com/
設定–>賬號–> 開啟PoP3/SMTP 協議*/


相關文章