Today the project has the need to use Java mail to send mail, find the relevant code on the Internet, the code is as follows:
Import java.io.IOException;
Import java.util.Properties;
Import Javax.mail.Message;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
public class SendMail {
public static void Main (string[] args) throws IOException, Exception {
String host = "smtp.163.com"; Sender uses e-mail server to send email
String from = "sender's mailbox"; Origin of Outgoing mail (sender's mailbox)
String to = "Recipient Mailbox"; e-mail Destination (recipient's mailbox)
Get System Properties
Properties props = System.getproperties ();
Setup Mail Server
Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.port", 25);
Get session
Props.put ("Mail.smtp.auth", "true"); So that you can pass the validation
Myauthenticator Myauth = new Myauthenticator (from, "Sender's mailbox password");
Session session = Session.getdefaultinstance (props, Myauth);
Session.setdebug (TRUE);
Define message
MimeMessage message = new MimeMessage (session);
Set the From address
Message.setfrom (New InternetAddress (from));
Set the To address
Message.addrecipient (Message.RecipientType.TO, New internetaddress (to));
Set the subject
Message.setsubject ("Test program! ");
Set the Content
Message.settext ("This is a test program for sending e-mails written in Java!") ");
Message.savechanges ();
Transport.send (message);
}
}
Class Myauthenticator extends Javax.mail.Authenticator {
Private String struser;
Private String strpwd;
Public Myauthenticator (string user, string password) {
This.struser = user; this.strpwd = password;
}
Protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (struser, strpwd);
}
}
The above import of two jar packages is Activation-1.1.1.jar, Javax.mail-1.5.4.jar
Then test the results, the following error always appears:
DEBUG smtp:found Extension "STARTTLS", Arg ""
DEBUG smtp:found Extension "8BITMIME", Arg ""
DEBUG smtp:attempt to authenticate using mechanisms:
Login PLAIN digest-md5 NTLM DEBUG smtp:auth login command Trace suppressed
DEBUG Smtp:auth LOGIN failed Exception in thread "main" javax.mail.authenticationfailedexception:535 Error:authenticati On failed
At Com.sun.mail.smtp.smtptransport$authenticator.authenticate (smtptransport.java:826) at Com.sun.mail.smtp.SMTPTransport.authenticate (smtptransport.java:761) at Com.sun.mail.smtp.SMTPTransport.protocolConnect (smtptransport.java:685) at Javax.mail.Service.connect ( service.java:317) at Javax.mail.Service.connect (service.java:176) at Javax.mail.Service.connect (service.java:125) At Javax.mail.Transport.send0 (transport.java:194) at Javax.mail.Transport.send (transport.java:124) at Com.eversec.smart.common.utils.SendMail.main (sendmail.java:131)
After finding the problem, there are two places that are critical:
1, the sender of the mailbox must be set to open "Pop3/smtp/imap", which must be set "client authorization password", or error;
2, the password of the sender's mailbox is very key, is this sentence myauthenticator Myauth = new Myauthenticator (from, "Sender's mailbox password"); This password must be the client authorization password set above, or the above error will occur.
Http://www.cnblogs.com/zhh-bky/articles/4725209.html
Summary of issues related to Java Mail e-mail (GO)