Package com;
Import java.util.Properties;
Import Javax.mail.Authenticator;
Import Javax.mail.BodyPart;
Import Javax.mail.Message;
Import Javax.mail.Multipart;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;
Import Com.abcchina.PopupAuthenticator;
public class Send {
public static void Main (string[] args) throws Exception {
Properties Props = new properties ();
String host = "smtp.ym.163.com"; 163 SMTP server for free enterprise mailboxes
String from = "[email protected]"; My business e-mail address
String to = "[email protected]"; e-mail address to be sent to
String username = "Web";
String Password = "******"; //
Props.put ("Mail.smtp.auth", "true");
Props.put ("Mail.smtp.host", host);
String content = "Hello";
String subject= "This is a good news 1";
The authentication is the use of the mailbox full spelling password instead of the user password this should pay attention to
Authenticator Smtpauth = new Popupauthenticator (
From, password);
Session session = Session.getdefaultinstance (props, Smtpauth);
Session.setdebug (TRUE);
MimeMessage message = new MimeMessage (session);
Message.setfrom (New InternetAddress (from));
Message.addrecipient (Message.RecipientType.TO, New internetaddress (to));
BodyPart MDP = new MimeBodyPart ();
Mdp.setcontent (Content, "text/html;charset=gb2312");
Multipart mm = new Mimemultipart ();
Mm.addbodypart (MDP);
Message.setcontent (mm);
Message.setsubject (SubJect);
Message.savechanges ();
Transport Transport = Session.gettransport ("SMTP");
Transport.connect (host, username, password);//link with the user name and password instead of the full spelling password of the mailbox to note
Transport.sendmessage (Message, message.getallrecipients ());
Transport.close ();
}
}
=======================================================
Package Com.abcchina;
Import Javax.mail.Authenticator;
Import javax.mail.PasswordAuthentication;
public class Popupauthenticator extends Authenticator {
String Username=null;
String Password=null;
Public Popupauthenticator () {}
Public popupauthenticator (String username,string password){
This.username=username;
This.password=password;
}
Public passwordauthentication Performcheck (String user,string pass) {
Username = user;
Password = pass;
return Getpasswordauthentication ();
}
Protected Passwordauthentication getpasswordauthentication () {
return new passwordauthentication (username, password);
}
}
What do not understand the direct comment can!!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Javax.mail Call Enterprise Mailbox Send mail, call personal mailbox Send mail