Overall effect:
Sendering end: NetEase Email; Receiving end: QQ Email.
1. Web front-end
2. You can see the email sent through the java code in NetEase Email "Sent"
3. You can also see this effect in the qq mailbox
Implementation process:
1. Web front-end (bootstrap layout)
<form action="mailAction!sendMail" method="post" name="mailForm" id="mailFormId"> <ul> <li> <div> <span id="basic-addon1">Name:</span> <input type="text" placeholder="your name" name="mailForm.name" aria-describedby="basic-addon1"> </div> </li> <li> <div> <span id="basic-addon2">Tel:</span> <input type="text" placeholder="your phone" name="mailForm.phone" aria-describedby="basic-addon1"> </div> </li> <li> <div> <span id="basic-addon2">Email:</span> <input type="text" placeholder="your e-mail" name="mailForm.e_mail" aria-describedby="basic-addon1"> </div> </li> <li style="padding-top: 20px;"> <span>Message:</span> <br><br> <textarea rows="10" name="mailForm.content" placeholder="Please enter a message (do not exceed 500 characters)"></textarea> </li> <li> <center><button onclick="$('#mailFormId').submit();" type="button">Send email</button></center> </li> </ul></form> 2. First prepare an XML template (<xml-body> contains text in html format for the email).
<?xml version="1.0" encoding="UTF-8"?><!-- Change spaces to full-width spaces to ensure that html will not filter out spaces--><xml-body> <html> <head> </head> <body style="margin: 0; padding: 0;"> <table cellpadding="0" cellpacing="0"> <tbody><tr> <td style="padding: 10px 0 30px 0;"> <table align="center" cellpadding="0" cellpacing="0" style="border: 1px solid #cccccc; border-collapse: collapse;"> <tbody><tr> <td align="center" bgcolor="#70bbd9" style="padding: 40px 0 30px 0; color: #153643; font-size: 28px; font-weight: bold; font-family: Arial, sans-serif;"> <img src="cid:{4}" style="display: block;"/> </td> </tr> <tr> <td bgcolor="#ffffffff" style="padding: 40px 30px 40px 30px;"> <table cellpadding="0" cellpacing="0"> <tbody><tr> <td style="color: #153643; font-family: Arial, sans-serif; font-size: 24px;"> <b> Sender: {0} <br/> Phone: {1} <br/> Email: {2} <br/> Content:<br/> </b> </td> </tr> <tr> <td style="padding: 20px 0 30px 0; color: #153643; font-family: Arial, sans-serif; font-size: 16px; line-height: 20px;"> {3} </td> </tr> <tr> <td> <table cellpadding="0" cellpacing="0"> <tbody><tr> <td valign="top"> <table cellpadding="0" cellpacing="0"> <tbody><tr> <td> <img src="cid:{5}" style="display: block;"/> </td> </tbody></table> </td> <td style="font-size: 0; line-height: 0;"> </td> <td valign="top"> <table cellpadding="0" cellpacing="0"> <tbody><tr> <td> <img src="cid:{6}" style="display: block;"/> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </td> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </td> </tr> <tr> <td bgcolor="#ee4c50" style="padding: 30px 30px 30px 30px;"> <table cellpadding="0" cellpacing="0"> <tbody><tr> <td align="right"> <table cellpadding="0" cellpacing="0"> <tbody><tr> <td style="font-family: Arial, sans-serif; font-size: 12px; font-weight: bold;"> <img src="cid:{7}" style="display: block;"/> </td> <td style="font-family: Arial, sans-serif; font-size: 12px; font-weight: bold;"> <img src="cid:{8}" style="display: block;"/> </td> </tbody></table> </td> </tbody></table> </td> </tr> </tbody></table> </body> </html></xml-body> There will be some identifiers in the template, such as {i}, table or reserved location for strings, and then format such messages through MessageFormat, and then insert the formatted string into the appropriate location in the pattern.
//Get the template of XML String XML_path = ServletActionContext.getServletContext().getRealPath("/mailTemplate")+"/myMailTemplete.xml"; String str=new ReaderXML().read(XML_path); Object[] obj=new Object[]{mailForm.getName(), mailForm.getPhone(), mailForm.getE_mail(), mailForm.getContent(), "e_mail", "left", "right", "tw", "fb"}; //MessageFormat can format such messages and then insert the formatted string into the appropriate position in the pattern String tcontent = MessageFormat.format(str, obj); Finally, the {i} in the XML template is replaced by obj[i] respectively.
3. Write a class ReaderXML for XML templates
public class ReaderXML { public String read(String path){ String str=null; str=reader(path); return str; } private String reader(String path){ SAXReader reader=new SAXReader(); String str=null; try { Document d=reader.read(new File(path)); Element e=d.getRootElement(); Element html=e.element("html"); str=html.asXML(); } catch (DocumentException e) { e.printStackTrace(); } return str; }} 4. Finally, our controller class MailAction
The image contained in the HTML format is to set the unique identifier of the corresponding resource file using the setContentID() method of the MimeBodyPart class. That is, the MIME protocol has the Content-ID header field in the structure and organization format of the message, which corresponds to the cid:{i} identifier in the XML template, such as <img src="cid:{8}"/> (Note: {i} will be replaced with the corresponding string through MessageFormat.format)
public class MailAction extends ActionSupport{ private MailForm mailForm; public MailForm getMailForm() { return mailForm; } public void setMailForm(MailForm mailForm) { this.mailForm = mailForm; } //Add an embedded image private MimeBodyPart createImageMimeBodyPart(String imageName) throws MessagingException, UnsupportedEncodingException{ FileDataSource fds=new FileDataSource(ServletActionContext.getServletContext().getRealPath("/image")+"/" + imageName + ".gif"); MimeBodyPart mbp=new MimeBodyPart(); DataHandler dh=new DataHandler(fds); mbp.setDataHandler(dh); //Set the unique identifier of the corresponding resource file, that is, the Content-ID header field in the structure and organization format of the MIME protocol for the message; mbp.setHeader("Content-ID", imageName); mbp.setFileName(MimeUtility.encodeText(fds.getName())); return mbp; } public String sendMail(){ try { HttpServletRequest request = ServletActionContext.getRequest(); String pwd = "******************";//Sender's email password String mailfrom = "***************@163.com"; //NetEase's email String wangyiFrom = mailfrom.substring(0, mailfrom.indexOf('@'));//NetEase Email Username String tu = "163.com"; //Sendor's email domain name String tto= "************@qq.com"; //The email address that receives the email String ttitle= "Someone contacts you---from Hu Junzheng's personal website"; //According to its physical path, get XML template String XML_path = ServletActionContext.getServletContext().getRealPath("/mailTemplate")+"/myMailTemplete.xml"; String str=new ReaderXML().read(XML_path); Object[] obj=new Object[]{mailForm.getName(), mailForm.getPhone(), mailForm.getE_mail(), mailForm.getContent(), "e_mail", "left", "right", "tw", "fb"}; //MessageFormat can format such messages and then insert the formatted string into the appropriate position in the pattern String tcontent = MessageFormat.format(str, obj); Properties props=new Properties(); props.put("mail.smtp.host","smtp."+tu);//Mailbox SMTP server address port props.put("mail.smtp.auth","true");//In this way, you can pass the verification Session s=Session.getInstance(props); s.setDebug(true); MimeMessage message=new MimeMessage(s); //Set the sender/recipe/send time for the message object InternetAddress from; from = new InternetAddress(mailfrom);//Send the sender's qq mailbox message.setFrom(from); InternetAddress to=new InternetAddress(tto);//Recipient's email message.setRecipient(Message.RecipientType.TO,to); message.setSubject(ttitle); message.setSentDate(new Date()); //Set content for message object BodyPart mbp=new MimeBodyPart();//Create a new BodyPart object mbp.setContent(tcontent,"text/html;charset=gb2312");//Set content and format/encoding method for BodyPart object// Used to combine text and pictures, "related" type MimeMultipart object Multipart mm=new MimeMultipart("related");// Create a new MimeMultipart object to store BodyPart objects (in fact, multiple) mm.addBodyPart(mbp);// Add BodyPart to the MimeMultipart object (multiple BodyPart can be added) // Add image mm.addBodyPart(createImageMimeBodyPart("e_mail")); mm.addBodyPart(createImageMimeBodyPart("left")); mm.addBodyPart(createImageMimeBodyPart("right")); mm.addBodyPart(createImageMimeBodyPart("right")); mm.addBodyPart(createImageMimeBodyPart("tw")); mm.addBodyPart(createImageMimeBodyPart("fb")); message.setContent(mm);//use mm as the content of the message object message.saveChanges(); Transport transport=s.getTransport("smtp"); transport.connect("smtp."+tu, wangyiFrom, pwd); //The wangyiFrom here is the sender's NetEase account transport.sendMessage(message, message.getAllRecipients()); transport.close(); ActionContext.getContext().getSession().put("operations", "The email was sent successfully, please wait patiently for the reply!"); } catch (Exception e) { System.out.println(e.toString()); ActionContext.getContext().getSession().put("errors", e.toString()); return "errors"; } return "sendMail"; }}The above is all about this article, I hope it will be helpful to everyone's learning.