1. 程式人生 > >java----傳送郵件2

java----傳送郵件2

1.新增jar包 

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.5</version>
        </dependency>

2.傳送程式碼

public class EmailUtils {
    /**
     * https://www.cnblogs.com/jimboi/p/6406237.html
     * 純文字內容郵件
     */
    public static void sendEmail(String emailSender,String emailSenderPassword,String subject,String emailReceiver,String content) throws EmailException {
        Email email = new SimpleEmail();
        email.setHostName("smtp.qq.com");
        //密碼是騰訊生成的第三方驗證碼 ascxdfbgztavbdlab
        email.setAuthentication(emailSender,emailSenderPassword);
        email.setFrom(emailSender);
        email.addTo(emailReceiver);
        email.setSubject(subject);
        //恭喜您註冊自媒體號稽核通過了,點選如下連結登入: <a href="http://www.baidu.com">xx電視臺自媒體登入平臺</a>
        email.setContent(content,"text/html;charset=UTF-8");
        email.send();
    }

3.測試程式碼 

public static void main(String[] args) {
        try {
            EmailUtils.sendEmail("[email protected]","ascxdfbgztavblab","稽核通過","[email protected]","恭喜您註冊自媒體號稽核通過了,點選如下連結登入: <a href=\"http://baidu,com">xx電視臺自媒體登入平臺</a>");
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }

4.如上去25埠傳送郵件,在本地測試可以通過。當打到線上環境時,發現報錯了。 

後來經排查發現,我們用的線上環境是阿里雲,阿里雲將 25埠封閉了。繼續查詢資料,發現騰訊郵箱支援465埠,需要設定ssl。不多說,程式碼如下。 

public class EmailUtils {
    /**
     * https://www.cnblogs.com/jimboi/p/6406237.html
     * https://www.cnblogs.com/15ho/p/5891148.html
     * 純文字內容郵件
     */
    public static void sendEmail(EmailVO emailVO) throws EmailException, MessagingException {

        //設定ssl連線
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", "smtp.qq.com");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.auth", "true");
        //建立郵件會話
        Session session = Session.getInstance(props, new Authenticator() {
            //身份認證
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(emailVO.getEmailSender(),emailVO.getEmailSenderPassword());
            }
        });
        //建立郵件物件
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(emailVO.getEmailSender()));
        message.setRecipients(Message.RecipientType.TO, emailVO.getEmailReceiver());
        message.setSubject(emailVO.getSubject());
        message.setContent(emailVO.getContent(),"text/html;charset=UTF-8");
        //傳送郵件
        Transport.send(message);
  }
}

注意:如上郵箱伺服器是採用騰訊的。所以發件人必須是騰訊的郵箱。 

ps:今天遇到個問題,我用的騰訊的郵箱伺服器,賬號+授權碼採用 網易的,傳送郵件始終報535:認證失敗。後來才知道郵箱伺服器認證只能認證自己家的賬號。