Java Email Send

Source: Internet
Author: User

Mail Service is the basic function of general system, this example uses 163 free mailbox send

1. Mail information Class (Mailsenderinfo)

Package Sendemail.email;import java.util.properties;/** * mail Message settings * @author Administrator * */public class Mailsenderinfo { Private String Mailserverhost; Send mail host private String mailserverport; Send Port private String fromaddress; Email Sender's address (mailbox) private String toaddress; Recipient's address (mailbox)//login mail send server user name and password private string userName;p rivate string passwrod;//Require authentication private Boolean validate = false;//message subject private string subject;//message contents private string content;//message Attachment private string[] attachfilenames;/* * Get mail Session properties * /public Properties GetProperties () {Properties P = new Properties ();p. put ("Mail.smtp.host", this.mailserverhost);// P.put ("Mail.smtp.port", This.mailserverport);p. Put ("Mail.sender.username", username);p. put ("Mail.sender.password ", Passwrod); return p;} Public String Getmailserverhost () {return mailserverhost;} public void Setmailserverhost (String mailserverhost) {this.mailserverhost = Mailserverhost;} Public String Getmailserverport () {return mailserverport;} public void Setmailserverport (STRing mailserverport) {this.mailserverport = Mailserverport;} Public String getfromaddress () {return fromaddress;} public void setfromaddress (String fromaddress) {this.fromaddress = fromaddress;} 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 Getpasswrod () {return passwrod;} public void Setpasswrod (String passwrod) {this.passwrod = Passwrod;} 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 getcontent () {return content;} public void SetContent (String content) {this.content = content;} Public string[] Getattachfilenames () {return attachfilenames;} public void Setattachfilenames (string[] attachfilenames) {This.attacHfilenames = Attachfilenames;}} 

2. Service class (Simplemailsender)

Package Sendemail.email;import Java.util.date;import Java.util.properties;import javax.mail.address;import    Javax.mail.message;import Javax.mail.session;import Javax.mail.BodyPart;    Import javax.mail.MessagingException;    Import Javax.mail.Multipart;    Import Javax.mail.Transport;    Import javax.mail.internet.InternetAddress;    Import Javax.mail.internet.MimeBodyPart;    Import Javax.mail.internet.MimeMessage;  Import Javax.mail.internet.MimeMultipart; /** * Send message format settings * @author Administrator * */public class Simplemailsender {/** * in (text) (textbox) Send mail * @param mailin FO message to be sent */public boolean sendtextmail (Mailsenderinfo mailinfo) {Properties pro = Mailinfo.getproperties ();   Session sendmailsession = session.getinstance (pro);          try {//Create a mail message based on session MailMessage = new MimeMessage (sendmailsession);          Create mail Sender address from = new InternetAddress (mailinfo.getfromaddress ()); 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 addr to = new InternetAddress (mailinfo.gettoaddress ());          Mailmessage.setrecipient (message.recipienttype.to,to);          Set the subject of the mail message Mailmessage.setsubject (Mailinfo.getsubject ());          Sets the time mailmessage.setsentdate the mail message is sent (new Date ());          Set the main content of the mail message String mailcontent = Mailinfo.getcontent ();          Mailmessage.settext (mailcontent);      Send mail mailmessage.savechanges ();      Transport Transport = Sendmailsession.gettransport ("SMTP");      Transport.connect (Mailinfo.getmailserverhost (), Mailinfo.getusername (), Mailinfo.getpasswrod ());         Transport.sendmessage (Mailmessage,mailmessage.getallrecipients ());                return true;          } catch (Messagingexception ex) {ex.printstacktrace ();   } return false;   /** * Send mail in HTML format * @param mailinfo message message to be sent */public static Boolean Sendhtmlmail (Mailsenderinfo mailinfo) {  Properties Pro = Mailinfo.getproperties ();     Constructs a session session Sendmailsession = Session.getdefaultinstance (pro) for sending mail based on mail session properties and Password verifier;   try {//Create a mail message based on session MailMessage = new MimeMessage (sendmailsession);   Create mail Sender address from = new InternetAddress (mailinfo.getfromaddress ());     Set the sender Mailmessage.setfrom (from) of the mail message;     Create the recipient address of the message, and set it to the mail message in addr to = new InternetAddress (mailinfo.gettoaddress ());     The Message.RecipientType.TO property indicates the recipient's type is to Mailmessage.setrecipient (message.recipienttype.to,to);     Set the subject of the mail message Mailmessage.setsubject (Mailinfo.getsubject ());     Sets the time mailmessage.setsentdate the mail message is sent (new Date ());     The Minimultipart class is a container class that contains objects of type MimeBodyPart Multipart Mainpart = new Mimemultipart ();     Create a mimebodypart bodypart html = new MimeBodyPart () that contains HTML content; Set HTML content html.setcontent (Mailinfo.getcontent (), "text/html;     Charset=utf-8 ");     Mainpart.addbodypart (HTML); Will MinimultipartThe object is set to the message content mailmessage.setcontent (Mainpart);     Send mail mailmessage.savechanges ();     Transport Transport = Sendmailsession.gettransport ("SMTP");     Transport.connect (Mailinfo.getmailserverhost (), Mailinfo.getusername (), Mailinfo.getpasswrod ());  Transport.sendmessage (Mailmessage,mailmessage.getallrecipients ());     return true;     } catch (Messagingexception ex) {ex.printstacktrace ();    } return false; }    }

3. Test Class (MailTest)

Package sendemail.email;/** * Test class * @author Administrator * */public class MailTest {public static void main (string[] args {//Set mail try {mailsenderinfo maininfo = new Mailsenderinfo (); Maininfo.setmailserverhost ("smtp.163.com"); Maininfo.setvalidate (True); Maininfo.setusername ("**************"); Maininfo.setpasswrod ("********"); Maininfo.setfromaddress ("********");    Maininfo.settoaddress ("********");    Maininfo.setsubject ("Lalalala"); Maininfo.setcontent ("----");   Simplemailsender.sendhtmlmail (maininfo);//Send stylistic format   } catch (Exception e) {e.printstacktrace ();}}}

Attention:

Maininfo.setpasswrod ("********"); is not the password to login 163 mailbox, is 163 mailbox authorization code.

Java Email Send

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.