/**
*
* @ 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.