javaMail

javaMail發送郵件

jar包:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.4</version>
</dependency>

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

工具類

package com.ufclub.util.mail;

import com.ufclub.DTO.mail.FoxmailPropertiesDTO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 發送郵件
 *
 * @author zhoushixia
 * @date 2017-08-10
 */
public class SendMailUtil {

    private static final Logger logger = LoggerFactory.getLogger(SendMailUtil.class);

    private static  boolean isInit = false;

    private static  Properties prop;

    public static  void setProperty(FoxmailPropertiesDTO foxmailPropertiesDTO){
        if(!isInit){
            logger.info("{0}郵箱初始化開始,發送者爲{1},接受者爲{2}",foxmailPropertiesDTO.getMailCode(),foxmailPropertiesDTO.getSenderAddress(),foxmailPropertiesDTO.getReceiveAddress());

            prop = new Properties();
            prop.setProperty("mail.host",foxmailPropertiesDTO.getMailHost());
            prop.setProperty("mail.transport.protocol", "smtp");
            prop.setProperty("mail.smtp.auth", "true");
            isInit = true;

            logger.info("{0}郵箱初始化成功,發送者爲{1},接受者爲{2}",foxmailPropertiesDTO.getMailCode(),foxmailPropertiesDTO.getSenderAddress(),foxmailPropertiesDTO.getReceiveAddress());

        }
    }

    public static void sendMail(FoxmailPropertiesDTO foxmailPropertiesDTO){
        if(StringUtils.isBlank(foxmailPropertiesDTO.getReceiveAddress())){
            logger.info("{0}發送郵件,郵件無接收者",foxmailPropertiesDTO.getMailCode());
            return;
        }
        try{
            setProperty(foxmailPropertiesDTO);
            //創建session
            Session session = Session.getInstance(prop);
            //開啓Session的debug模式,這樣就可以查看到程序發送Email的運行狀態
            session.setDebug(true);
            //通過session得到transport對象
            Transport ts = session.getTransport();
            //使用郵箱的用戶名和密碼連上郵件服務器,發送郵件時,發件人需要提交郵箱的用戶名和密碼給smtp服務器,用戶名和密碼都通過驗證之後才能夠正常發送郵件給收件人。
            ts.connect(foxmailPropertiesDTO.getMailHost(), foxmailPropertiesDTO.getSenderAddress(), foxmailPropertiesDTO.getSenderPassword());
            //創建郵件
            Message message = createSimpleMail(session,foxmailPropertiesDTO);
            //發送郵件
            ts.sendMessage(message, message.getAllRecipients());
            ts.close();
        }catch (Exception e) {
            logger.error("發送郵件異常");
            e.printStackTrace();
        }

    }

    public static MimeMessage createSimpleMail(Session session,FoxmailPropertiesDTO foxmailPropertiesDTO)
            throws Exception {
        //創建郵件對象
        MimeMessage message = new MimeMessage(session);
        //指明郵件的發件人
        message.setFrom(new InternetAddress(foxmailPropertiesDTO.getSenderAddress()));
        InternetAddress[] internetAddressTo = new InternetAddress().parse(foxmailPropertiesDTO.getReceiveAddress());
        message.addRecipients(Message.RecipientType.TO, internetAddressTo);
        //郵件的文本內容
        message.setContent(foxmailPropertiesDTO.getSendContent(), "text/html;charset=UTF-8");
        //返回創建好的郵件對象
        return message;
    }

}

數據庫截圖

調用截圖

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