In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use JavaEE to achieve mail sending function based on SMTP protocol", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use JavaEE to achieve mail sending function based on SMTP protocol" this article.
First, we need to understand the SMTP protocol and SSL encryption.
SMTP: known as simple Mail transfer Protocol (Simple Mail Transfer Protocal), the goal is to provide efficient and reliable mail delivery to users. SMTP is a protocol for request and response, that is, the client sends a request to the remote server, and the server responds. The listening port is 25, so it has two working modes: send SMTP and receive SMTP.
SSL encryption: used to ensure the security of browsers and web servers. The principle is: when your browser requests a secure web page (usually https://)) from the server
The server sends back its certificate and public key.
The browser checks that the certificate is issued by a trusted authority and confirms that the certificate is valid and that the certificate belongs to this site.
A random symmetric key is encrypted with the public key, including the encrypted URL sent to the server
The server decrypted the key you sent with its own private key. Then use this symmetric encryption key to decrypt your requested URL link.
The server encrypts the requested web page with the symmetric key you sent. If you have the same key, you can decrypt the web page sent back.
Then introduce how to implement javamail to send mail, first download the jar of javamail
QQ Mail, who acts as a server, starts the SMTP service:
Write a business class to send mail:
Package com.appms.email;import java.util.Date;import java.util.Properties;import javax.mail.Address;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import com.sun.mail.util.MailSSLSocketFactory;public class JavaEmailSender {public static void sendEmail (String toEmailAddress,String emailTitle,String emailContent) throws Exception {Properties props = new Properties (); / / enable debug debugging props.setProperty ("mail.debug", "true") / / the sending server requires authentication props.setProperty ("mail.smtp.auth", "true"); / / sets the mail server hostname props.setProperty ("mail.host", "smtp.qq.com"); / / sends the mail protocol name props.setProperty ("mail.transport.protocol", "smtp") / * * SSL authentication. Note that Tencent email is encrypted based on SSL, so you can use * * / MailSSLSocketFactory sf = new MailSSLSocketFactory (); sf.setTrustAllHosts (true); props.put ("mail.smtp.ssl.enable", "true"); props.put ("mail.smtp.ssl.socketFactory", sf); / / create session Session session = Session.getInstance (props) / / messages sent, Message msg = new MimeMessage (session) based on Observer pattern; msg.setSubject (emailTitle); / / use StringBuilder because StringBuilder loads faster than String and thread safety is good StringBuilder builder = new StringBuilder (); builder.append ("\ n" + emailContent); builder.append ("\ ntime" + new Date ()); msg.setText (builder.toString ()); msg.setFrom (new InternetAddress ("your QQ Mail")) Transport transport = session.getTransport (); transport.connect ("smtp.qq.com", "your QQ Mail", "your independent password for opening SMTP service application"); / / send message transport.sendMessage (msg, new Address [] {new InternetAddress (toEmailAddress)}); transport.close ();}}
Then write a Controller class of the SpringMVC framework:
/ * Jump to the outgoing email page * @ return * @ throws Exception * / @ RequestMapping ("/ goSendEmail") public ModelAndView goSendEmail (HttpServletRequest request) throws Exception {ModelAndView mv = this.getModelAndView (); String email = request.getParameter ("email"); if (emailments invalid nullmail messages! ".equals (email)) {email = email.trim (); mv.setViewName (" member/send_email "); mv.addObject (" email ", email);} return mv } / * send email * @ return * @ throws Exception * / @ RequestMapping (value= "/ sendEmail", produces= "application/json;charset=UTF-8") @ ResponseBody public Object sendEmail (HttpServletRequest request) throws Exception {Map map = new HashMap (); String msg = "ok"; / / send status String toEMAIL = request.getParameter ("EMAIL"); / / String TITLE = request.getParameter ("TITLE"); / / title String CONTENT = request.getParameter ("CONTENT") / / content JavaEmailSender.sendEmail (toEMAIL, TITLE, CONTENT); map.put ("result", msg); return map;}
The Jquery TIP plug-in is used for verification prompts, so you need to introduce the corresponding Jquery file
Jquery form validation and Ajax asynchronous requests:
/ send function sendEm () {if ($("# TYPE"). Val () = "1") {$("# CONTENT") .val (getContentTxt ());} else {$("# CONTENT") .val (getContent ());} if ($("# EMAIL"). Val () = ") {$(" # EMAIL "). Tips ({side:3, msg:', please enter mailbox', bg:'#AE81FF', time:2}); $(" # EMAIL "). Focus () Return false;} if ($("# TITLE"). Val () = "") {$("# TITLE") .tips ({side:3, msg:', please enter the title', bg:'#AE81FF', time:2}); $("# TITLE") .focus (); return false } if ($("# CONTENT"). Val () = ") {$(" # nr "). Tips ({side:1, msg:', please enter', bg:'#AE81FF', time:3}); return false;} var EMAIL = $(" # EMAIL "). Val (); var TYPE = $(" # TYPE "). Val (); var TITLE = $(" # TITLE "). Val (); var CONTENT = $(" # CONTENT "). Val () $("# zhongxin"). Hide (); $("# zhongxin2"). Show () $.ajax ({type: "POST", url: 'retroaction/sendEmail.do?tm='+new Date (). GetTime (), data: {EMAIL:EMAIL,TITLE:TITLE,CONTENT:CONTENT}, dataType:'json', / / beforeSend: validateData, cache: false, success: function (data) {if ("ok" = = data.result) {$("# msg"). Tips ({side:3, msg:' sent successfully!' , bg:'#68B500', time:5}); setTimeout ("showp ()", 1000);} else {$("# msg"). Tips ({side:3, msg:' failed to send!', bg:'#68B500', time:5});});}
The call to the JSP page:
${email}
Send
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.