java 發送 mail 純文本發送和html格式發送

原文鏈接:https://www.cnblogs.com/luffyu/p/6146718.html

java 發送 mail 純文本發送和html格式發送

一:需要引入mail maven jar包

<!--郵件發送包-->
<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4.7</version>
</dependency>

二:代碼示例

  • 2.1 定義消息實體類
package textAnalysis.dto;

/**
 * Created by 草帽boy on 2016/12/8.
 */

public class MailInfo {
    //郵箱服務器 如smtp.163.com
    private String host ;
    //用戶郵箱 如**@163
    private String formName ;
    //用戶授權碼 不是用戶名密碼 可以自行查看相關郵件服務器怎麼查看
    private String formPassword ;
    //消息回覆郵箱
    private String replayAddress ;
    //發送地址
    private String toAddress ;
    //發送主題
    private String subject ;
    //發送內容
    private String content ;

    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public String getFormName() {
        return formName;
    }
    public void setFormName(String formName) {
        this.formName = formName;
    }
    public String getFormPassword() {
        return formPassword;
    }
    public void setFormPassword(String formPassword) {
        this.formPassword = formPassword;
    }
    public String getReplayAddress() {
        return replayAddress;
    }
    public void setReplayAddress(String replayAddress) {
        this.replayAddress = replayAddress;
    }
    public String getToAddress() {
        return toAddress;
    }
    public void setToAddress(String toAddress) {
        this.toAddress = toAddress;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}
  • 2.2 兩種方式實現

首先你需要獲取你的授權碼,以@163.com爲例 http://blog.csdn.net/hughnes/article/details/52070878
sendTextMail是純文本發送
sendHtmlMail是HTML格式標籤發送 這種方式可以識別html標籤

package textAnalysis.util;

import textAnalysis.dto.MailInfo;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;

/**
 * Created by 草帽boy on 2016/12/8.
 */
public class MailSendUtil {
    private final static String host = "smtp.163.com"; //163的服務器
    private final static String formName = "[email protected]";//你的郵箱
    private final static String password = "***********"; //授權碼
    private final static String replayAddress = "[email protected]"; //你的郵箱


    public static void sendHtmlMail(MailInfo info)throws Exception{
        info.setHost(host);
        info.setFormName(formName);
        info.setFormPassword(password);   //網易郵箱的授權碼~不一定是密碼
        info.setReplayAddress(replayAddress);

        Message message = getMessage(info);
        // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象
        Multipart mainPart = new MimeMultipart();
        // 創建一個包含HTML內容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 設置HTML內容
        html.setContent(info.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 將MiniMultipart對象設置爲郵件內容
        message.setContent(mainPart);
        Transport.send(message);
    }

    public static void sendTextMail(MailInfo info) throws Exception {

        info.setHost(host);
        info.setFormName(formName);
        info.setFormPassword(password);   //網易郵箱的授權碼~不一定是密碼
        info.setReplayAddress(replayAddress);
        Message message = getMessage(info);
        //消息發送的內容
        message.setText(info.getContent());

        Transport.send(message);
    }

    private static Message getMessage(MailInfo info) throws Exception{
        final Properties p = System.getProperties() ;
        p.setProperty("mail.smtp.host", info.getHost());
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.user", info.getFormName());
        p.setProperty("mail.smtp.pass", info.getFormPassword());

        // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session
        Session session = Session.getInstance(p, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
            }
        });
        session.setDebug(true);
        Message message = new MimeMessage(session);
        //消息發送的主題
        message.setSubject(info.getSubject());
        //接受消息的人
        message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
        //消息的發送者
        message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"餘高峯"));
        // 創建郵件的接收者地址,並設置到郵件消息中
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress()));
        // 消息發送的時間
        message.setSentDate(new Date());


        return message ;
    }
}

測試

@Test
public void send(){
    String mail = "[email protected]"; //發送對象的郵箱
    String title = "我有一句話跟你說";
    String content = "<div>你不在學校嗎?</div><br/><hr/><div>記得28號來學校</div>";
    MailInfo info = new MailInfo();
    info.setToAddress(mail);
    info.setSubject(title);
    info.setContent(content);
    try {
       //MailSendUtil.sendTextMail(info);
        MailSendUtil.sendHtmlMail(info);
    } catch (Exception e) {
        System.out.print("'"+title+"'的郵件發送失敗!");
        e.printStackTrace();
    }
}

採用的Junit的方式測試的採用 sendTextMail 的結果是
在這裏插入圖片描述
採用的Junit的方式測試的採用 sendHtmlMai l的結果是
在這裏插入圖片描述

轉載地址:https://www.cnblogs.com/luffyu/p/6146718.html

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