Send mail with Java program

Source: Internet
Author: User

Send a mailbox with no attachments:

Public void Test2 () throws exception{
//1: Claim server address
Properties P = new properties ();
p.setproperty ("Mail.host", "smtp.sohu.com");//Specify the server, take Sohu mailbox as an example
p.setproperty ("Mail.smtp.auth", "true");//Validation
Authenticator auth = new Authenticator () {
@Override
protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication ("xxxxxx", "password");
}
};
//2: Declare session
Session session = Session.getdefaultinstance (P,auth);
//Set to debug mode
Session.setdebug (true);
//3: Declaring information
Message msg = new MimeMessage (session);
Msg.setfrom (New internetaddress ("[email protected]");
msg.setrecipient (recipienttype.to,new internetaddress ("Email to be sent"));
Msg.setsubject ("title, who will");
msg.setcontent ("content, write Yourself", "text/html;charset=utf-8");
//Send
transport.send (msg);
}

To send a message containing an attachment:

Public void Test3 () throws exception{
//1: Claim server address
Properties P = new properties ();
p.setproperty ("Mail.host", "smtp.sohu.com");//Specify the server, take Sohu as an example
p.setproperty ("Mail.smtp.auth", "true");//Validation
Authenticator auth = new Authenticator () {
@Override
protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication ("xxxxxxx", "password");
}
};
//2: Declare session
Session session = Session.getdefaultinstance (P,auth);
//Set to debug mode
Session.setdebug (true);
//3: Declaring information
Message msg = new MimeMessage (session);
Msg.setfrom (New internetaddress ("[email protected]");
msg.setrecipient (recipienttype.to,new internetaddress ("Email to be sent"));
Msg.setsubject ("Everyone good 1111");

//Declaration multibody
Mimemultipart multi = new Mimemultipart ();
//Add an attachment
Javax.mail.internet.MimeBodyPart part1 = new Javax.mail.internet.MimeBodyPart ();
Part1.attachfile ("Address of File");
Part1.setfilename (Mimeutility.encodetext ("file name and suffix to send");
Multi.addbodypart (part1);
Javax.mail.internet.MimeBodyPart part2 = new Javax.mail.internet.MimeBodyPart ();
part2.setcontent ("Hello this is content <b>Hello</b>", "Text/html;charset=utf-8");

Multi.addbodypart (part2);

msg.setcontent (multi);


transport.send (msg);
}

Send mail with Java program

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.