现在的位置: 首页 > 综合 > 正文

jsp中的简单邮件传输例子(已验证)

2013年07月03日 ⁄ 综合 ⁄ 共 2780字 ⁄ 字号 评论关闭

sendmail.html源码:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>

<body>
<form action="sendmail.jsp" method="post">
<table align="center" width="640">
<tr>
<td width="208" rowspan="2">
发送到:<br><input name="to" size="25">
</td>
<td width="194" rowspan="2">
发送人地址:<br><input name="from" size="25">
</td>
<td width="218">
用户名:<input type="text" name="userId" size="20">
</td>
</tr>
<tr>
<td width="218">
&nbsp; 密码:<input type="password" name="password" size="20">
</td>
</tr>
<tr>
<td colspan="3" width="632">
标题:<br><input name="subject" size="50">
</td>
</tr>
<tr>
<td colspan="3" width="632">
<p>内容:<br><textarea name="text" rows=25 cols=85></textarea></p>
</td>
</tr>
<tr>
<td colspan="3" width="632">
<input type="submit" value="提交">
<input type="reset" value="重写">
</td>
</tr>
</table>
</body>
</html>

sendmail.jsp 源码:

<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.activation.*" %>

<html><head><title>网络发件箱</title>
</head>
<body>
<%
try{
 Properties props = new Properties();
 props.put("mail.smtp.host","smtp.163.com");//163
 props.put("mail.transport.protocol","smtp");
 props.put("mail.smtp.auth","true");
 Session mailSession;
 mailSession = Session.getInstance(props,null);
//设置http访问要使用的代理服务器的地址
//props.setProperty("http.proxyHost", "10.214.32.177");
//设置http访问要使用的代理服务器的端口
//props.setProperty("http.proxyPort", "8080");
//设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
//props.setProperty("http.nonProxyHosts", "localhost|10.10.*");

//设置安全访问使用的代理服务器地址与端口
//它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问
//props.setProperty("https.proxyHost", "10.214.32.177");
//props.setProperty("https.proxyPort", "443");

//使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
//props.setProperty("ftp.proxyHost", "10.214.32.177");
//props.setProperty("ftp.proxyPort", "21");
//props.setProperty("ftp.nonProxyHosts", "localhost|10.10.*");

//socks代理服务器的地址与端口
//props.setProperty("socksProxyHost", "10.214.32.177");
//props.setProperty("socksProxyPort", "1080");

 mailSession.setDebug(true);
 Message message = new MimeMessage(mailSession);
 message.setFrom(new InternetAddress(request.getParameter("from")));
 message.addRecipient(Message.RecipientType.TO,new InternetAddress(request.getParameter("to")));
 message.setSubject(new String(request.getParameter("subject").getBytes("ISO8859_1"),"GBK"));
 message.setSentDate(new Date());
 message.setText(new String(request.getParameter("text").getBytes("ISO8859_1"),"GBK"));
 Transport transport = mailSession.getTransport("smtp");
 transport.connect("smtp.163.com",request.getParameter("userId"),request.getParameter("password"));
 transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
 //transport.send(message);
 transport.close();
%>
<p>ok!!!!!</p>
<%
}
catch(MessagingException m)
{
 out.println(m.toString());
}
%>
</body>
</html>

抱歉!评论已关闭.