使用網頁實現qq郵箱傳送
阿新 • • 發佈:2022-01-28
匯入依賴
<groupId>com.zou</groupId>
<artifactId>WebMail</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
建立實體類傳送
package com.zou.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private String username;
private String password;
private String email;
}
建立工具類傳送郵件
package com.zou.util;
import com.zou.pojo.User;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class Sendmail extends Thread{
private String from ="[email protected]";
private String username ="[email protected]";
private String password ="udgyxytarwlgecae";
private String host="smtp.qq.com";
private User user;
public Sendmail(User user) {
this.user = user;
}
@Override
public void run() {
try {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com");//設定請求郵件伺服器
prop.setProperty("mail.transport.protocol", "smtp");//郵件傳送協議
prop.setProperty("mail.smtp.auth", "true");//需要驗證使用者名稱密碼
//關於qq郵箱還要設定ssl加密,加上以下程式碼
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(prop, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "utrwlgecae");
}
});
session.setDebug(true);
//通過session得到transport物件
Transport transport = session.getTransport();
transport.connect("smtp.qq.com", "[email protected]", "uytrwlgecae");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setSubject("使用者註冊事件");
String info="恭喜您註冊成功,您的使用者名稱:"+user.getUsername()+",您的密碼;"+user.getPassword()+",請妥善你保管,如有問題請聯絡客服";
message.setContent(info,"text/html;charset=UTF-8");
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
建立jsp主頁
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>註冊</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/RegisterServlet.do" method="post">
使用者名稱:<input type="text" name="username"><br/>
密碼:<input type="password" name="password"><br/>
郵箱:<input type="text" name="email"><br/>
<input type="submit" value="註冊">
</form>
</body>
</html>
</body>
</html>
傳送後跳轉jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>
網站提示
</h1>
${message}
</body>
</html>
跳轉內容
package com.zou.servlet;
import com.zou.pojo.User;
import com.zou.util.Sendmail;
import javax.servlet.ServletException;
import java.io.IOException;
public class RegisterServlet extends javax.servlet.http.HttpServlet{
protected void doPost(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response)throws ServletException,IOException {
String username =request.getParameter("username");
String password =request.getParameter("password");
String email =request.getParameter("email");
User user = new User(username,password,email);
Sendmail send=new Sendmail(user);
send.start();
request.setAttribute("message","註冊成功,我們已經發了一封電子郵件,如網路不穩定,可能過一會才能收到!!");
request.getRequestDispatcher("info.jsp").forward(request,response);
}
protected void doGet(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response)throws ServletException,IOException{
}
}