Java implementation mailbox Send mail

Source: Internet
Author: User

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: &LT;BR/&GT;&LT;H2 style= ' color:green ' > +yzm+ ' 

Java implementation mailbox Send mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.