在lib加入activation.jar mail.jar两个jar包
index.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="error.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>发送电子邮件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="../image/com.css"/></head>
<body>
<form method="post" name="form1" action="email.jsp" onSubmit="return email()">
<table width="480" height="393" border="1" align="center" cellpadding="0" cellspacing="0">
<tr bordercolor="#D4D0C8" bgcolor="#FEFFC9">
<td height="27" colspan="2" align="left"><div align="center"><strong>发送电子邮件</strong></div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td width="68" height="27" align="left"><div align="right">收件人:</div></td>
<td width="406" height="27"><div align="center">
<input type="text" name="sname" size="51" value="252672099@qq.com">
</div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td height="27" align="left"><div align="right">发件人:</div></td>
<td height="27"> <div align="center">
<input type="text" name="jname" size="51" value="15890251132@126.com">
</div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td height="27" align="left"><div align="right">密 码:</div></td>
<td height="27"><div align="center"><input type="password" name="password" value="****" size="57"></div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td height="27" align="left"><div align="right">主 题:</div></td>
<td height="27"><div align="center">
<input name="title" type="text" value="15890251132" size="51">
</div></td>
</tr>
<tr align="left" bgcolor="#FEFFC9">
<td height="227" valign="top"><div align="right">内 容:</div></td>
<td height="227"><div align="center">
<textarea name="message" cols="50" rows="15"> asfdfasdfda</textarea>
</div></td>
</tr>
<tr align="center" valign="middle" bordercolor="#D4D0C8" bgcolor="#FEFFC9">
<td height="29" colspan="2"><input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="清除"></td>
</tr>
</table>
</form>
</body>
</html>
email.jsp
<%@ page language="java" import="javax.mail.*,javax.mail.internet.*,javax.activation.*,java.util.*,javax.mail.Message.*"
pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'email.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
try {
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.126.com");
Session session1 = Session.getInstance(props,new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("15890251132@126.com","*_****");
}
}
);
String S =request.getParameter("sname");
int n =S.indexOf('@');
int m=S.length() ;
String server=S.substring(n+1,m);
out.println(server);
session1.setDebug(true);
Message msg = new MimeMessage(session1);
msg.setFrom(new InternetAddress(request
.getParameter("jname")));
msg.setSubject(request.getParameter("title"));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(S));
msg.setContent(request.getParameter("message"), "text/html;charset=gbk");
Transport.send(msg);
%>
<P>Your mail has been sent.</P>
<%
} catch (Exception m) {
out.println(m.toString());
}
%>
</body>
</html>
1431

被折叠的 条评论
为什么被折叠?



