Package com.test.mail;
Import java.util.Properties;
Import Javax.mail.Authenticator;
Import Javax.mail.BodyPart;
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.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;
public class SendMail {
public static void Main (string[] args) {
try {
String username= "source@163.com";
String password= "xxxxx";
String smtp_server= "smtp.163.com";
String From_mail_address=username;
String to_mail_address= "to@163.com";
Authenticator auth = new Popupauthenticator (Username,password);
Properties Mailprops = new properties ();
Mailprops.put ("Mail.smtp.auth", "true");
Mailprops.put ("username", username);
Mailprops.put ("password", password);
Mailprops.put ("Mail.smtp.host", smtp_server);
Session mailsession = Session.getdefaultinstance (Mailprops, auth);
MimeMessage message = new MimeMessage (mailsession);
Message.setfrom (New InternetAddress (from_mail_address));
Message.setrecipient (Message.RecipientType.TO, New InternetAddress (to_mail_address));
Message.setsubject ("Mail Test");
Mimemultipart multi = new Mimemultipart ();
BodyPart Textbodypart = new MimeBodyPart ();
Textbodypart.settext ("e-mail test content!");
Multi.addbodypart (Textbodypart);
Message.setcontent (multi);
Message.savechanges ();
Transport.send (message);
catch (Exception ex) {
System.err.println ("The reason for the mail failure is:" + ex.getmessage ());
SYSTEM.ERR.PRINTLN ("Specific error Reason:");
Ex.printstacktrace (System.err);
}
}
}
Class Popupauthenticator extends Authenticator {
private String username;
private String password;
public popupauthenticator (String username,string password) {
this.username=username;
This.password=password;
}
public passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (This.username, This.password);
}
}