The code to send the message was copied directly from a previous application. Previously used Tencent's mail service, the program was executed without any problems. Later modified to Microsoft office365 Mail Service, but encountered two problems.
Problem one, TLS encryption settings
The exception information is as follows:
Copy Code code as follows:
Exception in thread "main" com.sun.mail.smtp.smtpsendfailedexception:530 5.7.57 SMTP; Client wasn't authenticated to send anonymous mail during mail from
This is easier to solve. Looking for some information, add the following configuration can be:
Mail.smtp.starttls.enable = True
Question two, indicating that the 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 when the application is deployed to a production environment. It was checked that the jar package that was invoked was not the version I specified in Maven. It was later confirmed that the jar bundle used by the application used for the jar bundle and the container (that is, jetty) was conflicting. The jar version used by the container is older, but the container's jar is first loaded by default. There are two ways to solve this problem:
- The jar that relies on the container to write code again;
- Updates the container's jar.
The second choice is somewhat dangerous, the first option is OK, only one line can be modified:
Transport transport = session.getTransport("smtp");
This problem will appear in the Javax.mail 1.4 version. Later versions will send mail by default with the SMTP protocol.
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 = "robin@zhyea.com";
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 mail/public static void Send (String to, string title, String COntent) {try {session sessions = session.getinstance (props);//create mail conversation mimemessage message = new MIMEMESSAG E (session),//Create a new Message Object Message.setfrom (new InternetAddress (Mail_smtp_password)) by the mail conversation;/Set the sender's address message.setre Cipient (Message.RecipientType.TO, New internetaddress (To)), set the recipient, and set its receive type to//set the content of the letter//message.settext (mail Content); Send a plain text message TODO message.setsubject (title);/Set the title Message.setcontent (content, "TEXT/HTML;CHARSET=GBK");
Send HTML message, content style richer message.setsentdate (new Date ());//Set the time to send message.savechanges ()//store mail message//Mail
Transport transport = Session.gettransport ("SMTP");
Transport.connect (Mail_smtp_user, Mail_smtp_password);
Transport.sendmessage (Message, message.getallrecipients ())//Send mail, where the second parameter is all the set of recipient address transport.close ();
catch (Exception e) {e.printstacktrace ();
}
}
}