Java implementation Mailbox Send verification code

Source: Internet
Author: User

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

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.