Send a simple email to JavaMail.

Source: Internet
Author: User

Send a simple email to JavaMail.

It is very simple to send emails. There are a lot of emails on the Internet, but write your own records.

Package cn. jmail. test; import java. util. properties; import javax. mail. *; import javax. mail. internet. *; public class FirstMail {/*** simple email sending Method * @ param host IP address of the email sending server * @ param from sender address * @ param to recipient address * @ param subject topic * @ param text content * @ param senderUsername sender's account * @ param senderPassword sender's password * mail. smtp. whether auth requires authentication is generally required */public static void sendMail (String host, String from, String to, String subject, String text, final String senderUsername, final String senderPassword) {Properties props = System. getProperties (); props. put ("mail. smtp. host ", host); props. put ("mail. smtp. auth "," true "); Session session = Session. getDefaultInstance (props, new Authenticator () {@ Override public PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (senderUsername, senderPassword );}}); try {MimeMessage message = new MimeMessage (session); message. setFrom (new InternetAddress (from); message. addRecipient (Message. recipientType. TO, new InternetAddress (to); message. setSubject (subject); message. setText (text); Transport. send (message);} catch (AddressException e) {e. printStackTrace ();} catch (MessagingException e) {e. printStackTrace () ;}} public static void main (String [] args) {String host = "smtp.163.com"; String from = "xxxxxx@163.com"; String to = "xxxxxxxx@qq.com "; string subject = "Hello, this is a test email. "; String text =" Hello, LiLei. "; String senderUsername =" xxxxxx@163.com "; String senderPassword =" xxxxxx "; FirstMail. sendMail (host, from, to, subject, text, senderUsername, senderPassword );}}

Well, of course we won't miss the http://pan.baidu.com/s/1qW7LZvm.

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.