各位jsp高手 。麻烦看一下我的代码哪里出现错误。我刚学习Java Mail
这里有两个文件 第一个是Fj.html,第二是Testj.jsp。
第一个文件Fj.html
-----------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>写邮件---附件</title>
</head>
<body>
<form action="Testfj.jsp" method="post" name="form1">
<table width="75" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black">
<tr bgcolor="#FFFFFF">
<td width="40%">收信人地址:</td>
<td width="80%"> <input name="to" type="text" id="to"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>主题:</td>
<td> <input name="title" type="text" id="title"></td>
</tr><tr>
<td height="53" colspan="2" bgcolor="#FFFFFF"><textarea name="content" cols="50" rows="5" id="content"></textarea></td>
</tr><tr align="center" valign="bottom">
<td colspan="2" bgcolor="#FFFFFF">附件:
<input name="fj" type="file" id="fj" size="10"></td>
</tr><tr align="center">
<td colspan="2" bgcolor="#FFFFFF"> <input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
第二个文件
-------------------------------------------------------
<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%><!--要发送附件必须引入该库--><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>
<body><%
try{
String tto=request.getParameter("to");String ttitle=request.getParameter("title");String tcontent=request.getParameter("content");String tfj=request.getParameter("fj");
Properties props=new Properties();
props.put("mail.smtp.host","smtp.126.com");
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);MimeMessage message=new MimeMessage(s);//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from=new InternetAddress("[email protected]");
message.setFrom(from);
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());Multipart test=new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象//设置信件文本内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
test.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中//设置信件的附件
mdp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(tfj);
DataHandler dh=new DataHandler(fds);
int ddd=tfj.lastIndexOf("\\");
String fname=tfj.substring(ddd);//提取文件名
String ffname=new String(fname.getBytes( "gb2312 "), "ISO8859-1 ");//处理文件名是中文的情况 mdp.setFileName(ffname);//
mdp.setDataHandler(dh);
test.addBodyPart(mdp);
message.setContent(test);//把mm作为消息对象的内容message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect("smtp.126.com","jspmail123","123456");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p><br></div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>
这里有两个文件 第一个是Fj.html,第二是Testj.jsp。
第一个文件Fj.html
-----------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>写邮件---附件</title>
</head>
<body>
<form action="Testfj.jsp" method="post" name="form1">
<table width="75" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black">
<tr bgcolor="#FFFFFF">
<td width="40%">收信人地址:</td>
<td width="80%"> <input name="to" type="text" id="to"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>主题:</td>
<td> <input name="title" type="text" id="title"></td>
</tr><tr>
<td height="53" colspan="2" bgcolor="#FFFFFF"><textarea name="content" cols="50" rows="5" id="content"></textarea></td>
</tr><tr align="center" valign="bottom">
<td colspan="2" bgcolor="#FFFFFF">附件:
<input name="fj" type="file" id="fj" size="10"></td>
</tr><tr align="center">
<td colspan="2" bgcolor="#FFFFFF"> <input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
第二个文件
-------------------------------------------------------
<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%><!--要发送附件必须引入该库--><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>
<body><%
try{
String tto=request.getParameter("to");String ttitle=request.getParameter("title");String tcontent=request.getParameter("content");String tfj=request.getParameter("fj");
Properties props=new Properties();
props.put("mail.smtp.host","smtp.126.com");
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);MimeMessage message=new MimeMessage(s);//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from=new InternetAddress("[email protected]");
message.setFrom(from);
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());Multipart test=new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象//设置信件文本内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
test.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中//设置信件的附件
mdp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(tfj);
DataHandler dh=new DataHandler(fds);
int ddd=tfj.lastIndexOf("\\");
String fname=tfj.substring(ddd);//提取文件名
String ffname=new String(fname.getBytes( "gb2312 "), "ISO8859-1 ");//处理文件名是中文的情况 mdp.setFileName(ffname);//
mdp.setDataHandler(dh);
test.addBodyPart(mdp);
message.setContent(test);//把mm作为消息对象的内容message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect("smtp.126.com","jspmail123","123456");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p><br></div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>
import java.util.Properties;import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;/**
*author:East(张栋芳)
*date:2008-7-17
*
**/
public class SendMail {
private static MailAuthenticator autherticator=null;
public static void main(String[] args) {
for (int i=0;i<5;i++){
String from="[email protected]";
String to="[email protected]";
String smtpServer="smtp.163.com";
String subject="Hello ,this is a email Test!!";
String content ="Welcome to you!!";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth","true");
autherticator = new MailAuthenticator("[email protected]","XXXXXXX");
Session session = Session.getDefaultInstance(props,autherticator);
MimeMessage msg = new MimeMessage(session);
try{
msg.setFrom(new InternetAddress(from));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(content);
Transport.send(msg);
System.out.println("成功发送邮件......");
}catch(Exception se){
se.printStackTrace();
}
}
}
}
//现在的大部分的邮件服务器都要求有身份验证,所以需要此类实现验证功能
class MailAuthenticator extends Authenticator{
private String username = null;
private String userpasswd = null;
public MailAuthenticator(){}
public MailAuthenticator(String username,String userpasswd){
this.username = username;
this.userpasswd = userpasswd;
}
public void setUserName(String username){
this.username = username;
}
public void setPassword(String password){
this.userpasswd = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,userpasswd);
}
}
package com.east.email;import java.util.Date;
import java.util.Properties;import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;/**
*author:East(张栋芳)
*date:2008-7-17
*
**/
public class SendMail {
private static MailAuthenticator autherticator=null;
public static void main(String[] args) {
for (int i=0;i<5;i++){
String from="[email protected]";
String to="[email protected]";
String smtpServer="smtp.163.com";
String subject="Hello ,this is a email Test!!";
String content ="Welcome to you!!";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth","true");
autherticator = new MailAuthenticator("[email protected]","xtsaiyy520");
Session session = Session.getDefaultInstance(props,autherticator);
MimeMessage msg = new MimeMessage(session);
try{
msg.setFrom(new InternetAddress(from));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(content);
Transport.send(msg);
System.out.println("成功发送邮件......");
}catch(Exception se){
se.printStackTrace();
}
}
}
}
//现在的大部分的邮件服务器都要求有身份验证,所以需要此类实现验证功能
class MailAuthenticator extends Authenticator{
private String username = null;
private String userpasswd = null;
public MailAuthenticator(){}
public MailAuthenticator(String username,String userpasswd){
this.username = username;
this.userpasswd = userpasswd;
}
public void setUserName(String username){
this.username = username;
}
public void setPassword(String password){
this.userpasswd = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,userpasswd);
}
}