Talk about the problems encountered in sending messages in Java _java

Source: Internet
Author: User
Tags auth starttls

Objective

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:

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 SEssion = session.getinstance (props);//create mail session mimemessage messages = new MimeMessage (session);//New Message object messages by mail conversation . 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 receive type to//set the content of the letter//message.settext (mailcontent); Send a plain text message TODO message.setsubject (title);/Set the title Message.setcontent (content, "TEXT/HTML;CHARSET=GBK"); Send HTML message, content style is richer message.setsentdate (new Date ());//Set the time to send message.savechanges ()//store mail message/email 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 ();
 
}} 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 {sessions session = Session.getinstance (props);//Create a mail session mimemessage messages = new MimeMessage (session);//New Message object created by mail conversation 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 receive type to// Set up letter content//message.settext (mailcontent); Send a plain text message TODO message.setsubject (title);/Set the title Message.setcontent (content, "TEXT/HTML;CHARSET=GBK");  Send HTML message, content style is richer message.setsentdate (new Date ());//Set the time to send message.savechanges ()//store mail message/email 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 (); }
 }
 
}

The above is the entire content of this article, I hope the content of this article for everyone's learning work can help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.