Nothing to do research a bit javamail e-mail. Although more complex than the Spring framework mail, it is worth studying.
Mail.jar and the jar package of this program will be uploaded in the attachment for everyone to use to learn.
The first class, used for password verification, is important to note.
Package com.xian.gmail; Import Javax.mail.authenticator;import javax.mail.PasswordAuthentication; /** * JavaMail's password authenticator * @author Jia Xiaoxian */public class Myauther extends authenticator{String username=null; String Password=null;public Myauther () {}public myauther (string userName, string password) {this.username = UserName; This.password = password;} Getpasswordauthentication name must be right. The source code is return null. Equivalent to rewriting protected passwordauthentication getpasswordauthentication () {return new passwordauthentication (UserName, password);} }
The second class is an entity class that does some basic parameter processing
package com.xian.gmail;public class mailsenderinfo { // IP and port of the server sending the mail private string host; private string port= " "; // Mail Sender's address private string address; // e-Mail recipient's address private String toaddress; // Login Email server username and password private String userName; private String password; // whether authentication is required private Boolean Validate=true; // Mail Topics private String subJect; // text content of messages private String context; // file name of the message attachment private string[] filename; /** * Get mail Session Properties */ /* public Properties GetProperties () { properties pro=new properties (); Pro.put ("Mail.smtp.host", This.host); pro.put ("Mail.smtp.port", this.port); pro.put ("Mail.smtp.auth", validate ? "true": "false"); Return pro; }*/public string gethost () {return host;} Public void sethost (String host) {this.host = host;} Public string getport () {return port;} Public void setport (String port) {this.port = port;} Public string getaddress () {return address;} Public void setaddress (string address)  {THIS.ADDRESS =&Nbsp;address;} Public string gettoaddress () {return toaddress;} Public void settoaddress (string toaddress) {this.toaddress = toaddress;} Public string getusername () {return username;} Public void setusername (String username) {this.username = username;} Public string getpassword () {return password;} Public void setpassword (String password) {this.password = password;} Public boolean isvalidate () {return validate;} Public void setvalidate (boolean validate) {this. Validate=validate;} Public string getsubject () {return subject;} Public void setsubject (String subject) {this.subject = subject;} Public string getcontext () {return context;} Public void setcontext (String context) {this.context = context;} Public string[] getfilename () {return filename;} Public void setfilename (String[] filename) {this.filename = filename;} }
The third class is the implementation class, and the specific operation to send the message is here
package com.xian.gmail;import java.util.date;import java.util.properties;import javax.mail.bodypart;import javax.mail.message;import javax.mail.messagingexception;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 javax.mail.address; import javax.mail.multipart; /** * Simple Mail (mail without attachments) transmitter */ public class MailSender { public boolean sendtextmail (Mailsenderinfo mailinfo) { // Determine if identity authentication is required myauther authenticator = null; if (Mailinfo.isvalidate ()) { // If authentication is required, create a password validator authenticator = new myauther ( Mailinfo.getusername (), mailinfo.getpassword ()); } properties pro= GetProperties (Mailinfo); // constructs a session that sends messages based on mail session properties and password validators session sendmailsession = session.getdefaultinstance (Pro,authenticator); try { // Create a mail message based on session Message Mailmessage = new miMemessage (sendmailsession); // Create Mail Sender Address address from = new internetaddress (Mailinfo.getaddress ()); // set the sender of the mail message Mailmessage.setfrom (from); // Create the recipient address of the message and set it to the mail message in address to = new internetaddress (Mailinfo.gettoaddress ()); mailmessage.setrecipient (message.recipienttype.to,to); // set the subject of the mail message mailmessaGe.setsubject (Mailinfo.getsubject ()); / / set the time the mail message was sent Mailmessage.setsentdate (New date ()); // set the main content of the mail message String Mailcontent = mailinfo.getcontext (); mailmessage.settext (mailcontent); // Send mail transport.send (MailMessage); return true; } catch (Messagingexception ex) { ex.printstacktrace (); } return false; } /** * Send mail * @param in HTML format mailInfo Mail basic information * @return Success * @author Jia Xiaoxian */ public boolean sendhtmlmail ( Mailsenderinfo mailinfo) { myauther auth=null; if ( Mailinfo.isvalidate ()) { auth=new myauther (Mailinfo.getusername (), Mailinfo.getpassword ()); } properties pro= GetProperties (mailinfo); try&nbSp {session sendmailsession = session.getdefaultinstance (Pro, auth); Message msg = new mimemessage (sendmailsession); Address address = new internetaddress (Mailinfo.getaddress ()); Msg.setFrom (address); Address toaddress=new internetaddress (Mailinfo.gettoaddress ()); Msg.setrecipient ( message.recipienttype.to, toaddress); Msg.setsubject (Mailinfo.getsubject ()); Msg.setsentdate (New Date ( )); The // minimultipart class is a container class that contains objects of type MimeBodyPart Multipart mailPart=new Mimemultipart (); bodypart html =new mimebodypart (); Html.setcontent ( Mailinfo.getcontext (), "Text/html;charset=utf-8"), Mailpart.addbodypart (HTML);// set Minimultipart object to message content msg.setcontent (mailpart); // Send mail transport.send (msg); return true;} catch (messagingexception e) {// todo: handle exceptione.printstacktrace ();} return false; } public Properties getproperties (mailsenderinfo mailinfo) { properties pro = new properties (); string Mailname=mailinfo.getusername (); string mailtype= Mailname.substring (Mailname.indexof (' @ ') +1, mailname.length ()); if (Mailtype.indexof ("Gmail")!=-1) { pro.setproperty (" Mail.smtp.host ", " smtp.gmail.com "); pro.setproperty (" Mail.smtp.port ", " 465 ") pro.setproperty (" Mail.smtp.auth ", "true"); &nbsP; pro.setproperty ("Mail.smtp.socketFactory.class", "Javax.net.ssl.SSLSocketFactory"); pro.setproperty ("Mail.smtp.socketFactory.fallback", "false"); pro.setproperty (" Mail.smtp.socketFactory.port ", " 465 "); pro.setproperty ("Mail.smtp.ssl", "true"); } if (Mailtype.equals ("163.com") | | Mailtype.equals ("sohu.com") | | Mailtype.equals ("126.com")) { String mailserver = "SMTP." +mailtype; &nbSp; pro.setproperty ("Mail.smtp.host", mailserver); pro.setproperty ("Mail.smtp.port" , (" pro.setproperty"); Mail.smtp.auth ", " true "); } if (Mailtype.indexof ("Sina")!=-1) { Pro.setproperty ("Mail.smtp.host", "smtp.sina.com.cn"); pro.setproperty ("Mail.smtp.port", "); pro.setproperty ("Mail.smtp.auth", "true"); } return pro; }}
The fourth class is equivalent to an interface class that is used to pass parameters.
package com.xian.gmail;public class send {/** * Required Parameters user name, password, recipient for sending mail , themes, content * @param userName * @param password * @param toaddress * @param subJect * @param context */ public Static void sendmail (string username,string password,string toaddress,string subject,string context) { mailsenderinfo mailinfo=new Mailsenderinfo (); mailinfo.setusername (UserName); mailinfo.setpassword (password); mailinfo.setaddress (UserName); mailinfo.settoaddress (toaddress); Mailinfo.setsubject (SubJect); mailinfo.setcontext (context); mailsendEr send=new mailsender (); //This class mainly to send mail boolean result=send.sendtextmail (mailinfo);//Send stylistic format if (Result) { system.out.println ("sent successfully"); }else{ system.out.println ("Send Failed"); } };}
Class fifth specific invocation
Import Com.xian.gmail.send;public class Gomail extends send{public void Main () {} public void Main (string[] args {Send.sendmail ("*****@163.com", "* * * * * *", "***@163.com", "Hello", "Can you speek 中文版?") Mr Xiao Jia ");}}
Simple implementation, Universal 126,163,sohu,sina,gmail. QQ forgot to add it yourself
51cto jar Package Illegal file, uploaded csdn
Download Link: http://download.csdn.net/detail/jiazhipeng12/8578161
This article is from the "Jia Xiaoxian" blog, make sure to keep this source http://hackerxian.blog.51cto.com/9240575/1630593
Send mail using JavaMail