Java - using JavaMail to send an email to the user's mailbox

<----—–start----—–>
Send an email via javamail:
Earlier, I had written a tool class MailUtils for sending mail. The following points need to be noted:
① The mail server address of the Outbox.
② Outbox account number.
③ Mailbox authorization code, which is set in the used mailbox background management.
④ Address of the activation message.

public class MailUtils {
    private static String smtp_host = "smtp.163.com"; // NetEase
    private static String username = "niwotaxuexiba_search@163.com"; // email accounts 
    private static String password = "niwotaxuexiba123"; // Email authorization code

    private static String from = "niwotaxuexiba_search@163.com"; // Use current account
    public static String activeUrl = "http://localhost:9003/bos_fore/customer_activeMail";

    public static void sendMail(String subject, String content, String to) {
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", smtp_host);
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.auth", "true");
        Session session = Session.getInstance(props);
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(from));
            message.setRecipient(RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setContent(content, "text/html;charset=utf-8");
            Transport transport = session.getTransport();
            transport.connect(smtp_host, username, password);
            transport.sendMessage(message, message.getAllRecipients());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to send mail...");
        }
    }

    //Test code: test whether the mail can be sent successfully
    public static void main(String[] args) {
        sendMail("Test mail", "Hello, let's learn", "niwotaxuexiba_search@163.com");
    }
}

Set three parameters in sendmail (parameter ①, parameter ②, parameter ③) method:
① Email subject;
② Email content;
③ Target email account (to whom).
You need to add the coordinates of the javamail mail mailing kit to the pom.xml file. After adding the dependency, re install the following.
<----—–end----—–>

Tags: Session xml

Posted on Fri, 01 May 2020 14:04:09 -0700 by chriskiely