Mail tool class: (the case is Netease 163 mailbox)
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.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");
props.setProperty("mail.host", "smtp.163.com"); //The mailbox is different, the format is different
props.setProperty("mail. smtp.auth", "true");// Specify the authentication as true
// Create an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("leis17", "a123456");// Account authorization code (authorization code should be set in Netease mailbox)
}
};
Session session = Session.getInstance(props, auth);
// 2. Create a Message, which is equivalent to the content of the email
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]")); // set sender
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // Set the sending method and recipient
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 a Transport for sending mail
Transport.send(message) ;
}
}
jsp page
<form class="form-horizontal" style="margin-top: 5px;" method="post" action="/17Store/register1">
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">邮箱</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="email"
placeholder="请输入163邮箱" name="email">
</div>
</div>
</form>
Servlet
public class register1Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String remail = request.getParameter("email"); //Get the input email address
session .setAttribute("email", remail);
// System.out.println("phone:"+request.getParameter("telephone"));
String emailCode = null;// 6-digit verification code
emailCode = "122222";
RequestDispatcher dispatcher = request.getRequestDispatcher("/getCode.jsp");// Because it is under the same web application, there is no need to write /WEB_request/servlet2
// The method of executing forwarding
try {
MailUtils.sendMail(remail, emailCode);//Send the message to the specified mailbox
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Error report and its solution:
163 mailbox open pop3/smtp and other protocols, and then use the authorization code instead of the password to simulate the login, and the sending can be successful!