In this way, we have created a new test email account, and use Foxmail to receive emails here.
4. Install foxmail and configure the outgoing mail server:
5. Reference jar
6. Specific code implementation
Tool class for mail sending:
package cn.itcast.shop.utils;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
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;
/*
- mail sending tools
*/
public class MailUtils {
/*
-
Method of sending mail
-
@param to recipient
-
@param code activation code
*/
public static void sendMail(String to,String code){
/*
-
1. Obtain a Session object
-
2. Create an object Message representing the mail
-
3. Send mail transport
*/
//1. Get the connection object
Properties props=new Properties();
props.setProperty(“mail.host”, “localhost”);
Session session=Session.getInstance(props, new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(“[email protected]”,“1”);
}
});
//2, create mail object
Message message = new MimeMessage(session);
// Set sender:
try {
message.setFrom(new InternetAddress(“[email protected]”));
// Set recipients:
message.addRecipient(RecipientType.TO, new InternetAddress(to));
// CC CC BCC
// set title
message.setSubject("Activation email from North Pole");
// Set the email body:
message.setContent(“
Arctic activation email! Click the link below to complete the activation operation!
http://192.168.21.197:8088/shop/user_active.action?code=”+code+“
”, “text/html;charset=UTF-8”);// 3. Send email:
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catc "Analysis of Java interview questions in first-line manufacturers + back-end development study notes + latest architecture explanation video + practical project source code handouts" free open source prestige search official account [Programming Advanced Road] h (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
sendMail(“[email protected]”,“1111”);
}
}
Implement code in service