First step, import jar package, jar package [Http://pan.baidu.com/s/1kVRvGyF]
Official code:
First write a tool class:
Mailutil
ImportJavax.mail.*;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage;Importjava.util.Properties;/*** Mail Tool class*/ Public classMailutil {/*** Send mail *@paramto whom to send *@paramText Send content*/ Public Static voidSend_mail (String to,string text)throwsmessagingexception {//to create a connection object to connect to a mail serverProperties Properties =NewProperties (); //set basic parameters for sending messages//Send mail serverProperties.put ("Mail.smtp.host", "smtp.huic188.com"); //Send PortProperties.put ("Mail.smtp.port", "25"); Properties.put ("Mail.smtp.auth", "true"); //set up an account and password for sending mailSession session = Session.getinstance (properties,NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {//two parameters are the email account and password return NewPasswordauthentication ("[Email protected]", "Write the password for your account here"); } }); //Create a Mail objectMessage message =NewMimeMessage (session); //Set SenderMessage.setfrom (NewInternetAddress ("[Email protected]")); //Set RecipientMessage.setrecipient (Message.RecipientType.TO,Newinternetaddress (to)); //Set ThemeMessage.setsubject ("This is a test mail"); //Set message body The second parameter is the type of message sentMessage.setcontent (text, "Text/html;charset=utf-8"); //Send an e-mailtransport.send (message); }}
Test class:
TEST:
Importjavax.mail.MessagingException;/*** Test Class*/ Public classTest { Public Static voidMain (string[] args) {Try{Mailutil.send_mail ("[Email protected]", String.valueof (Math.random () * 999)); System.out.println ("Mail sent successfully!"); } Catch(messagingexception e) {e.printstacktrace (); } }}
Java implementation Mailbox Send verification code