采用标准的javamail 类库,实现最简单的无附件的邮件发送
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
<%!class SmtpAuth extends javax.mail.Authenticator {
private String username, password;
public SmtpAuth(String username, String password) {
this.username = username;
this.password = password;
}
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
}%>
<%
if ("POST".equals(request.getMethod())) {
String smtpServer = "xxxx.net"; // 修改为你的邮件服务器的地址
int smtpServerPort = 25; // 端口,一般都是25
boolean ifAuth = true;// 服务器需要身份认证,如果不需要认证,则后面的用户名和密码就不需要了
String username = "xxxx@xxxx.net"; // 外发邮件的话,需要用户名
String password = ""; // 密码
String from = "xxxxx@xxxx.net"; // 发信人的邮件地址
String displayName = "老紫竹"; // 发信人的称呼
request.setCharacterEncoding("UTF-8");
HashMap<String, String> map = new HashMap<String, String>();
map.put("state", "success");
String message = "邮件发送成功!";
Session emailSession = null;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", smtpServerPort);
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
String contentType = "text/plain";
String charset = "UTF-8";
try {
if (ifAuth) {
props.put("mail.smtp.auth", "true");
SmtpAuth smtpAuth = new SmtpAuth(username, password);
emailSession = javax.mail.Session.getInstance(props, smtpAuth);
} else {
props.put("mail.smtp.auth", "false");
emailSession = javax.mail.Session.getDefaultInstance(props, null);
}
emailSession.setDebug(false);
Transport trans = null;
Message msg = new MimeMessage(emailSession);
try {
Address from_address = new InternetAddress(from, displayName);
msg.setFrom(from_address);
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content.toString(), contentType + ";charset=" + charset);
mp.addBodyPart(mbp);
msg.setContent(mp); // Multipart加入到信件
msg.setSentDate(new Date()); // 设置信件头的发送日期
// 发送信件
msg.saveChanges();
trans = emailSession.getTransport("smtp");
trans.connect(smtpServer, username, password);
trans.sendMessage(msg, msg.getAllRecipients());
trans.close();
} catch (AuthenticationFailedException e) {
map.put("state", "failed");
message = "邮件发送失败!错误原因:\n" + "身份验证错误!";
e.printStackTrace();
} catch (MessagingException e) {
message = "邮件发送失败!错误原因:\n" + e.getMessage();
map.put("state", "failed");
e.printStackTrace();
Exception ex = null;
if ((ex = e.getNextException()) != null) {
System.out.println(ex.toString());
ex.printStackTrace();
}
}
out.println("\n提示信息:" + message);
}
%>
<form method="post">
<table>
<tr>
<td>收件人:</td>
<td><input type="text" name="to" size="70" /></td>
</tr>
<tr>
<td>标题:</td>
<td><input type="text" name="subject" size="70" /></td>
</tr>
<tr>
<td>内容:</td>
<td><textarea name="content" cols="60" rows="10"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" /></td>
</tr>
</table>
</form>