这几天做了个表单提交邮件的小组件,供大家参考。
先简单介绍下:
一个jsp表单提交邮箱信息,注意这里我们把这些信息都只是封装起来,统一用我们的我们的中转邮箱去发送到收件人邮箱。
1.jsp 表单如下(js部分省了):
<body>
<center>
<div id="formDiv">
<form name="mailform" id="mailform" method="POST" method="post"
ENCTYPE="multipart/form-data">
<input type='hidden' name='actiontype' value='yxjg'>
<table width="786" style="margin-top: 68px">
<tr>
<td><b>用户名:</b></td>
<td><input type="text" name="userName" id="userName"
style="width: 228px;" οnfοcus="userNameFocus()"
οnblur="userNameBlur()" /></td>
<td><div id="userNameCheck" class="tipDiv"></div></td>
</tr>
<tr>
<td><b>您的邮箱:</b></td>
<td><input type="text" name="userEmail" id="userEmail"
style="width: 228px;" οnfοcus="userEmailFocus()"
οnblur="userEmailBlur()" /></td>
<td><div id="userEmailDiv" class="tipDiv"></div></td>
</tr>
<tr>
<td><b>您的电话:</b></td>
<td><input type="text" name="userPhone" id="userPhone"
style="width: 228px;" οnfοcus="userPhoneFocus()"
οnblur="userPhoneBlur()" /></td>
<td><div id="userPhoneDiv" class="tipDiv"></div></td>
</tr>
<tr>
<td><b>收件人:</b></td>
<td><input type=text name="toName" style="width: 228px;"
value="<%=toName%>" /></td>
<td><input type="hidden" name="toEmail" value="<%=toEmail%>" /></td>
</tr>
<tr>
<td><b>邮件主题:</b></td>
<td><input type="text" name="theme" id="theme"
style="width: 228px;" οnfοcus="themeFocus()" οnblur="themeBlur()" /></td>
<td><div id="themeDiv" class="tipDiv"></div></td>
</tr>
<tr>
<td><b>邮件正文:</b></td>
<td><textarea rows="12" cols="38" name="zhengwen"
id="zhengwen"></textarea></td>
</tr>
<tr>
<td><b>邮件附件:</b></td>
<td><input type="file" name="fujian" id="fujian" /></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type='button' id='tijiao' value='提交' οnclick='checkform()'> </td>
</tr>
</table>
</form>
</div>
</center>
</body>
String myName = "";
String myEmail = "";
//String myPhone = "";
String subject = "";
String msg = "";
String attachment = "";
List<FileItem> files = new ArrayList<FileItem>();
String actionmethod = StringUtil.getParameter(request,"e");
if ("yxjg".equals(actionmethod)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
boolean isMultipart = upload.isMultipartContent(request);
if(isMultipart){
Iterator items = null;
items = upload.parseRequest(request).iterator();
while(items.hasNext()){
FileItem item = (FileItem) items.next();
if(item.isFormField()){
if (item.getFieldName().equals("userEmail")) {
myEmail = new String(item.getString().getBytes("8859_1"),"utf-8");
} else if (item.getFieldName().equals(
"userName")) {
myName = new String(item.getString().getBytes("8859_1"),"utf-8");
} else if (item.getFieldName().equals("theme")) {
subject = new String(item.getString().getBytes("8859_1"),"utf-8");
} else if (item.getFieldName().equals(
"zhengwen")) {
msg = new String(item.getString().getBytes("8859_1"),"utf-8");
}
}else if(!item.isFormField()){
files.add(item);
attachment = item.getName();
}
}
}
}
3.发送邮件:
public void sendMail(){
String myEmailSMTPHost = "smtp.163.com";
String myEmailAccount = "****@163.com";
String myEmailPassword = "********";
Properties props = new Properties(); // 参数配置
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP
props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
Session session = Session.getDefaultInstance(props);
session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log
try
{
MimeMessage message = createMimeMessage(session, from,fromName, to, toName, subject, msg, attachmentPath);
Transport transport = session.getTransport();
transport.connect(myEmailSMTPHost, myEmailAccount, myEmailPassword);
//transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public MimeMessage createMimeMessage(Session session,
String from,String fromName, String to,String toName,String subject,String msg,String attachmentPath) throws Exception
{
// 1. 创建一封邮件
MimeMessage message = new MimeMessage(session);
// 2. From: 发件人
message.setFrom(new InternetAddress(from, fromName, "UTF-8"));
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(
to, toName, "UTF-8"));
// 4. Subject: 邮件主题
message.setSubject(subject, "UTF-8");
// 设置消息内容
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(msg);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// 附件部分
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
String attachmentPathTrim = attachmentPath.trim();
String fileName = attachmentPathTrim.substring(attachmentPathTrim.lastIndexOf("\\")+1);
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setSentDate(new Date());
message.saveChanges();
return message;
}