Java利用stmp协议实现简单邮件发送功能
程序员文章站
2024-03-18 13:58:04
...
项目结构如下:
实现邮件发送的基本jar包:
Mail.java:
package com.wx.utils;
import java.io.IOException;
import java.io.PrintWriter;
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 javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* Description:邮件群发核心类
* @author Administrator
* @date 2017年11月19日下午6:09:25
*/
public class Mail extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1799582803909189350L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//设置字符编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//收件人
String name=request.getParameter("m-name");
//主题
String topic=request.getParameter("m-topic");
//正文
String content=request.getParameter("c-content");
//设置邮箱用户名
String username="aaa@qq.com";
//设置邮箱密码
String password="jyzydby960524";
//读取邮件发送协议
Properties props = new Properties();
//设置邮箱协议
props.put("mail.transport.protocol", "smtp");
//设置邮箱主机
props.put("mail.smtp.host", "smtp.163.com");
//设置邮箱验证
props.put("mail.smtp.auth", "true");
//创建一个邮箱会话
Session session = Session.getInstance(props);
MimeMessage message = new MimeMessage(session);
//ujqvaquelvzjhfch tlygmlxzcdwkgabc
//设置邮箱的来源
Address toAddress = new InternetAddress(username);
message.setFrom(toAddress);
//设置接收者
message.setRecipients(Message.RecipientType.TO, name);
//设置主题
message.setSubject(topic);
//设置内容
message.setText(content);
//设置保存
message.saveChanges();
//创建发送工具
Transport ts =session.getTransport();
//发送工具连接发送者
ts.connect(username, password);
//发送消息
ts.sendMessage(message, message.getAllRecipients());
ts.close();
PrintWriter pw =response.getWriter();
pw.print("发送成功");
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" encoding="utf-8">
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Java开发邮件群发系统</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
*{margin:0;}
body{background:url("${pageContext.request.contextPath}/images/bg.jpg");background-size:cover;}
/* body{background:url("https://ss0.bdstatic.com/l4oZeXSm1A5BphGlnYG/skin/483.jpg?2");background-size:cover;} */
/*start header*/
.header{width:100%;height:40px;margin-top:20px;}
.header .h-con{width:1000px;height:40px;margin:0 auto;}
.header .h-con span{font-size:20px;font-family:"微软雅黑";font-weight:60;float:right;line-height:40px;}
/*end header*/
/*start content*/
.content{width:770px;height:365px;margin:100px 0 0 150px;line-height:60px;}
.content input{width:400px;height:30px;}
.content textarea{width:400px;height:100px;}
.content .c-btn{width:400px;height:30px;margin-left:70px;border:none;outline:none;background:#2688ea;color:#fff;font-size:20px;font-weight:20px;border-radius:4px;}
.c-btn:hover{background:#53a8ee}
/*end content*/
</style>
</head>
<body>
<!--start header-->
<div class="header">
<div class="h-con">
<img src="${pageContext.request.contextPath}/images/logo.png" ><span>Java开发邮件群发系统-汪祥</span>
<!-- <img src="https://rescdn.qqmail.com/zh_CN/htmledition/images/webp/logo/logo_0_01f1927.png"><span>Java开发邮件群发系统-汪祥</span> -->
</div>
</div>
<!--end start-->
<!--start content-->
<form action="mail" method="post">
<div class="content">
<p>
<span>收件人:</span><input type="text" name="m-name">
</p>
<p>
<span>主 题:</span><input type="text" name="m-topic">
</p>
<p>
<span>正 文:</span>
<textarea name="c-content"></textarea>
</p>
<input type="submit" value="发送邮件" class="c-btn">
</div>
</form>
<!--end content-->
</body>
</html>
jsp代码中有两张图片,自己去找一下,在项目结构中可以看到
注意:这个项目只是简单实现了邮件发送的功能,这里有一个小问题:就是发送的邮件会被接收方当作垃圾邮件处理,所以要去垃圾邮件中找。