Now the project has been on the line for some time, and I want to be able to notify the relevant developers after an exception occurs in the project. I've been looking for a lot of third-party interfaces from the Internet (which costs money) and sending emails to demo. Finally, we chose an implementation that only needs to introduce a jar package and a tool class, no configuration files, and the following tool class code:
Packagecom. Evan. Mail;Import Java. Util. Date;Import Java. Util. Properties;Import Javax. Mail. Authenticator;Import Javax. Mail. Message;Import Javax. Mail. Messagingexception;Import Javax. Mail. Passwordauthentication;Import Javax. Mail. Session;Import Javax. Mail. Transport;Import Javax. Mail. The Internet. Addressexception;Import Javax. Mail. The Internet. InternetAddress;Import Javax. Mail. The Internet. MimeMessage;public class SendMail {public static void main (string[] args) {Messagevo mv = new Messagevo ();Mv. Setsubject("Unfortunately, your program is out of a bug."+new Date ());Mv. SetText("The specific content of the bug is:"+"Null pointer exception");Mv. Settomailaddress("[email protected]");SendMail (MV);} public static void SendMail (Messagevo mv) {Final String username ="[email protected]";Final String Password ="evan0202";Boolean Isssl = True;String host ="Smtp.163.com";int port =465;Boolean Isauth = True;String from ="[email protected]";Properties Props = new properties ();Props. Put("Mail.smtp.ssl.enable", Isssl);Props. Put("Mail.smtp.host", host);Props. Put("Mail.smtp.port", port);Props. Put("Mail.smtp.auth", Isauth);Session Session = Session. Getdefaultinstance(Props, new Authenticator () {@Override protected passwordauthentication getpasswordauthentication () {return new passwordauthentication (username, password);} });try {Message message = new MimeMessage (session);Message. Setfrom(New InternetAddress (from));Message. Setsubject(MV. Getsubject());Message. SetText(MV. GetText());Message. AddRecipient(Message. RecipientType. to, New InternetAddress (MV. Gettomailaddress()));Transport. Send(message);} catch (Addressexception e) {E. Printstacktrace();} catch (Messagingexception e) {E. Printstacktrace();} System. out. println("Sent over!" ");}}
It is important to note that:
1, the sender of the mailbox is not open POP3/SMTP protocol needs to be opened
2, the Sender 163 mail sent more times, 163 will be prompted to say that the account has security threats and so on, you need to set up the authorization in 163 mailbox
3. The jar to be used in the project is Mail-1.4.5.jar
Import the project directly into Eclipse and run the main method to complete the sending message
Complete project (free of credit): http://download.csdn.net/download/zl544434558/9393226
Java Free Send mail implementation