JAVA 使用Javax.mail向公司內(nèi)部Outlook郵箱發(fā)送郵件

參考:

1、用Java發(fā)送郵件 帶有圖片和附件

2、java代碼如何發(fā)送一封帶圖片的郵件

3、Javamail官方論壇-沒啥人

package com.cy.sendmail.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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;

/**
 * @author z
 * @title: SendEmailUtil
 * @projectName sendmail
 * @description: 向outlook發(fā)送郵件工具
 * @date 2020/11/17 13:43
 */
@Component
public class SendEmailUtil {

    private Properties props;

    private Session session;

    private MimeMessage message;

    @Autowired
    private DateFormatUtil dateFormatUtil;

    @Value("${Email.fromEmail}")
    private String fromEmail;

    @Value("${Email.server}")
    private String emailServer;

    @Value("${Email.fromEmailPwd}")
    private String fromEmailPwd;

    /**
     * 郵件內(nèi)容部分,html
     * @return
     */
    public void emailText(Date pwdLastSet, int days) {
        try{
            Multipart mimeMultipart = new MimeMultipart();
            // 主題
            message.setSubject("自動發(fā)送:提醒測試","utf-8");

            // 內(nèi)容
            MimeBodyPart text = new MimeBodyPart();
            text.setContent("您好:<br>&nbsp;&nbsp;" +
                    "測試郵件內(nèi)容","text/html;charset=UTF-8");

            mimeMultipart.addBodyPart(text);

            // 放到Message中
            message.setContent(mimeMultipart);
            message.saveChanges();

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

    /**
     * 發(fā)送郵件
     */
    public void send(String toEmail,Date pwdLastSet, int days){
        try{
            props = new Properties();
            //發(fā)件人
            String from = props.getProperty("fromEmail", this.fromEmail);
            //收件人
            String to = props.getProperty("toEmail", toEmail);
            props.put("mail.transport.protocol", "smtp"); // 設(shè)置協(xié)議
            props.put("mail.smtp.host", emailServer); // 服務(wù)器地址
            props.put("mail.smtp.port", "587"); // 端口號,默認(rèn)25,TLS加密587,SSL加密465
            props.put("mail.smtp.auth", "true");
            //當(dāng)前smtp host設(shè)為可信任
            props.put("mail.smtp.ssl.trust", "mail.chaoyue.com.cn");
            props.put("mail.smtp.ssl", "true");// 設(shè)置是否使用ssl安全連接
            props.put("mail.smtp.starttls.enable", "true"); // 設(shè)置是否使用tsl安全連接
//            props.put("mail.debug", "true");// 設(shè)置是否顯示debug信息 true 會在控制臺顯示相關(guān)信息
            // 用戶驗(yàn)證并返回Session
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    //發(fā)件人郵箱前綴,郵箱登錄賬戶名
                    String userId = props.getProperty("userId", fromEmail.substring(0, fromEmail.indexOf("@")));
                    //發(fā)件人郵箱密碼(qq、163等郵箱用的是授權(quán)碼,outlook是密碼)
                    String password = props.getProperty("password", fromEmailPwd);
                    return new PasswordAuthentication(userId, password);
                }
            };
            session = Session.getInstance(props, authenticator);
            // 創(chuàng)建MimeMessage消息對象,消息頭配置了收發(fā)郵箱的地址,消息體包含了郵件標(biāo)題和郵件內(nèi)容
            message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from)); // 發(fā)送人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));// 接收者類型:TO代表直接發(fā)送,CC代表抄送,BCC代表秘密抄送
            // 生成文件內(nèi)容,兩個參數(shù)是我需要在郵件中顯示的內(nèi)容
            emailText(pwdLastSet, days);
            // 發(fā)送
            Transport.send(message);
            System.out.println("-------------" + toEmail + "--郵件發(fā)送成功!");
        }catch (Exception e) {
            e.printStackTrace();
           System.out.println("*************"+ toEmail +"郵件發(fā)送失??!");
        }
    }

}

存在的問題

①2020.11.23 發(fā)送5封郵件后即報該錯誤:Connection closed by remote host。似乎由于發(fā)送過于頻繁被郵件服務(wù)器鎖定,與郵件服務(wù)器設(shè)置有關(guān),待研究
報錯.png
202012.01 Javax.mail打印詳細(xì)的Debug信息,打開之前代碼中這一行的注釋:
// props.put("mail.debug", "true");// 設(shè)置是否顯示debug信息 true 會在控制臺顯示相關(guān)信息
能看到報錯:
421 4.4.2 Message submission rate for this client has exceeded the configured limit
郵件服務(wù)器限制了每分鐘最大發(fā)送數(shù)量,需要修改服務(wù)器配置參數(shù)MessageRateLimit:
參考以下地址修改:
(1)、Exchange – 421 4.4.2 Message submission rate for this client has exceeded the configured limit
(2)、Exchange user send mail error 421 4.4.2 Message submission rate for this client has exceeded the configured limit
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容