jsp发送邮件(使用邮件服务器)
jsp页面 (表单页面)
<form name="form1" method="post" action="email.jsp" onSubmit="return mailCheck()">
<table width="100%" height="225" border="0" cellpadding="0" cellspacing="0" style="border:1px #ddd solid;">
<tr>
<td height="25" class="text1_1">您的邮箱:
<input type="text" id="fUser" name="fromUser" value="">
<font color="#FF0000">* </font><span class="text1">填写您能收到邮件的邮箱 如:zhangsan@163.co</span></td>
</tr>
<tr>
<td height="25" class="text1_1">发送地址:
<input type="text" id="toUser" name="toUser" value=aaa.@163.com readonly="redaonly">
</td>
</tr>
<tr>
<td height="25" class="text1_1">邮件主题:
<input type="text" id="fSubject" name="subject">
<font color="#FF0000">* </font><span class="text1">填写您的名字与应聘的岗位 如:张三应聘XX岗位</span></td>
</tr>
<tr>
<td height="100" valign="top">
<FCK:editor id="context" basePath="FCKeditor/" height="150" width="754" toolbarSet="Simple">
把您的简历粘贴到此处</FCK:editor></td>
</tr>
<tr>
<td height="25" class="text1">
<input type="submit" name="Submit" value="提 交">
<input type="reset" name="Submit2" value="重 写">
带<span class="text1_1"><font color="#FF0000">*</font></span>为必填项,请</td>
</tr>
</table>
</form>
?
email.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@page import="javax.mail.*;"%>
<jsp:useBean id="MyMail" scope="page" class="com.bf.isp.mail.Email" />
<%
String from=request.getParameter("fromUser");
String to=request.getParameter("toUser");
String subject=request.getParameter("subject");
subject = new String(subject.getBytes("iso8859-1"),"gb2312");
String body=request.getParameter("context");
body = new String(body.getBytes("iso8859-1"),"gb2312");
int i = MyMail.sendMail(from,to,subject,body);
if(i==1)
{%>
<script type="text/javascript">
<!--
alert("邮件发送成功!!!");
window.location.href="/indexServlet";
//-->
</script>
<% }else
{%>
<script type="text/javascript">
<!--
alert("邮件发送失败!!!");
window.location.href="/indexServlet";
//-->
</script>
<%
}
%>
?
email.java
package com.bf.isp.mail;
import java.util.*;
import javax.mail.*;
import java.io.*;
import javax.mail.internet.*;
import javax.activation.*;
public class Email {
public Email() {
super();
}
public int sendMail(String fromeUser, String toUser, String subject,
String context) {
Session session;
//创建MIME邮件对象
MimeMessage message;
//获得系统属性对象
Properties props = System.getProperties();
//设置smtp主机
props.put("mail.smtp.host", "smtp.jspinsider.com");
props.put("mail.smtp.host", "127.0.0.1"); //本机内测试
//props.put("mail.smtp.auth", "true");
//获得邮件会话对象
session = Session.getInstance(props, null);
//创建MIME邮件对象
message = new MimeMessage(session);
try {
//设置发信人
message.setFrom(new InternetAddress(fromeUser));
//设置收件人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
"toUser"));
//设置邮件主题
message.setSubject(subject,"gb2312");
//设置邮件正文
//message.setText(context);
//设置时间
message.setSentDate(new Date());
//设置内容,把内容转换成html内容
message.setContent(context,"text/html;charset=utf8");
Transport tr