Step one: Encapsulate Sender account password
Import Javax.mail.Authenticator;
Import javax.mail.PasswordAuthentication;
/**
* Sender account password
* @author Zhangdi
*
*/
public class Mailauthenticator extends authenticator{
public static String USERNAME = "";
public static String PASSWORD = "";
Public Mailauthenticator () {
}
Protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (USERNAME, PASSWORD);
}
}
Step Two: Provide the Send Operation class
import java.util.Date;
import java.util.Properties;
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;/**
* Mail send operation class
*
* @author Zhangdi
*
*/
Public class Mailoperation {
/**
* Send mail
* @param user Sender Mailbox
* @param Password Authorization Code (note not the email login password)
* @param host
* @param from Sender
* @param to Recipient mailbox
* @param subject Mail Subject
* @param content Messages
* @return Success sent successfully failure send failed
* @throws Exception
*/
Public string SendMail (string user, string password, string host,
string from, string to, string subject, string content)
throws Exception {
if (to! = null) {
Properties props = System.getproperties ();
props.put ("Mail.smtp.host", host);
props.put ("Mail.smtp.auth", "true");
mailauthenticator auth = new Mailauthenticator ();
mailauthenticator.username = user;
Mailauthenticator.password = PASSWORD;
Session session = Session.getinstance (props, auth);
Session.setdebug (true);
try {
mimemessage message = new MimeMessage (session);
Message.setfrom (New InternetAddress (from));
if (!to.trim (). Equals (""))
message.addrecipient (Message.RecipientType.TO,
New InternetAddress (To.trim ()));
Message.setsubject (subject);
MimeBodyPart MBP1 = new MimeBodyPart ();//Text
mbp1.setcontent (content, "Text/html;charset=utf-8");
Multipart MP = new Mimemultipart ();//Whole message: Body + attachments
Mp.addbodypart (MBP1);
//Mp.addbodypart (MBP2);
Message.setcontent (MP);
message.setsentdate (New Date ());
message.savechanges ();
Transport trans = Session.gettransport ("SMTP");
trans.send (message);
System.out.println (message.tostring ());
} catch (Exception e) {
e.printstacktrace ();
return "failure";
}
return "Success";
}else{
return "failure";
}
}
}
Step Three: Test
public static void Main (string[] args) {mailoperation operation = new Mailoperation (); String user = "Your e-mail address"; String Password = "Your mailbox Authorization Code"; String host = "smtp.163.com"; String from = "Your email address"; String to = "Target mailbox address";//Recipient String subject = "Enter message subject"; Mailbox Content StringBuffer SB = new StringBuffer (); String Yzm = randomutil.getrandomstring (6); Sb.append ("<! Doctype> "+" <div bgcolor= ' #f1fcfa ' style= ' border:1px solid #d9f4ee; font-size:14px; line-height:22px; Color: #005aa0;p adding-left:1px;padding-top:5px; padding-bottom:5px; ' ><span style= ' font-weight:bold; ' > Warm hint:</span> "+" <div style= ' width:950px;font-family:arial; ' > Welcome to Net Micro activity, your registration code is: <BR/><H2 style= ' color:green ' > +yzm+ ' Java implementation mailbox Send mail