Javamail use 5-send mail 2-send HTML format mail

Source: Internet
Author: User

/**
*
* @ Param SMTP
* SMTP host address
* @ Param Port
* SMTP host address port
* @ Param user
* User Name
* @ Param Password
* Password
* @ Param from
* Sender
* @ Param
* Recipient
* @ Param CC
* CC
* @ Param BCC
* Give away
* @ Param subject
* Topic
* @ Param body
* Content
* @ Throws exception
*/
Public static void send (string SMTP, int port, string user,
String password, string from, string to, string CC, string BCC,
String subject, string body) throws exception {
// Variable Declaration
Properties props = new properties (); // System Properties
// Set system attributes
Props. Put ("mail. Transport. Protocol", "SMTP"); // use the SMTP protocol
Props. Put ("mail. SMTP. Auth", "true"); // whether email authentication is required

// Obtain the email Session Object
Session mailsession = session. getdefaultinstance (props, null );
Mailsession. setdebug (true );

// Create a mime mail object
Mimemessage mimemsg = new mimemessage (mailsession );

// Set the sender
Mimemsg. setfrom (New internetaddress (from ));
// Set the recipient
If (! = NULL ){
Mimemsg. setrecipients (message. recipienttype. To, internetaddress
. Parse ());
}

// Set CC recipients
If (CC! = NULL ){
Mimemsg. setrecipients (message. recipienttype. CC, internetaddress
. Parse (CC ));
}

// Set the hidden recipients
If (bcc! = NULL ){
Mimemsg. setrecipients (message. recipienttype. BCC, internetaddress
. Parse (BCC ));
}
// Set the Email Subject
Mimemsg. setsubject (subject );
// Set the email content and convert the body part to HTML Format
Mimemsg. setcontent (body, "text/html; charset = GBK ");

// Send an email
Transport transport = mailsession. gettransport ();
Transport. Connect (SMTP, port, user, password );
Transport. sendmessage (mimemsg, mimemsg. getallrecipients ());
Transport. Close ();
}

 

 

This is an HTML-format email. I simply encapsulate it into a method.
There are many parameters, some of which are optional.
Later, in order to reduce parameters during development, I packaged the parameters into a map,
This method is reloaded to make it easier for external development.

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.