java發送郵件 發送帶附件的郵件

b4c2 9年前發布 | 15K 次閱讀 Java

import java.io.File;
import java.util.Date;

import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; 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;

/**

  • 發送帶附件的郵件
  • 需要導入mail.jar */ public class AttachmentMailSender {

    public static boolean sendMail(MailSenderInfo mailInfo) {

     // 判斷是否需要身份認證
     MyAuthenticator authenticator = null;
     if (mailInfo.isValidate()) {
         // 如果需要身份認證,則創建一個密碼驗證器
         authenticator = new MyAuthenticator(mailInfo.getUserName(),
                 mailInfo.getPassword());
     }
     // 根據郵件發送的屬性和密碼驗證器構造一個發送郵件的session
     Session sendMailSession = Session.getInstance(mailInfo.getProperties(),
             authenticator);
     try {
         // 根據session創建一個郵件消息
         Message mailMessage = new MimeMessage(sendMailSession);
         // 創建郵件發送者地址
         Address from = new InternetAddress(mailInfo.getFromAddress());
         // 設置郵件消息的發送者
         mailMessage.setFrom(from);
         // 創建郵件的接收者地址,并設置到郵件消息中
         Address to = new InternetAddress(mailInfo.getToAddress());
         mailMessage.setRecipient(Message.RecipientType.TO, to);
         // 設置郵件消息的主題
         mailMessage.setSubject(mailInfo.getSubject());
         // 設置郵件消息發送的時間
         mailMessage.setSentDate(new Date());
    
         // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象
         Multipart mainPart = new MimeMultipart();
         // 創建一個包含HTML內容的MimeBodyPart
         BodyPart html = new MimeBodyPart();
         // 設置HTML內容
         html.setContent(mailInfo.getContent(), "text/html; charset=GBK");
         mainPart.addBodyPart(html);
         // 為郵件添加附件
         String[] attachFileNames = mailInfo.getAttachFileNames();
         if (attachFileNames != null && attachFileNames.length > 0) {
             // 存放郵件附件的MimeBodyPart
             MimeBodyPart attachment = null;
             File file = null;
             for (int i = 0; i < attachFileNames.length; i++) {
                 attachment = new MimeBodyPart();
                 // 根據附件文件創建文件數據源
                 file = new File(attachFileNames[i]);
                 FileDataSource fds = new FileDataSource(file);
                 attachment.setDataHandler(new DataHandler(fds));
                 // 為附件設置文件名
                 attachment.setFileName(MimeUtility.encodeWord(
                         file.getName(), "GBK", null));
                 mainPart.addBodyPart(attachment);
             }
         }
         // 將MiniMultipart對象設置為郵件內容
         mailMessage.setContent(mainPart);
         // 發送郵件
         Transport.send(mailMessage);
         return true;
    
     } catch (Exception e) {
         e.printStackTrace();
         return false;
     }
    

    }

    public static void main(String[] args) {

     // 創建郵件信息
     MailSenderInfo mailInfo = new MailSenderInfo();
     mailInfo.setMailServerHost("smtp.sina.com.cn");
     mailInfo.setMailServerPort("25");
     mailInfo.setValidate(true);
     mailInfo.setUserName("***");
     mailInfo.setPassword("***");
     mailInfo.setFromAddress("***@sina.com");
     mailInfo.setToAddress("***@163.com");
     mailInfo.setSubject("MyMail測試");
     mailInfo.setContent("我的郵件測試\n\rMy test mail\n\r");
    
     String[] fileNames = new String[3];
     fileNames[0] = "C:/temp/new.txt";
     fileNames[1] = "C:/temp/test.wav";
     fileNames[2] = "C:/temp/mary_photo.jpg";
     mailInfo.setAttachFileNames(fileNames);
    
     AttachmentMailSender.sendMail(mailInfo);
    

    } } </pre>

 本文由用戶 b4c2 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!