SmtpJS

  • What is SmtpJS?
  • Enable the POP3/SMTP service
  • The import plug-in
  • Send E-mail
  • Encryption SMTP
  • Including attachments

What is SmtpJS?


SmtpJS is a plug-in that sends emails through front-end JS code. After importing the plug-in, it only takes a few lines of code to send emails. Of course, because it is the front-end, its security is naturally weaker than the backend Javamail package, but SmtpJS is very simple and convenient, powerful amway.

Enable the POP3/SMTP service


Before introducing the use of SmtpJS, we need to prepare an openPOP3 / SMTP serviceIf yes, skip this step.

Let’s take QQ mailbox as an example. After logging in to QQ mailbox, set -> account



Locate the POP3/SMTP service and click open



Complete the corresponding secret protection verification



Then obtain the corresponding authorization code.



So far your QQ mailbox opened POP3/SMTP service.

The import plug-in


Go to theSmtpJS websiteDownload the SMTP. Js

I uploaded it toogithub



You can use the official website if you don’t want to download it, but the former is recommended.

<script src="https://smtpjs.com/v3/smtp.js">
</script>
Copy the code

Send E-mail


Parameters that

parameter instructions
Host The SMTP address of your email address
Username Your email username (e.g[email protected])
Password Your email password (authorization code for previous service opening)
To Recipient email Address
From Sender email Address
Subject Email subject
Body Email content
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"% > <! DOCTYPE html PUBLIC"- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> Send mail test </title> <% pagecontext.setattribute ("APP_PATH",request.getContextPath());
%>
<script type="text/javascript" src="${APP_PATH}/static/js/smtp.js"></script>
</head>
<body>
<script type="text/javascript">
Email.send({
    Host : "smtp.yourisp.com",
    Username : "username",
    Password : "password",
    To : '[email protected]',
    From : "[email protected]",
    Subject : "This is the subject",
    Body : "And this is the body"
}).then(
  message => alert(message)
);
</script>
</body>
</html>
Copy the code



So far we have successfully sent the email! (. · ∀ ·) Blue

The port number and SMTP address of each mailbox are attached:

email Pop address SMTP address port
188 email pop3.188.com smtp.188.com 25
163 email pop3.163.com smtp.163.com 25
126 email pop3.126.com smtp.126.com 25
Netease mail pop.netease.com smtp.netease.com 25
Yeah your email pop.yeah.net smtp.yeah.net 25
QQ email pop.qq.com smtp.qq.com 465 or 587
Netease Enterprise Email pop3.163.com smtp.qiye.163.com 994
Tencent Enterprise Mailbox pop.qq.com smtp.exmail.qq.com 25

Encryption SMTP


Click Encrypt Your SMTP Credentials on the official website



Take QQ mailbox as an example, click Generate security token



A key is then generated



Replace it with your key

Email.send({
    SecureToken : "Key".To : '[email protected]'.From : "[email protected]".Subject : "This is the subject".Body : "And this is the body"
}).then(
  message= > alert(message)
);
Copy the code

Including attachments


That is, one more Attachments parameter

Email.send({
    SecureToken : "Key".To : '[email protected]'.From : "[email protected]".Subject : "This is the subject".Body : "And this is the body".Attachments: [{name : "smtpjs.png".path : "https://networkprogramming.files.wordpress.com/2017/11/smtpjs.png"
	}]
}).then(
  message= > alert(message)
);
Copy the code

Original is not easy, please do not reprint (this is not rich visits add insult to injury) blogger home page: blog.csdn.net/qq_45034708 If the article is helpful to you, remember to focus on the likes collection ❤