package com.hans.email;
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;
public class Email extends HttpServlet{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
try {
//request.setCharacterEncoding("utf-8");//设置字符编码
//response.setCharacterEncoding("utf-8");
//System.out.println(6);
//接收前端数据
String name="wuyunhuiv587@163.com,3385351592@qq.com,2673629310@qq.com,184867747@qq.com";
String top="你好";
String tile="最近过的怎么,有";
String username="2694072078@qq.com";
String pass="bmmhpqtkvilhdeca";
//bmmhpqtkvilhdeca
//邮箱发送
Properties pr=new Properties();
pr.put("mail.transprot.protocol", "smtp");
pr.put("mail.smtp.port", 587);
pr.put("mail.host", "smtp.qq.com");
pr.setProperty("mail.transport.protocol", "smtp");
pr.put("mail.smtp.auth", true);
//创建邮箱
Session session=Session.getInstance(pr);
MimeMessage ms=new MimeMessage(session);
//邮箱来源
Address toAddress;
toAddress=new InternetAddress(username);
ms.setFrom(toAddress);
//邮箱接受者
ms.addRecipients(Message.RecipientType.TO, name);
ms.setSubject(top);
ms.setText(tile);
ms.saveChanges();//保存
Transport tr=session.getTransport();
tr.connect(username, pass);
tr.sendMessage(ms, ms.getAllRecipients());
//PrintWriter pw=response.getWriter();
/*pw.print("");
pw.close();*/
System.out.println("发送成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
需要这三个jar
javax.activation.jar
javax.mail.jar
smtp-1.4.jar