package org.fuckj.email;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.AuthenticationFailedException;
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;
public class SendEmailServlet extends HttpServlet {
public SendEmailServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String to = request.getParameter("to");
String from = request.getParameter("from");
String password = request.getParameter("password");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
if ("".equals(to)) {
out.println("未填写<h2>收件人</h2>邮箱地址.");
} else if (!isMail(to)) {
out.println("收件人邮箱格式不正确.");
} else if ("".equals(from)) {
out.println("未填写<h2>发件人</h2>邮箱地址.");
} else if (!isMail(from)) {
out.println("发件人邮箱格式不正确.");
} else if ("".equals(password)) {
out.println("发件人邮箱密码未填写.");
} else if ("".equals(subject)) {
out.println("请填写邮箱标题.");
} else {
String beforeAt = from.substring(0, from.indexOf("@"));
String afterAt = from.substring(from.indexOf("@") + 1, from
.length());
String smtp = "smtp." + afterAt;
System.out.println(smtp);
try {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
Session sendMailSession = Session.getInstance(props, null);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
newMessage.setText(message);
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(smtp, beforeAt, password);
newMessage.saveChanges();
transport
.sendMessage(newMessage, newMessage.getAllRecipients());
transport.close();
out.println("您的邮件已发送成功。");
} catch (AuthenticationFailedException ex) {
out.println("发件人邮箱认证错误.");
} catch (Exception ex) {
// ex.printStackTrace();
out.println("请确认发件人及收件人邮箱地址是否真实.");
}
}
out.flush();
out.close();
}
private boolean isMail(String str) {
String check = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(str);
boolean isMatched = matcher.matches();
if (isMatched) {
return true;
} else {
return false;
}
}
public void init() throws ServletException {
}
public static void main(String[] args) {
System.out.println(new SendEmailServlet().isMail("mysoft_ok@126.com"));
}
}
评论1
最新资源