Cyh的博客

          Email:kissyan4916@163.com
          posts - 26, comments - 19, trackbacks - 0, articles - 220

          JavaMail(2)--發(fā)送帶附件的郵件

          Posted on 2009-05-31 00:40 啥都寫點(diǎn) 閱讀(963) 評論(0)  編輯  收藏 所屬分類: J2SE
          關(guān)鍵技術(shù):
          • 發(fā)送帶附件的郵件時(shí)必須使用MimeMultipart和MimeBodyPart。MimeBodyPart可以存儲HTML子內(nèi)容,也可以存儲二進(jìn)制數(shù)據(jù),如文件內(nèi)容。
          • MimeBodyPart的setDataHandler方法能夠把DataHandler對象保存到郵件消息中,而用文件的File對象可以構(gòu)造文件數(shù)據(jù)源FileDataSource,由FileDataSource可以構(gòu)造DataHandler對象,所以,可以把文件內(nèi)容設(shè)置到郵件消息中,此時(shí)文件便成了郵件的附件。
          • MimeBodyPart的setFileName方法給附件設(shè)置文件。

          package book.email;

          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;

          /**
           * 發(fā)送帶附件的郵件
           
          */
          public class AttachmentMailSender {

              
          public static boolean sendMail(MailSenderInfo mailInfo) {
                  
          // 判斷是否需要身份認(rèn)證
                  MyAuthenticator authenticator = null;
                  
          if (mailInfo.isValidate()) {
                      
          // 如果需要身份認(rèn)證,則創(chuàng)建一個(gè)密碼驗(yàn)證器
                      authenticator = new MyAuthenticator(mailInfo.getUserName(),
                              mailInfo.getPassword());
                  }
                  
          // 根據(jù)郵件發(fā)送的屬性和密碼驗(yàn)證器構(gòu)造一個(gè)發(fā)送郵件的session
                  Session sendMailSession = Session.getInstance(mailInfo
                          .getProperties(), authenticator);
                  
          try {
                      
          // 根據(jù)session創(chuàng)建一個(gè)郵件消息
                      Message mailMessage = new MimeMessage(sendMailSession);
                      
          // 創(chuàng)建郵件發(fā)送者地址
                      Address from = new InternetAddress(mailInfo.getFromAddress());
                      
          // 設(shè)置郵件消息的發(fā)送者
                      mailMessage.setFrom(from);
                      
          // 創(chuàng)建郵件的接收者地址,并設(shè)置到郵件消息中
                      Address to = new InternetAddress(mailInfo.getToAddress());
                      mailMessage.setRecipient(Message.RecipientType.TO,to);
                      
          // 設(shè)置郵件消息的主題
                      mailMessage.setSubject(mailInfo.getSubject());
                      
          // 設(shè)置郵件消息發(fā)送的時(shí)間
                      mailMessage.setSentDate(new Date());
                      
                      
          // MiniMultipart類是一個(gè)容器類,包含MimeBodyPart類型的對象
                      Multipart mainPart = new MimeMultipart();
                      
          // 創(chuàng)建一個(gè)包含HTML內(nèi)容的MimeBodyPart
                      BodyPart html = new MimeBodyPart();
                      
          // 設(shè)置HTML內(nèi)容
                      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();
                              
          // 根據(jù)附件文件創(chuàng)建文件數(shù)據(jù)源
                              file = new File(attachFileNames[i]);
                              FileDataSource fds 
          = new FileDataSource(file);
                              attachment.setDataHandler(
          new DataHandler(fds));
                              
          // 為附件設(shè)置文件名
                              attachment.setFileName(MimeUtility.encodeWord(file.getName(), "GBK",
                                      
          null));
                              mainPart.addBodyPart(attachment);
                          }
                      }
                      
          // 將MiniMultipart對象設(shè)置為郵件內(nèi)容
                      mailMessage.setContent(mainPart);
                      
          // 發(fā)送郵件
                      Transport.send(mailMessage);
                      
          return true;
                      
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                      
          return false;
                  }
              }
              
              
          public static void main(String[] args) {
                  
          // 創(chuàng)建郵件信息
                  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);
              }
          }



                                                                                                                 --    學(xué)海無涯
                  

          主站蜘蛛池模板: 肃宁县| 缙云县| 德州市| 云南省| 开平市| 桃江县| 灵石县| 扶沟县| 股票| 磐安县| 青海省| 陆川县| 鄯善县| 澜沧| 安乡县| 惠水县| 冀州市| 承德县| 启东市| 阿巴嘎旗| 南靖县| 衢州市| 张家川| 思茅市| 巨野县| 扶沟县| 化德县| 灵寿县| 葵青区| 德江县| 黄龙县| 濉溪县| 江华| 衢州市| 福贡县| 白山市| 阳西县| 田林县| 满城县| 连城县| 花垣县|