Java: Sending mail

Source: Internet
Author: User

Java sends mail, there is monitoring need to send mail to OP notification very simple call directly

Sendmail2.sendmail ("[Email protected]", "alarm Information")


Package com.stevezong.sendmail;import java.io.ioexception;import java.util.properties;import  javax.mail.Authenticator;import javax.mail.BodyPart;import javax.mail.Message;import  javax.mail.multipart;import javax.mail.passwordauthentication;import javax.mail.session;import  javax.mail.transport;import javax.mail.message.recipienttype;import javax.mail.messagingexception; import javax.mail.internet.internetaddress;import javax.mail.internet.mimebodypart;import  javax.mail.internet.mimemessage;import javax.mail.internet.mimemultipart;import  javax.mail.internet.mimeutility;public class sendmail2 {private static final  string mail_host =  "smtp.exmail.qq.com";p Rivate static final int mail_ Timeout = 3000;private static final boolean mail_auth = true;private  static final string auth_user_address =  "Mailbox";p rivate static final string auth_user_password =  "email password"; private static final string auth_user_nickname =  "monitoring watchdog";//receiver  Receiver// mailinfo  mail Message public static void sendmail (string receiver, string mailinfo)  throws ioexception, messagingexception  {properties serverproperties =  new properties (); Serverproperties.put ("Mail.smtp.host",  mail_host); Serverproperties.put (" Mail.smtp.auth ",  mail_auth); Serverproperties.put (" Mail.smtp.timeout ",  mail_timeout); Authenticator authenticator = new authenticator ()  {@Overrideprotected   Passwordauthentication getpasswordauthentication ()  {return new passwordauthentication (AUTH _user_address, auth_user_password);}}; Session session = session.getinstance (Serverproperties, authenticator); message mailmsg = new mimemessage (session);//Mail header mailmsg.setsubject ("xxx exception"); Mailmsg.setfrom (new  InternetAddress (Mimeutility.encodetext (auth_user_nickname)  +  "<"  + auth_user_address  +  ">"); mailmsg.setrecipient (recipienttype.to, new internetaddress (receiver)); Mailmsg.setrecipient (recipienttype.cc, new internetaddress ("CC person mailbox")); Multipart mailcontent = new mimemultipart (); Bodypart contentpart = new mimebodypart (); Contentpart.settext (mailInfo+ "Time:" + System.currenttimemillis ()); Mailcontent.addbodypart (Contentpart); mailmsg.setcontent (mailContent); Transport.send (MAILMSG); System.out.println ("sendmailsucceed!!! ");}}




Java: Sending mail

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.