JavaMail(3)——發送帶附件的郵件

關鍵技術:


發送帶附件的郵件時必須使用MimeMultipart和MimeBodyPart。MimeBodyPart可以存儲HTML子內容,也可以存儲二進制數據,如文件內容。


MimeBodyPart的setDataHandler方法能夠把DataHandler對象保存到郵件消息中,而用文件的File對象可以構造文件數據源FileDataSource,由FileDataSource可以構造DataHandler對象,所以,可以把文件內容設置到郵件消息中,此時文件便成了郵件的附件。


MimeBodyPart的setFileName方法給附件設置文件。


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;

/**

 * 發送帶附件的郵件
*/
publicclass AttachmentMailSender {

publicstaticboolean 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);
returntrue;

        } catch (Exception e) {
            e.printStackTrace();
returnfalse;
        }
    }

publicstaticvoid 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);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章