Preface
The code for sending the email was copied directly from a previous application. The Tencent email service I used in the past has no problems with the execution of the program. Later, after modifying it to Microsoft's office365 mail service, I encountered two problems.
Question 1, tls encryption settings
The exception information is as follows:
Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
This is easier to solve. After finding some information, add the following configuration:
mail.smtp.starttls.enable = true
Question 2: Prompt protocol is null:
The exception information is as follows:
Exception in thread "main" javax.mail.NoSuchProviderException: Invalid protocol: null at javax.mail.Session.getProvider(Session.java:449) at javax.mail.Session.getTransport(Session.java:667) at javax.mail.Session.getTransport(Session.java:648) at javax.mail.Session.getTransport(Session.java:634)
This problem is encountered only after deploying the application to a production environment. After inspection, I found that the jar package I called was not the version I specified in maven. Later it was confirmed that the jar package used by the application conflicted with the jar package used by the container (i.e. jetty). The jar version used by the container is older, but the jars of the container are loaded by default. There are two ways to solve this problem:
Re-write code with jars that depend on containers;
Update the jar of the container.
The second choice is somewhat dangerous, so use the first option, just modify one line:
Transport transport = session.getTransport("smtp");This problem will occur in javax.mail version 1.4. Later, higher versions will use SMTP protocol to send emails by default.
Modified program:
package com.zhyea.zytools;import java.util.Date;import java.util.Properties;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class MailSender { private static final String MAIL_SMTP_HOST = "smtp.exmail.qq.com"; private static final Integer MAIL_SMTP_PORT = 587; private static final Boolean MAIL_SMTP_AUTH = true; private static final String MAIL_SMTP_USER = "[email protected]"; private static final String MAIL_SMTP_PASSWORD = "robinzhyea"; private static Properties props = new Properties(); static { props.put("mail.smtp.host", MAIL_SMTP_HOST); props.put("mail.smtp.auth", MAIL_SMTP_AUTH); props.put("mail.smtp.user", MAIL_SMTP_USER); props.put("mail.smtp.password", MAIL_SMTP_PASSWORD); props.put("mail.smtp.starttls.enable", true); } /** * Send email*/ public static void send(String to, String title, String content) { try { Session session = Session.getInstance(props);//Create a mail session MimeMessage message = new MimeMessage(session);//Create a new message object from the mail session message.setFrom(new InternetAddress(MAIL_SMTP_PASSWORD));//Set the sender's address message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//Set the recipient and set its receiving type to TO //Set the message content//message.setText(mailContent); //Send plain text email TODO message.setSubject(title);//Set the title message.setContent(content, "text/html;charset=gbk"); //Send HTML mail, the content style is richer message.setSentDate(new Date());//Set the sending time message.saveChanges();//Storage email information//Send email Transport transport = session.getTransport("smtp"); transport.connect(MAIL_SMTP_USER, MAIL_SMTP_PASSWORD); transport.sendMessage(message, message.getAllRecipients());//Send email, the second parameter is all set recipient addresses transport.close(); } catch (Exception e) { e.printStackTrace(); } } }package com.zhyea.zytools; import java.util.Date;import java.util.Properties; import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage; public class MailSender { private static final String MAIL_SMTP_HOST = "smtp.exmail.qq.com"; private static final Integer MAIL_SMTP_PORT = 587; private static final Boolean MAIL_SMTP_AUTH = true; private static final String MAIL_SMTP_USER = "[email protected]"; private static final String MAIL_SMTP_PASSWORD = "robinzhyea"; private static Properties props = new Properties(); static { props.put("mail.smtp.host", MAIL_SMTP_HOST); props.put("mail.smtp.auth", MAIL_SMTP_AUTH); props.put("mail.smtp.user", MAIL_SMTP_USER); props.put("mail.smtp.password", MAIL_SMTP_PASSWORD); props.put("mail.smtp.starttls.enable", true); } /** * Send email*/ public static void send(String to, String title, String content) { try { Session session = Session.getInstance(props);//Create a mail session MimeMessage message = new MimeMessage(session);//Create a new message object from the mail session message.setFrom(new InternetAddress(MAIL_SMTP_PASSWORD));//Set the sender's address message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//Set the recipient and set its receiving type to TO //Set the message content//message.setText(mailContent); //Send plain text email TODO message.setSubject(title);//Set the title message.setContent(content, "text/html;charset=gbk"); //Send HTML mail, the content style is richer message.setSentDate(new Date());//Set the sending time message.saveChanges();//Storage email information//Send email Transport transport = session.getTransport("smtp"); transport.connect(MAIL_SMTP_USER, MAIL_SMTP_PASSWORD); transport.sendMessage(message, message.getAllRecipients());//Send email, the second parameter is all set recipient addresses transport.close(); } catch (Exception e) { e.printStackTrace(); } } }The above is all the content of this article. I hope the content of this article will be helpful to everyone's study and work.