package com.znsd.jsp.myschool.servlet;
import java.io.IOException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.znsd.jsp.myschool.util.Email;
import com.znsd.jsp.myschool.util.EmailHelper;
/**
* Servlet implementation class EmailServlet
*/
@WebServlet("/email")
public class EmailServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String from = request.getParameter("from");
String to = request.getParameter("to");
String title = request.getParameter("title");
String context = request.getParameter("context");
String msg = "";
if (from == null || "".equals(from)) {
msg = "发件人不能为空!";
}
if (to == null || "".equals(to)) {
msg = "收件人不能为空";
}
if ("".equals(msg)) {
try {
InternetAddress fromAddress = new InternetAddress(from);
// 收件人的处理
InternetAddress[] toAddress;
String[] array;
if (to.indexOf(",") > 0) {
// 如果有多个收件人,用,隔开。
array = to.split(",");
toAddress = new InternetAddress[array.length];
for (int i = 0; i < array.length; i++) {
toAddress[i] = new InternetAddress(array[i]);
}
} else {
// 如果只有一个人收件人,直接获取
toAddress = new InternetAddress[1];
toAddress[0] = new InternetAddress(to);
}
Email email = new Email(fromAddress, toAddress, title, context, "utf-8");
if(EmailHelper.sendEmail(email)) {
System.out.println("成功");
response.sendRedirect("sendemail.jsp");
}else {
request.setAttribute("msg", msg);
request.getRequestDispatcher("sendemail.jsp").forward(request, response);
}
} catch (AddressException e) {
e.printStackTrace();
}
}
}
}
package com.znsd.jsp.myschool.util;
import javax.mail.internet.InternetAddress;
public class Email {
public Email() {
}
public Email(InternetAddress from, InternetAddress[] addresses, String title, String context, String encode) {
this.from = from;
this.addresses = addresses;
this.title = title;
this.context = context;
this.encode = encode;
}
//发件人
private InternetAddress from;
//收件人
private InternetAddress[] addresses;
//标题
private String title;
//内容
private String context;
//编码格式
private String encode = "UTF-8";
public InternetAddress getFrom() {
return from;
}
public void setFrom(InternetAddress from) {
this.from = from;
}
public InternetAddress[] getAddresses() {
return addresses;
}
public void setAddresses(InternetAddress[] addresses) {
this.addresses = addresses;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getEncode() {
return encode;
}
public void setEncode(String encode) {
this.encode = encode;
}
}
package com.znsd.jsp.myschool.util;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* 发送邮件
*
*/
public class EmailHelper {
public static boolean sendEmail(Email email) {
Properties prop = System.getProperties();
// 设置SMTP服务器
prop.setProperty("mail.host", "smtp.qq.com");
// 声明发送邮件所使用的协议
prop.setProperty("mail.transport.protocol", "smtp");
// 开启邮箱安全验证
prop.setProperty("mail.smtp.auth", "true");
// 开启邮箱使用SSL加密协议
prop.setProperty("mail.smtp.ssl.enable", "true");
// 设置SMTP服务器端口号
prop.setProperty("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(prop, new QQAuthenticator());
session.setDebug(true); // 开启调试
// 邮件内容
// 创建Message对象
MimeMessage message = new MimeMessage(session);
try {
// 设置发件人
message.setFrom(email.getFrom());
// 设置收件人
message.setRecipients(Message.RecipientType.TO, email.getAddresses());
// 设置邮件标题
message.setSubject(email.getTitle());
// 设置邮件内容(发送文本)
// message.setText(email.getContext(), email.getEncode());
Multipart mp = new MimeMultipart();
BodyPart bp1 = new MimeBodyPart();
bp1.setContent(email.getContext(), "text/html;charset=utf-8");
BodyPart bp2 = new MimeBodyPart();
FileDataSource ds = new FileDataSource("D:\\上课代码\\jsp\\workspaces\\demo10\\WebContent\\uploads\\javamail1_4_7.zip");
bp2.setDataHandler(new DataHandler(ds));
bp2.setFileName("javamail1_4_7.zip");
mp.addBodyPart(bp1);
mp.addBodyPart(bp2);
message.setContent(mp);
message.saveChanges();
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
Transport transport;
try {
transport = session.getTransport();
// 连接到smtp服务器
transport.connect();
// 发送消息
transport.sendMessage(message, message.getAllRecipients());
// 关闭
transport.close();
return true;
} catch (NoSuchProviderException e) {
e.printStackTrace();
return false;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
// 收取邮件
public static void collectEmail() {
Properties prop = System.getProperties();
// 声明接收邮件所使用的协议
prop.setProperty("mail.store.protocol", "pop3");
// 设置POP3服务器
prop.setProperty("mail.pop3.host", "pop.qq.com");
// 开启邮箱安全验证
prop.setProperty("mail.pop3.auth", "true");
// 开启邮箱使用SSL加密协议
prop.setProperty("mail.pop3.ssl.enable", "true");
// 设置SMTP服务器端口号
prop.setProperty("mail.pop3.port", "995");
Session session = Session.getDefaultInstance(prop, new QQAuthenticator());
session.setDebug(true); // 开启调试
try {
// 连接pop3服务器
Store store = session.getStore();
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY); // 只读方式
Message message[] = folder.getMessages(); // 读取邮件
for (Message m : message) {
System.out.println(m.getFrom());
System.out.println(m.getSubject());
// System.out.println(m.getContent());
}
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
collectEmail();
}
}
package com.znsd.jsp.myschool.util;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class QQAuthenticator extends Authenticator{
//发送邮件
String username = "h123456789qw8@qq.com"; //配置发送邮件的用户名
String password = "nejvgsfnhzdkdjgj"; //配置发送邮件的密码
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
邮件发送
最新推荐文章于 2022-04-16 10:56:38 发布