Package COM. future. portal. util; import Java. io. ioexception; import Java. util. date; import Java. util. properties; import javax. mail. message; import javax. mail. messagingexception; import javax. mail. nosuchproviderexception; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. addressexception; import javax. mail. internet. internetaddress; import javax. mail. internet. mimemessage; import Org. apache. log4j. logger; public class mailsender {final static logger = logger. getlogger (mailsender. class);/*** send a simple email * @ Param str_from: sender address * @ Param str_to: Recipient address * @ Param str_title: email title * @ Param str_content: email body * @ Param emailpwd: email password */@ suppresswarnings ("finally") public static void send (string str_to, string str_title, string str_content) {Boolean flog = false; logger.info ("sending... to: "+ str_to +" mail title: "+ str_title +" fileattachment: "+ str_content); properties props = new properties (); classloader = thread. currentthread (). getcontextclassloader (); try {props. load (classloader. getresourceasstream ("mail. properties "); // use property to read the file} catch (ioexception e) {e. printstacktrace ();} string str_from = props. getproperty ("mail. from "); string emailpwd = props. getproperty ("mail. password ");
Final string mailserver_163 = props. getproperty ("mail. smtp.163.mailserver "); final string mailserver_qq = props. getproperty ("mail. SMTP. QQ. mailServer "); final string mailserver_sina = props. getproperty ("mail. SMTP. sina. mailServer ");
String MailServer = ""; string emailtype = tools. substring (str_from, "@"); // capture the string ename = tools. subberstring (str_from, "@") before ,"@");
If (emailtype. Trim ()! = "" & Emailtype. Equals ("QQ.com") {MailServer = mailserver_qq;} If (emailtype. Trim ()! = "" & Emailtype. Equals ("163.com") {MailServer = mailserver_163;} If (emailtype. Trim ()! = "" & Emailtype. Equals ("sina.com.cn") {MailServer = mailserver_sina;} If (emailtype. Trim ()! = "" & Emailtype. equals ("Sina.com") {MailServer = mailserver_sina;} // stores the information of the sending mail server props. put ("mail. SMTP. host ", MailServer); // verify the props at the same time. put ("mail. SMTP. auth "," true "); // create an email session according to the attributes. getinstance (props); S. setdebug (true); // some debugging information is printed. // Create a message object mimemessage message = new mimemessage (s) by the mail session; // set the email internetaddress from = NULL; try {from = new internetaddress (str_from );} catch (addressexception e) {// todo auto-generated Catch Block E. printstacktrace ();} // [email protected] Try {message. setfrom (from);} catch (messagingexception e) {// todo auto-generated Catch Block E. printstacktrace ();} // sets the sender's address // sets the recipient and its receiving Class Type: To internetaddress to = NULL; try {to = new internetaddress (str_to);} catch (addressexception e) {e. printstacktrace ();} Try {message. setrecipient (message. recipienttype. to, to); // set the title message. setsubject (str_title); // Java learning // set the mail content // message. settext (str_content); // send a text email // how are you? Message. setcontent (str_content, "text/html; charset = GBK"); // send an HTML email // set the sending time message. setsentdate (new date (); // stores the mail message. savechanges ();} catch (messagingexception e) {e. printstacktrace ();} // send the email transport Transport = NULL; try {transport = S. gettransport ("SMTP");} catch (nosuchproviderexception e) {// todo auto-generated Catch Block E. printstacktrace ();} // use SMTP to log on to your mailbox. The first parameter is the SMTP address of the email sending server, and the second parameter is the user name, the third parameter is the password try {transport. connect (MailServer, ename, emailpwd);} catch (messagingexception e) {e. printstacktrace ();} // send an email. The second parameter is the try {transport. sendmessage (message, message. getallrecipients ();} catch (messagingexception e) {e. printstacktrace ();} Try {transport. close ();} catch (messagingexception e) {e. printstacktrace () ;}/ *** method of sending mails asynchronously */public static void sendmailbyasynchronous (final string str_to, final string str_title, final string str_content) throws exception {New thread (New runnable () {public void run () {try {mailsender. send (str_to, str_title, str_content);} catch (exception ex) {logger. error ("mail sender error to:" + str_to + "mail title:" + str_title + "fileattachment:" + str_content, ex );}}}). start ();}}
mail.smtp.163.mailServer=smtp.163.commail.smtp.qq.mailServer=smtp.qq.commail.smtp.sina.mailServer=smtp.sina.com.cnmail.username=asdmail.password=123456mail.from[email protected]
Send mail. Jar via simple email