Features: front-end to fill in certain information submitted to the background, the background information is saved to the database and related information send a message to the administrator
text:
1, Controller layer
@ResponseBody @RequestMapping (value = "Submit", Method = RequestMethod.POST) public the Result Submit (SomeInfo someInfo) { // SomeInfo: value of the transmission to the entity class the Result = Result new new the Result (); // Return value can define their own = TheService.submit the Result (someInfo); // saved to the database IF (result.isSuccess) { // save successfully, send e-mail Sendmail the send = new new Sendmail (someInfo); // start the thread, the thread will run method after start to send mail send.start (); } return result; }
2, Sendmail class
package com.bfweb.util; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import com.bfweb.view.SomeInfo; public class Sendmail the extends the Thread { Private String from = "[email protected]"; // used to send messages to the administrator mailbox Private String username = "xxxMailName"; // mail username Private String password = "password" ; // password of the mailbox Private String Host = "mail.xx.com"; // sending mail server address, ask operation and maintenance, I used our company private SomeInfo someInfo; public Sendmail(SomeInfo someInfo){ this.someInfo = someInfo; } / * Overriding implementations run method, send mail to a run method specified user * @see java.lang.Thread#run() */ public void run() { try{ Properties prop = new Properties(); prop.setProperty("mail.host", host); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.smtp.auth", "true"); Session session = Session.getInstance(prop); session.setDebug(true); Transport ts = session.getTransport(); ts.connect(host, username, password); Message message = createEmail(session,operationView); ts.sendMessage(message, message.getAllRecipients()); ts.close(); }catch (Exception e) { throw new RuntimeException(e); } } public Message createEmail(Session session, SomeInfo someInfo) throws Exception{ MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); //[email protected] message.setSubject(MimeUtility.encodeText("邮件提醒", MimeUtility.mimeCharset("gb2312"), null)); String info = "Code : %s<br>" + "Name : %s<br>" + "Gender : %s<br>" + "Email: %s<br>" + "Admin Url : <a href='%s'>%s</a><br>"; info = String.format(info, someInfo.getCode(), someInfo.getName(), someInfo.getGender(), someInfo.getEmail(), "https://www.baidu.com/", "点击进入后台"); message.setContent(info,"text/html;charset=UTF-8"); message.saveChanges(); return message; } }
Reference blog:
1, JavaWeb study concluded (52) - Use JavaMail to create and send e-mail messages - aloof Wolf - blog Park
https://www.cnblogs.com/xdp-gacl/p/4216311.html
2, java program send a message to the specified mailbox - v_yang_guang_v column - CSDN blog
https://blog.csdn.net/v_yang_guang_v/article/details/45001075
3, mail service return code Meaning - CSDN blog - weixin_34256074's blog
https://blog.csdn.net/weixin_34256074/article/details/86168513