Java代碼實現發送郵件

來源:互聯網
上載者:User

標籤:密碼驗證   tor   ati   temp   attach   static   nim   toad   instance   

package com.ust.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;

/**

  • 發送帶附件的郵件
    */
    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("mail.XXX.com")//mail.qq.com 這個自己看類型百度查詢下,我們公司地址不透露啦
    mailInfo.setMailServerPort("25");
    mailInfo.setValidate(true);
    mailInfo.setUserName("使用者名稱");
    mailInfo.setPassword("密碼");
    mailInfo.setFromAddress("你的郵箱");
    mailInfo.setToAddress("收件郵箱");
    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);

    }
    }

Java代碼實現發送郵件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.