java使用javax.mail進行免費的郵件發送


1. 建議發送方使用阿里雲郵箱https://mail.aliyun.com/,阿里雲默認是開啟個人郵箱pop3、smtp協議的,所以無需在阿里雲郵箱里設置,pop3、smtp的密碼默認郵箱登錄密碼,沒有可以去申請一個。

如果要使用別的郵箱,可以自行查詢對應郵箱怎么開啟smtp協議。

2.pom.xml中引入javax.mail的jar包,這里使用1.4.7版本。

        <!-- javax mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

3.新建MailSend類:

package com.jarfk.util.email;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * Created by Administrator on 2017/9/29 0029.
 */
public class MailSend {
    private static final String EMAIL_OWNER_ADDR_HOST = "smtp.aliyun.com"; //smtp.163.com  smtp.aliyun.com  smtp.qq.com
    private static final String EMAIL_OWNER_ADDR = "xxx@aliyun.com";
    private static final String EMAIL_OWNER_ADDR_PASS = "xxx";

    public static void sendMail(String title, String email, String content) throws MessagingException {
        Properties prop = new Properties();
        prop.put("mail.host", EMAIL_OWNER_ADDR_HOST);
        prop.put("mail.transport.protocol", "smtp");
        prop.put("mail.smtp.auth", "true");
        //如果不加下面的這行代碼 windows下正常,linux環境下發送失敗,解決:http://www.cnblogs.com/Harold-Hua/p/7029117.html
        prop.setProperty("mail.smtp.ssl.enable", "true");
        //使用java發送郵件5步驟
        //1.創建sesssion
        Session session = Session.getInstance(prop);
        //開啟session的調試模式,可以查看當前郵件發送狀態
        //session.setDebug(true);

        //2.通過session獲取Transport對象(發送郵件的核心API)
        Transport ts = session.getTransport();
        //3.通過郵件用戶名密碼鏈接,阿里雲默認是開啟個人郵箱pop3、smtp協議的,所以無需在阿里雲郵箱里設置
        ts.connect(EMAIL_OWNER_ADDR, EMAIL_OWNER_ADDR_PASS);

        //4.創建郵件
        //創建郵件對象
        MimeMessage mm = new MimeMessage(session);
        //設置發件人
        mm.setFrom(new InternetAddress(EMAIL_OWNER_ADDR));
        //設置收件人
        mm.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //設置抄送人
        //mm.setRecipient(Message.RecipientType.CC, new InternetAddress("XXXX@qq.com"));

        //mm.setSubject("吸引力注冊郵件");
        mm.setSubject(title);

        //mm.setContent("您的注冊驗證碼為:<b style=\"color:blue;\">0123</b>", "text/html;charset=utf-8");
        mm.setContent(content, "text/html;charset=utf-8");

        // true表示開始附件模式 -----------------------------------------------------------------------
        /*MimeMessageHelper messageHelper = new MimeMessageHelper(mm, true, "utf-8");
        // 設置收件人,寄件人
        messageHelper.setTo(email);
        messageHelper.setFrom(EMAIL_OWNER_ADDR);
        messageHelper.setSubject(title);
        // true 表示啟動HTML格式的郵件
        messageHelper.setText(content, true);

        FileSystemResource file1 = new FileSystemResource(new File("d:/rongke.log"));
        FileSystemResource file2 = new FileSystemResource(new File("d:/新建文本文檔.txt"));
        // 添加2個附件
        messageHelper.addAttachment("rongke.log", file1);
        try {
            //附件名有中文可能出現亂碼
            messageHelper.addAttachment(MimeUtility.encodeWord("新建文本文檔.txt"), file2);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new MessagingException();
        }*/
        //-------------------------------------------------------------------------------------------
        //5.發送電子郵件

        ts.sendMessage(mm, mm.getAllRecipients());
    }

    public static void main(String[] args) throws MessagingException {
        //sendMail("吸引力注冊郵件", "xxx@qq.com", "您的注冊驗證碼為:<b style=\"color:blue;\">651899</b>");
        sendMail("吸引力", "xxx@qq.com", "spring boot 郵件測試");
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM