This is the local mailbox server,
Code Execution conditions:
1. mailbox server, password S4XN
Mailbox Server Configuration:
1): Installation
2): Open server
The red part is the default account, no processing
3) System Settings "Click on the tool" Server Settings "
4) Create an account
2. Email client, can be downloaded to official website:
1. Installation of the mailbox client
Incoming and outgoing mail server: localhost
3. Mail.jar Bag
4.Util Tool Class
PackageCom.study.mail;Importjava.util.Properties;ImportJavax.mail.Authenticator;ImportJavax.mail.Message;Importjavax.mail.MessagingException;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.AddressException;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage;ImportJavax.mail.internet.MimeMessage.RecipientType; Public classmailutils {//Email:email address, subject mailbox subject, EMAILMSG mailbox information Public Static voidSendMail (String email,string Subject, string emailmsg)throwsaddressexception, messagingexception {//1. Create a program and mail server session object sessionsProperties Props=NewProperties (); Props.setproperty ("Mail.transport.protocol", "SMTP");//protocol for sending mailProps.setproperty ("Mail.host", "localhost");//server address for sending mailProps.setproperty ("Mail.smtp.auth", "true");//Specifies that validation is true//creating validatorsAuthenticator auth =NewAuthenticator () { Publicpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ("Tom", "123456");//e-mail account authentication } }; Session Session=session.getinstance (props, auth); //2. Create a message that corresponds to the content of the messageMessage message =NewMimeMessage (session); Message.setfrom (NewInternetAddress ("[email protected]");//Set Sendermessage.setrecipient (recipienttype.to,NewInternetAddress (email));//set the sending method and receiverMessage.setsubject (subject);//set the subject of the message//Message.settext ("This is an activation email, please <a href= ' # ' > Click </a>"); //set the contents of a messageMessage.setcontent (emailmsg, "Text/html;charset=utf-8"); //3. Create transport to send messagestransport.send (message); }}
Test class:
Package Com.study.mail; Import javax.mail.MessagingException; Import javax.mail.internet.AddressException; Public class sendmailtest { publicstaticvoidthrows Addressexception, messagingexception { mailutils.sendmail ("[email protected]", "Test Mail", "This is a test message");} }
Javaweb Mailbox Send (mailbox client configuration)