Recommended not to use QQ mailbox, I use QQ mailbox directly to my report 530 error, I always think that my code write error or POP3/SMTP service is not ready. So it is recommended to register an individual mailbox, I have applied for a NetEase 163 mailbox instant good. So go and apply for an individual email.
To send a verification code by mail, you first need a jar package. I write the code only need Mail.jar, no words can give me a message, I send you a.
That's nonsense. directly on the code (this code is a tool class directly with the good)
Package Com.itheima.utils;import Java.util.properties;import Javax.mail.authenticator;import javax.mail.Message; Import Javax.mail.messagingexception;import Javax.mail.passwordauthentication;import Javax.mail.Session;import Javax.mail.transport;import Javax.mail.internet.addressexception;import javax.mail.internet.InternetAddress; Import Javax.mail.internet.mimemessage;import Javax.mail.internet.mimemessage.recipienttype;public class MailUtils {public static void SendMail (string email, string emailmsg) throws Addressexception, messagingexception {//1. Create a program and mail server session object sessions//create parameter configuration, parameter configuration for connecting mail server properties props = new properties (); Parameter Configuration props.setproperty ("Mail.transport.protocol", "SMTP"),//protocol used (JavaMail specification required) Props.setproperty ("Mail . Host "," smtp.163.com ");////Sender's mailbox SMTP server address Props.setproperty (" Mail.smtp.auth "," true ");//request authentication, parameter name and specific implementation of the specified inspection Certificate is true//create authenticator Authenticator auth = new Authenticator () {@OVerride public passwordauthentication getpasswordauthentication () {//NetEase mailbox Yang_li_g user name hao123 Grant Password, change to your own account number and passwords return to new Passwordauthentication ("Yang_li_g", "hao123"); } }; Session session = Session.getinstance (props, auth); 2. Create a message that is equivalent to the message content message message = new MimeMessage (session); Here also to change and the above correspondence, notice suffix and above set the same as otherwise will error Message.setfrom (new InternetAddress ("[email protected]")); Set Sender Message.setrecipient (recipienttype.to, new internetaddress (email)); Set the sending method with the receiver Message.setsubject ("user activation"); Message.settext ("This is an activation email, please <a href= ' # ' > Click </a>"); Message.setcontent (emailmsg, "text/html;charset=utf-8"); 3. Create Transport to send mail transport.send (message); }} Here is the code for the servlet, the response code is//Send activation message Activecode: Is the captcha String emailmsg = "Congratulations on your successful registration, please click on the link below to activate the account" + "<a href = ' http://localhost:8080/Shop/active?activeCode= ' + Activecode + ">" + "http://localhost:8080/Shop/active?activeCode=" + Activecode + "</a>"; try {//user.getemail (): is the registered person's mail user name Mailutils.sendmail (user.getemail (), emailmsg); } catch (Messagingexception e) {e.printstacktrace (); }
NetEase Mailbox to open POP3/SMTP service steps
First click settings -"pop3/smtp/imap---" to find the Open option click on ---"Open the password after the successful
I'm setting it up so you can look at it. After the setup is ready, you can implement the verification code with the mail.
Java implementation message Send verification code