Send mail via Java code

Source: Internet
Author: User

To send mail in Java, you must import a new package

mail.jar– Core pack of E-mails

activation.jar– encrypt the user and password.

There are three core classes in Mail.jar:

Javax.mail.session– refers to a session with a mail server. Only one on the whole project is available.

Javax.mail.Message (interface)-ready to send data information.

MimeMessage-You can set the type of data information.

transport– it has a way to send a message.

First step; import two jar packages

A second step; send a simple message

@Test Public voidSendMail ()throwsexception{//first step: Declaring the Properties object drop informationProperties prop =NewProperties (); //set which server to connect toProp.setproperty ("Mail.host", "smtp.126.com"); //set whether to validateProp.setproperty ("Mail.smtp.auth", "true"); //Step Two: Declare the user name and passwordAuthenticator auth =NewAuthenticator () {//This adapting returns the user and password objects             Publicpasswordauthentication getpasswordauthentication () {passwordauthentication PA=NewPasswordauthentication ("wj_leaf12345", "1QAZ2WSX"); returnPA;        }        }; ////Step Two: Get the Session objectSession session =session.getdefaultinstance (Prop,auth); //set debug mode for sessionSession.setdebug (true); //Step Three: declaring informationMimeMessage MM1 =NewMimeMessage (session); //Fourth Step: Set Sender emailAddress from =NewInternetAddress ("[Email protected]");        Mm1.setfrom (from); //Fifth Step: Set the recipientMm1.setrecipient (Recipienttype.to,NewInternetAddress ("[Email protected]")); Mm1.setrecipient (recipienttype.cc,NewInternetAddress ("[Email protected]")); Mm1.setrecipient (RECIPIENTTYPE.BCC,NewInternetAddress ("[Email protected]")); //Sixth step: Set the themeMm1.setsubject ("This is mail with Java 3"); Mm1.setcontent ("Hello, this is the mail sent with Java, 3333 try again", "Text/plain;charset=utf-8"); //Seventh Step:transport.send (MM1); }

Step three: Send a message with an extra connection

Mm1.setsubject ("This is a mail sfasdf3 sent with Java");        Mm1.setcontent ("Hello, this is the mail sent with Java, <a href= ' http://www.baidu.com ' > ' Wisdom </a>", "Text/html;charset=utf-8 " );                 // Seventh Step:        Transport.send (MM1);

Fourth step: Attachment of mail

 Public classSendattechmentdemo {@Test Public voidSendFile ()throwsexception{Properties P=NewProperties (); P.setproperty ("Mail.host", "smtp.163.com"); P.setproperty ("Mail.smtp.auth", "true"); Session s= Session.getdefaultinstance (P,NewAuthenticator () {@Override Publicpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ("wj_leaf12345", "1QAZ2WSX");        }        }); S.setdebug (true); //Statement MimeMessageMimeMessage msg =NewMimeMessage (s); Msg.setfrom (NewInternetAddress ("[Email protected]")); Msg.setrecipient (Recipienttype.to,NewInternetAddress ("[Email protected]")); Msg.setsubject ("Picture of"); //first step: Declaring a multi-processing partMimemultipart mm =NewMimemultipart (); //Step Two: DeclareMimeBodyPart body1 =NewMimeBodyPart (); //Step Three: Set up the character pieceDataSource ds =NewFiledatasource (NewFile ("./img/a.jpg"))); DataHandler DH=NewDataHandler (DS);        Body1.setdatahandler (DH); //You have to set the nameBody1.setfilename (Mimeutility.encodetext ("beauty. jpg"))); MimeBodyPart Body2=NewMimeBodyPart (); //Step Three: Set up the character pieceDataSource DS2 =NewFiledatasource (NewFile ("./img/b.jpg"))); DataHandler DH2=NewDataHandler (DS2);        Body2.setdatahandler (DH2); //You have to set the nameBody2.setfilename (Mimeutility.encodetext ("Beauty 2.jpg")); MimeBodyPart body3=NewMimeBodyPart (); //Step Three: Set up the character pieceDataSource DS3 =NewFiledatasource (NewFile ("./img/m.mp3"))); DataHandler DH3=NewDataHandler (DS3);        Body3.setdatahandler (DH3); //You have to set the nameBody3.setfilename (Mimeutility.encodetext ("The end of the century. mp3")); //add body1 to mmMm.addbodypart (body1);        Mm.addbodypart (BODY2);                Mm.addbodypart (BODY3);                Msg.setcontent (mm); //Sendtransport.send (msg); }}

Send mail via Java code

Related Article

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.