Java mail notes

Source: Internet
Author: User
Tags mailmessage

Find the example on the Internet. Here is the original article link: http://www.cnblogs.com/liyazhou/archive/2010/08/20/18020.7.html.

But I always reported an error during the test. I found a lot of articles online and finally found a solution (unfortunately, I still don't understand the principle ).

Now, paste the code here to make a record. You can also refer to this issue.

In additionJava mailJar package, you also needJAF.

Also, some netizens shared their research experiences on Java mail. For details, visit http://www.cnblogs.com/qianru/archive/2010/10.html.

1. send an email


Mailsenderinfo

Import Java. util. properties; public class mailsenderinfo {// IP address and port of the server sending the mail private string mailserverhost; private string mailserverport = "587"; // Private string fromaddress; // The email recipient's address: Private string toaddress; // the username and password used to log on to the email sending server: Private string username; private string password; // whether authentication is required: Private Boolean validate = true; // mail subject private string subject; // mail text content private string content; // Mail Attachment File Name 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. SMTP. auth "," true "); 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 Boolean isvalidate () {return validate;} public void setvalidate (Boolean validate) {This. validate = validate;} Public String [] getattachfilenames () {return attachfilenames;} public void setattachfilenames (string [] filenames) {This. attachfilenames = filenames;} Public String getfromaddress () {return fromaddress;} public void setfromaddress (string fromaddress) {This. fromaddress = fromaddress;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} 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 getsubject () {return subject;} public void setsubject (string subject) {This. subject = subject;} Public String getcontent () {return content;} public void setcontent (string textcontent) {This. content = textcontent ;}}

Myauth

Import javax. mail. authenticator; import javax. mail. passwordauthentication; public class myauth extends authenticator {// be sure to inherit authenticatorstring username = NULL; string Password = NULL; Public myauth () {} public myauth (string username, string password) {This. username = username; this. password = password;} protected passwordauthentication getpasswordauthentication () {return New passwordauthentication (username, password );}}

Simplemailsender

Import Java. util. date; import Java. util. properties; import javax. mail. address; import javax. mail. bodypart; import javax. mail. message; import javax. mail. messagingexception; 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; public class simplemailsender {/*** send emails in text format * @ Param mailinfo information of the emails to be sent */Public Boolean sendtextmail (mailsenderinfo mailinfo) {// determine whether identity authentication is required. myauth auth = NULL; properties pro = mailinfo. getproperties (); If (mailinfo. isvalidate () {// If identity authentication is required, create a password authenticator auth = new myauth (mailinfo. getUserName (), mailinfo. getPassword ();} // construct a mail sending session sendmailsession = session based on the mail session attribute and password validators. getdefaultinstance (Pro, auth); try {// create an email message mailmessage = new mimemessage (sendmailsession) based on the session; // create the email sender address from = new internetaddress (mailinfo. getfromaddress (); // sets the sender of the email message mailmessage. setfrom (from); // create the recipient address of the email and set it to address to = new internetaddress (mailinfo. gettoaddress (); mailmessage. setrecipient (message. recipienttype. to, to); // set the subject of the email message mailmessage. setsubject (mailinfo. getsubject (); // set the mail message sending time mailmessage. setsentdate (new date (); // you can specify string mailcontent = mailinfo. getcontent (); mailmessage. settext (mailcontent); // send the email transport. send (mailmessage); Return true;} catch (messagingexception ex) {ex. printstacktrace ();} return false;}/*** send an email in HTML format * @ Param mailinfo message to be sent */Public Boolean sendhtmlmail (mailsenderinfo mailinfo) {// determine whether identity authentication is required. myauth authenticator = NULL; properties pro = mailinfo. getproperties (); // If identity authentication is required, create a password validator if (mailinfo. isvalidate () {authenticator = new myauth (mailinfo. getUserName (), mailinfo. getPassword ();} // construct a mail sending session sendmailsession = session based on the mail session attribute and password validators. getdefaultinstance (Pro, Authenticator); try {// create an email message mailmessage = new mimemessage (sendmailsession) based on the session; // create the mail sender address from = new internetaddress (mailinfo. getfromaddress (); // sets the sender of the email message mailmessage. setfrom (from); // create the recipient address of the email and set it to address to = new internetaddress (mailinfo. gettoaddress (); // message. recipienttype. the to attribute indicates that the receiver type is to mailmessage. setrecipient (message. recipienttype. to, to); // set the subject of the email message mailmessage. setsubject (mailinfo. getsubject (); // set the mail message sending time mailmessage. setsentdate (new date (); // The minimultipart class is a container class that contains the multipart mainpart = new mimemultipart (); // create a mimebodypart bodypart HTML containing HTML content = new mimebodypart (); // set the HTML content in HTML. setcontent (mailinfo. getcontent (), "text/html; charset = UTF-8"); mainpart. addbodypart (HTML); // set the minimultipart object to mailmessage. setcontent (mainpart); // send the email transport. send (mailmessage); Return true;} catch (messagingexception ex) {ex. printstacktrace () ;}return false ;}}

Mailtest

Public class mailtest {public static void main (string [] ARGs) {mailsenderinfo mailinfo = new mailsenderinfo (); mailinfo. setmailserverhost ("smtp.qq.com"); mailinfo. setmailserverport ("25"); // The common value is "25", and the SSL value is "465" or "587". You can refer to mailinfo in mailbox settings. setusername ("xxx@qq.com"); mailinfo. setpassword ("XXX"); // username and password, which are actually used to log on to mailinfo. setfromaddress ("xxx@qq.com"); // sender, that is, the preceding login name mailinfo. settoaddress ("yyy@163.com"); // recipient Mai Linfo. setsubject ("My work mail test"); // The title mailinfo. setcontent ("Hello, \ n \ t this is a test and I hope everything works properly. "); // Content // This class mainly sends the email simplemailsender SMS = new simplemailsender (); SMS. sendtextmail (mailinfo); // sending style format // SMS. sendhtmlmail (mailinfo); // send HTML format }}

2,

3,

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.