Integrated mail sending function in javaWeb project

reference

Java based on JavaMail to send mail to QQ mailbox
java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail

Write in front

Many Web projects now integrate the function of sending emails to users in order to remind users of some information, or sending verification codes to mailboxes to realize the registration function. So how to implement such a function is actually very simple.

Enable the SMTP function of the administrator mailbox

Take the QQ mailbox as an example. First, the administrator (that is, the company, website server) needs to open an SMTP service in their mailbox. In computer networks, the SMTP service is used to send mail.

In the QQ mailbox, in Settings-Account, enable the SMTP function.

After opening, QQ will provide you with an authorization code, which is equivalent to an API KEY, which will be used in the next program.

Main code

The specific code is as follows, but pay special attention to the specific code of different mailboxes. For example, QQ mailbox must provide SSL encryption operation, and jar is managed by maven

    <!-- 发送邮件依赖的 jar 包-->
    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>javax.mail-api</artifactId>
      <version>1.5.6</version>
    </dependency>

    <!-- MailSSLSocketFactory 加密的 jar 包 -->
    <!-- 注意这里的版本是 1.5.3 如果是低版本,比如我之前用的 1.4.4 ,那么下面的核心代码中有类不能使用 -->
    <dependency>
      <groupId>com.sun.mail</groupId>
      <artifactId>javax.mail</artifactId>
      <version>1.5.3</version>
    </dependency>
package com.www.utils;

import com.sun.mail.util.MailSSLSocketFactory;
import org.junit.Test;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

public class MailUtil {
    
    

    public  void sendMail() throws GeneralSecurityException, MessagingException {
    
    
        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        // ssl 加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        msg.setSubject("标题");
        StringBuilder builder = new StringBuilder();
        builder.append("内容");
        builder.append("\n内容");
        msg.setText(builder.toString());
        msg.setFrom(new InternetAddress("发送者邮箱"));

        Transport transport = session.getTransport();
        transport.connect("smtp.qq.com", "发送者邮箱", "授权码");

        transport.sendMessage(msg, new Address[] {
    
     new InternetAddress("接收者邮箱") });
        transport.close();
    }
}

effect

Insert picture description here

The problem

I don’t know why, the messages I sent were all treated as spam by Tencent and stored in the trash can.

Guess you like

Origin blog.csdn.net/qq_34902437/article/details/105723092