Java free email sending implementation

Source: Internet
Author: User

Java free email sending implementation

Now that the project has been going online for a while, I hope to send an email to relevant developers if an exception occurs in the project. I have found a large number of third-party interfaces (cost-effective) and email sending demos from the Internet. Finally, a jar package and a tool class are selected, without the configuration file. The specific tool class code is as follows:

Package com. 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. internet. addressException; import javax. mail. internet. internetAddress; import javax. mail. internet. mimeMessage; public class SendMail {public static void main (String [] args) {MessageVo mv = new MessageVo (); mv. setSubject ("sorry, your program has 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 = 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 ();} cat Ch (MessagingException e) {e. printStackTrace ();} System. out. println ("sent! ");}}

Note that:
1. The pop3/SMTP protocol is not activated in the sender's mailbox.
2. The number of emails sent by the sender is higher than 163, and 163 will prompt that the account has security threats and so on. You need to set authorization in the 163 mailbox.
3. The jar needed in the project is the mail-1.4.5.jar.

Directly import the project to eclipse and run the main method to complete the email sending.
Full Project (no points): http://download.csdn.net/download/zl544434558/9393226

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.