index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Java Mail</title>
</head>
<body>
<div align="center">
<br><br>
<font color="blue" style="楷书"><h1>利用JavaMail来发送电子邮件</h1></font>
<br>
<br>
<form action="JavaMail.jsp" name="form1">
<p>寄信人: <input type="text" name="From" value="" size="30" /></p>
<p>收件人: <input type="text" name="To" value="" size="30" /></p>
<p>主 题: <input type="text" name="Subject" value="" size="30" /></p>
<p>内 容:</p>
<p><textarea name="Message" rows="8" cols="40">
</textarea></p>
<p><input type="submit" value="发送"/> <input type="reset" value="清除" /></p>
</form>
</div>
</body>
</html>
JavaMail.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.io.*,java.util.*" %>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Java Mail</title>
</head>
<body>
<center>
<h2>利用JavaMail来传送电子邮件</h2>
<%
request.setCharacterEncoding("GB2312");
InternetAddress[] address=null;
String mailserver="smtp.126.com";
String From=request.getParameter("From");
String To=request.getParameter("To");
String Subject=request.getParameter("Subject");
String message=request.getParameter("Message");
boolean sessionDebug=false;
try{
//设定服务器和传输协议
java.util.Properties props=System.getProperties();
props.put("mail.smtp.host",mailserver);
props.put("mail.transport.protocol","smtp");
//产生新的Session
javax.mail.Session mailSession=javax.mail.Session.getInstance(props,null);
mailSession.setDebug(sessionDebug);
javax.mail.Message msg=new MimeMessage(mailSession);
//设定传送邮件的发件人
msg.setFrom(new InternetAddress(From));
address=InternetAddress.parse(To,false);
msg.setRecipients(javax.mail.Message.RecipientType.TO,address);
msg.setSubject(Subject);
msg.setSentDate(new Date());
msg.setText(message);
Transport transport =mailSession.getTransport("smtp");
transport.send(msg);
out.println("邮件已顺利传送");}
catch(MessagingException mex){
mex.printStackTrace();}
%>
</center>
<%--
This example uses JSTL, uncomment the taglib directive above.
To test, display the page like this: index.jsp?sayHello=true&name=Murphy
--%>
<%--
<c:if test="${param.sayHello}">
<!-- Let's welcome the user ${param.name} -->
Hello ${param.name}!
</c:if>
--%>
</body>
</html>
用JAVAMAIL作了个邮件传送的例子却怎么也实现不了,填写了信息后到JavaMail.jsp就什么也不显示了也没有错误提示。我用的126的油箱。是不是写的有问题请高手指教以下。 本人万分感激!!!谢谢