Java Simple Send mail

Source: Internet
Author: User

The required jars are said to be:

<dependency>            <groupId>javax.mail</groupId>            <artifactId>mail</artifactId>            <version>1.4.1</version>        </dependency>

The introduced jar:

Import javax.mail.Address; Import Javax.mail.Authenticator; Import Javax.mail.Message.RecipientType; Import javax.mail.PasswordAuthentication; Import javax.mail.Session; Import Javax.mail.Transport; Import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;

The code sent:

 Public Static voidSendMail ()throwsexception{Authenticator Auth=NewAuthenticator () { Publicpasswordauthentication getpasswordauthentication () {passwordauthentication PA=NewPasswordauthentication ("username", "password"); returnPA;        }        }; Properties prop=NewProperties (); Prop.setproperty ("Mail.host", "smtp.126.com"); Prop.setproperty ("Mail.smtp.auth", "true"); Session Session=session.getdefaultinstance (Prop,auth); Session.setdebug (true); MimeMessage mm1=NewMimeMessage (session); Address from=NewInternetAddress ("Fromaddress");         Sender Mm1.setfrom (from); Mm1.setrecipient (Recipienttype.to,NewInternetAddress ("Toaddress")); Recipient Mm1.setsubject ("This is a message sent in Java 3"); Mm1.setcontent ("Hello, this is the mail sent with Java, 3333 try again", "Text/plain;charset=utf-8"); Mm1.setcontent ("Hello, this is the mail sent with Java, <a href= ' http://www.baidu.com ' > Baidu </a>", "Text/html;charset=utf-8" );     Transport.send (MM1); }

Reference article: http://www.jb51.net/article/80958.htm

Java Simple Send 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.