We can send the message using a third-party e-mail server.
Mail Transfer Protocol commonly there are two: the POP3 / the SMTP, the IMAP / the SMTP .
The difference between POP and IMAP: operating-mail client, such as mobile e-mail, Mark Read, if you use POP, is not synchronized to the mailbox on the server; if you use IMAP, these operations will be synchronized to the mailbox on the server.
Jar requires two packets
- javax.mail.jar
- activation.jar
Examples of the use of QQ-mail server to send mail
Here use IMAP. Be in Settings -> Accounts -> POP3 / IMAP / SMTP / Exchange / CardDAV / CalDAV service configuration.
@WebServlet("/sendMailServlet") public class SendMailServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Properties properties = new Properties(); properties.put("mail.transport.protocol", "smtp");// 连接协议 properties.put("mail.smtp.host", "smtp.qq.com");// 邮箱服务器主机名 properties.put("mail.smtp.port", 465);// 端口号 properties.put("mail.smtp.auth", "true"); properties.put ( "mail.smtp.ssl.enable", "to true"); // whether to use a secure connection ssl properties.put ( "mail.debug", "to true"); // whether to display information in the console // Get session object the session the session = Session.getInstance (Properties); // Get message object the message message = new new the MimeMessage (the session); the try { // set the sender mail address message.setFrom ( new new the InternetAddress ( " XXX @ QQ .com " )); // set the recipient email address message.setRecipient (Message.RecipientType.TO, new newThe InternetAddress ( " [email protected] " )); // multiple recipients, written in the form of an array // the InternetAddress [] = {new new receiverArr the InternetAddress ( "[email protected]"), the InternetAddress new new ( "XXX qq.com @ "), new new InternetAddress (" [email protected] ")}; // message.setRecipients (Message.RecipientType.TO, receiverArr); // set the message header message.setSubject (" message header " ); // set the message content message.setText ( " mail content " ); // get the postman objects Transport Transport = Session.getTransport (); // connect to their email account, and the second parameter is the authorization code transport.connect("[email protected]", "xxxxxxxxxxx"); //发送邮件 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (MessagingException e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
Modify the red part to use.