第一步,導入依賴
發送郵件所需要的依賴有
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
頁面包括 郵箱 驗證碼 接收人的郵箱 發送的內容 提交按鈕
<form action="${pageContext.request.contextPath}/test" method="post">
<p> 自己的郵箱: <input type="email" name="username" value="786555160@qq.com"></p>
<p> 驗證碼 : <input type="password" name="psw" value="emzspejrljnvbfad"></p>
<p>發給誰: <input type="email" name="touser"/> </p>
<p style="color: red">發送內容:</p>
<textarea name="textarea" rows="20" cols="100" style="font-size: 30px"></textarea>
<p><input type="submit" value="確定"></p>
</form>
第三步,接收頁面發送過來的信息
用servlet中req接收
String uesrname = req.getParameter("username");
String psw = req.getParameter("psw");
String touser = req.getParameter("touser");
String textarea = req.getParameter("textarea");
第四步,創建一個發郵件的方法
public static void t1(String username,String passwprd,String touser,String textarea) throws Exception{
Properties properties = new Properties();
properties.setProperty("mail.host","smtp.qq.com");//設置qq郵件服務器
properties.setProperty("mail.transport.protocol","smtp");//郵件發送協議
properties.setProperty("mail.smtp.auth","true");//需要驗證用戶名密碼
//qq郵箱還要ssl加密
MailSSLSocketFactory sf=new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp,ssl.enable","true");
properties.put("mail.smtp,ssl.socketFactory",sf);
Session session= Session.getDefaultInstance(properties, new Authenticator() {
第五步,調用方法並轉發頁面
try {
test01.t1(uesrname,psw,touser,textarea);
} catch (Exception e) {
e.printStackTrace();
}
resp.sendRedirect("/test_war/success.jsp");
自動發送郵件(線程)
常用在注冊后發送短信
第一步,寫一個簡單的寫郵箱的text框
<form action="${pageContext.request.contextPath}/test02" method="get">
<input type="text" name="mailPath">
<input type="submit">
第二步,獲取框里的信息
String mailpath = req.getParameter("mailPath");
第三步,序列化信息
public class User {
private String mailPath;
第四步,創線程,獲取發送郵件路徑
public class sendmail extends Thread {
private User user;
public sendmail(User user){
this.user=user;
}
第五步,執行線程,定向頁面
User user = new User(mailpath);
sendmail sendmail = new sendmail(user);
sendmail.start();
resp.sendRedirect("/test_war/success.jsp");
帶有附件/圖片的郵件
把 message.setContent("你好~","text/html;charset=UTF-8");
變為
//圖片
MimeBodyPart body1 = new MimeBodyPart();
body1.setDataHandler(new DataHandler(new FileDataSource(" ")));
body1.setContentID("lt.png");
//文本
MimeBodyPart body2 = new MimeBodyPart();
body2.setContent("*************<img src='cid:lt.png' ","text/html;charset=utf-8");
//附件
MimeBodyPart body3 = new MimeBodyPart();
body3.setDataHandler(new DataHandler(new FileDataSource(" ")));
body3.setFileName("lt1.properties");
//拼裝
MimeMultipart mimeMultipart =new MimeMultipart();
mimeMultipart.addBodyPart(body1);
mimeMultipart.addBodyPart(body2);
mimeMultipart.setSubType("related");//文本和圖片內嵌成功
//將拼裝好的正文設置為主體
MimeBodyPart contentText = new MimeBodyPart();
contentText.setContent(mimeMultipart);
//拼接附件
MimeMultipart allFile = new MimeMultipart();
allFile.addBodyPart(body3);
allFile.addBodyPart(contentText);
allFile.setSubType("mixed");
message.setContent(allFile);
message.saveChanges();
