📄 sendmailservlet.java
字号:
package mypro;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Properties;
public class SendMailServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public class PopupAuthenticator extends Authenticator {
String username = null;
String password = null;
public PopupAuthenticator(String user, String pass) {
super();
this.username = user;
this.password = pass;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
// String smtpServer = request.getParameter("mailserver");
// String mailTo = request.getParameter("mailto");
// String mailFrom = request.getParameter("mailfrom");
// String subject = request.getParameter("subject");
// String content = request.getParameter("content");
String subject = "邮件主题";
String content = "邮件内容";
//String filecontent = request.getParameter("filecontent");
// System.out.println(smtpServer + mailTo + mailFrom + subject + content);
//System.out.println(request.getParameter("file1"));
//创建会话的时候,设置系统属性和连接服务器 的协议。
try {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
//定义了与邮件系统进行通信的邮件会话
Authenticator au = new PopupAuthenticator("", "");//用户名 密码
Session mailSession = Session.getInstance(props, au);
mailSession.setDebug(true);
//MimeMessage用来封装整个电子邮件对象
javax.mail.internet.MimeMessage msg = new MimeMessage(mailSession);
javax.mail.internet.InternetAddress address = new InternetAddress(
"@163.com"); //电子邮件地址
address.setPersonal("davinci528");
//指定消息的发件人
msg.setFrom(address);
//指定收件人的地址
msg.setRecipients(Message.RecipientType.TO, "@163.com");//电子邮件地址
//加入邮件标题
msg.setSubject(subject);
/**
* 邮件的正文内容通常可以分几个部分,包括文本正文,脚本正文及附件。
* 每一部分通常都用一个MimeBodyPart对象来装
* 接一下把几部分都加入Multipart对象,Multipart对象包含邮件正文的全部内容
* 最后把Multipart对象放入MimeMessage对象中,正文内容就设定完成
*/
//定义一个Multipart对象用来装整个邮件的正文部分
Multipart multipart = new MimeMultipart();
//定义一个MimeBodyPart对象,用来装邮件的文本内容然后加入Multipart对象中
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(content);
multipart.addBodyPart(messageBodyPart);
//定义一个MimeBodyPart对象,用来装邮件的附件内容然后加入Multipart对象中
MimeBodyPart fileBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("c:\\aaa.txt");
fileBodyPart.setDataHandler(new DataHandler(source));
fileBodyPart.setFileName(source.getName());
multipart.addBodyPart(fileBodyPart);
//定义一个MimeBodyPart对象,用来装邮件的脚本内容然后加入Multipart对象中
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(
"<meta http-equiv=Content-Type content=text/html; charset=gb2312>" +
"<meta http-equiv=Content-Type content=text/html; charset=gb2312>" +
"<div align=center><a href=http://www.csdn.net> csdn </a></div>",
"text/html;charset=GB2312");
multipart.addBodyPart(htmlBodyPart);
//把Multipart对象加入到MimeMessage对象中
msg.setContent(multipart);
javax.mail.Transport trans = mailSession.getTransport("smtp");
trans.connect("smtp.163.com", "", ""); //用户名 密码
trans.send(msg, msg.getRecipients(Message.RecipientType.TO));
// out.println("邮件发送成功" + mailTo);
trans.close();
} catch (Exception ex) {
System.out.println(ex.toString());
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -