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