摘自:http://hi.baidu.com/lhypang2006/blog/item/72266af4c4b0bdd8f2d385f6.html
发送HTML格式的邮件
所谓HTML格式,就是超文本格式.你的邮件可以用HTML代码编写,发给对方后,对方收到的将是信息将是超文本,超文本比纯文本好看多了.下以面是在以前例子的基础上修改的程序:
注:撰写邮件的html文件仍然和前面(请参考jsp和Java Mail(三))那个一样,不需要作任何修改.
怎么样,这个程序是不是很简单呢?如果还有什么不懂的话,请在下面留言.下一次我们将要讲一讲怎样发送附件.
发送三种类型的附件
前面我们已学会了发送一般文本邮件和超文本邮件,今天我们将让大家学会编写三种类型的附件的邮件
发送程序.(注:撰写界面仍然用前面的)
发送附件是不是很有趣呢?但是在这里由于没有修改撰写界面,要发送的附件只能在程序里固定,所以一点都不灵活.不用担心,下一次我们将重写撰写界面,当然同时也会修改邮件处理程序,到时我们将能灵活地发送自己的邮件.
编写灵活的发送程序
本小节没加什么新鲜的东西,但是综合了以前的所有内容,可以让你灵活地发送你想要发的邮件.看了本小节之后对你会感觉到非常有用.
更改后的撰写界面程序如下:
-------------------------------------------------------------------------------------------
处理邮件的JSP程序如下:
----------------------------------------------------------------------------------------
到目前为止,我们基本上已学会了发各种类型的邮件了.但是,我们怎样去收自己的邮件呢,下一次再说吧:)!
发送HTML格式的邮件
所谓HTML格式,就是超文本格式.你的邮件可以用HTML代码编写,发给对方后,对方收到的将是信息将是超文本,超文本比纯文本好看多了.下以面是在以前例子的基础上修改的程序:
<%
@ page contentType
=
"
text/html;charset=GB2312
"
%>
<% request.setCharacterEncoding( " gb2312 " ); %>
<% @ page import = " java.util.*,javax.mail.* " %>
<% @ page import = " javax.mail.internet.* " %>
< 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 " );
Properties props = new Properties();
props.put( " mail.smtp.host " , " 127.0.0.1 " );
props.put( " mail.smtp.auth " , " true " );
Session s = Session.getInstance(props);
s.setDebug( true );
MimeMessage message = new MimeMessage(s);
// 给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from = new InternetAddress( " xxf@cafe.com " );
message.setFrom(from);
InternetAddress to = new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate( new Date());
// 给消息对象设置内容
BodyPart mdp = new MimeBodyPart(); // 新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent, " text/html;charset=gb2312 " ); // 给BodyPart对象设置内容和格式/编码方式
Multipart mm = new MimeMultipart(); // 新建一个MimeMultipart对象用来存放BodyPart对
// 象(事实上可以存放多个)
mm.addBodyPart(mdp); // 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
message.setContent(mm); // 把mm作为消息对象的内容
message.saveChanges();
Transport transport = s.getTransport( " smtp " );
transport.connect( " 127.0.0.1 " , " xxf " , " coffee " );
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
< div align = " center " >
< p >< font color = " #FF6600 " > 发送成功 !</ font ></ p >
< p >< a href = " recmail.jsp " > 去看看我的信箱 </ a >< br >
< br >
< a href = " index.htm " > 再发一封 </ a > </ p >
</ div >
<%
} catch (MessagingException e){
out.println(e.toString());
}
%>
</ body >
</ html >
<% request.setCharacterEncoding( " gb2312 " ); %>
<% @ page import = " java.util.*,javax.mail.* " %>
<% @ page import = " javax.mail.internet.* " %>
< 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 " );
Properties props = new Properties();
props.put( " mail.smtp.host " , " 127.0.0.1 " );
props.put( " mail.smtp.auth " , " true " );
Session s = Session.getInstance(props);
s.setDebug( true );
MimeMessage message = new MimeMessage(s);
// 给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from = new InternetAddress( " xxf@cafe.com " );
message.setFrom(from);
InternetAddress to = new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate( new Date());
// 给消息对象设置内容
BodyPart mdp = new MimeBodyPart(); // 新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent, " text/html;charset=gb2312 " ); // 给BodyPart对象设置内容和格式/编码方式
Multipart mm = new MimeMultipart(); // 新建一个MimeMultipart对象用来存放BodyPart对
// 象(事实上可以存放多个)
mm.addBodyPart(mdp); // 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
message.setContent(mm); // 把mm作为消息对象的内容
message.saveChanges();
Transport transport = s.getTransport( " smtp " );
transport.connect( " 127.0.0.1 " , " xxf " , " coffee " );
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
< div align = " center " >
< p >< font color = " #FF6600 " > 发送成功 !</ font ></ p >
< p >< a href = " recmail.jsp " > 去看看我的信箱 </ a >< br >
< br >
< a href = " index.htm " > 再发一封 </ a > </ p >
</ div >
<%
} catch (MessagingException e){
out.println(e.toString());
}
%>
</ body >
</ html >
注:撰写邮件的html文件仍然和前面(请参考jsp和Java Mail(三))那个一样,不需要作任何修改.
怎么样,这个程序是不是很简单呢?如果还有什么不懂的话,请在下面留言.下一次我们将要讲一讲怎样发送附件.
发送三种类型的附件
前面我们已学会了发送一般文本邮件和超文本邮件,今天我们将让大家学会编写三种类型的附件的邮件
发送程序.(注:撰写界面仍然用前面的)
<%
@ page contentType
=
"
text/html;charset=GB2312
"
%>
<% request.setCharacterEncoding( " gb2312 " ); %>
<% @ page import = " java.util.*,javax.mail.* " %>
<% @ page import = " javax.mail.internet.* " %>
<% @ page import = " javax.activation.* " %><!-- 要发送附件必须引入该库 -->
<% @ page import = " java.net.* " %><!-- 要用到URL类 -->
< 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 " );
Properties props = new Properties();
props.put( " mail.smtp.host " , " 127.0.0.1 " );
props.put( " mail.smtp.auth " , " true " );
Session s = Session.getInstance(props);
s.setDebug( true );
MimeMessage message = new MimeMessage(s);
// 给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from = new InternetAddress( " xxf@cafe.com " );
message.setFrom(from);
InternetAddress to = new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate( new Date());
Multipart mm = new MimeMultipart(); // 新建一个MimeMultipart对象用来存放多个BodyPart对象
// 设置信件文本内容
BodyPart mdp = new MimeBodyPart(); // 新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent, " text/html;charset=gb2312 " ); // 给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp); // 将含有信件内容的BodyPart加入到MimeMultipart对象中
// 设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
mdp = new MimeBodyPart(); // 新建一个存放附件的BodyPart
DataHandler dh = new DataHandler( " JavaMail附件测试 " , " text/plain;charset=gb2312 " );
// 新建一个DataHandler对象,并设置其内容和格式/编码方式
mdp.setFileName( " xxf.txt " ); // 加上这句将作为附件发送,否则将作为信件的文本内容
mdp.setDataHandler(dh); // 给BodyPart对象设置内容为dh
mm.addBodyPart(mdp); // 将含有附件的BodyPart加入到MimeMultipart对象中
// 设置信件的附件2(用本地上的文件作为附件)
mdp = new MimeBodyPart();
FileDataSource fds = new FileDataSource( " g:/xx.txt " );
dh = new DataHandler(fds);
mdp.setFileName( " dd.txt " ); // 可以和原文件名不一致
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
// 设置信件的附件3(用远程文件作为附件)
mdp = new MimeBodyPart();
URLDataSource ur = new URLDataSource( new URL( " http://localhost:8080/jspstudy/email/xx.gif " ));
// 注:这里用的参数只能为URL对象,不能为URL字串,在前面类介绍时有误(请谅解),这里纠正一下.
dh = new DataHandler(ur);
mdp.setFileName( " ss.txt " );
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
message.setContent(mm); // 把mm作为消息对象的内容
message.saveChanges();
Transport transport = s.getTransport( " smtp " );
transport.connect( " 127.0.0.1 " , " xxf " , " coffee " );
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
< div align = " center " >
< p >< font color = " #FF6600 " > 发送成功 !</ font ></ p >
< p >< a href = " recmail.jsp " > 去看看我的信箱 </ a >< br >
< br >
< a href = " index.htm " > 再发一封 </ a > </ p >
</ div >
<%
} catch (MessagingException e){
out.println(e.toString());
}
%>
</ body >
</ html >
<% request.setCharacterEncoding( " gb2312 " ); %>
<% @ page import = " java.util.*,javax.mail.* " %>
<% @ page import = " javax.mail.internet.* " %>
<% @ page import = " javax.activation.* " %><!-- 要发送附件必须引入该库 -->
<% @ page import = " java.net.* " %><!-- 要用到URL类 -->
< 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 " );
Properties props = new Properties();
props.put( " mail.smtp.host " , " 127.0.0.1 " );
props.put( " mail.smtp.auth " , " true " );
Session s = Session.getInstance(props);
s.setDebug( true );
MimeMessage message = new MimeMessage(s);
// 给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from = new InternetAddress( " xxf@cafe.com " );
message.setFrom(from);
InternetAddress to = new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate( new Date());
Multipart mm = new MimeMultipart(); // 新建一个MimeMultipart对象用来存放多个BodyPart对象
// 设置信件文本内容
BodyPart mdp = new MimeBodyPart(); // 新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent, " text/html;charset=gb2312 " ); // 给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp); // 将含有信件内容的BodyPart加入到MimeMultipart对象中
// 设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
mdp = new MimeBodyPart(); // 新建一个存放附件的BodyPart
DataHandler dh = new DataHandler( " JavaMail附件测试 " , " text/plain;charset=gb2312 " );
// 新建一个DataHandler对象,并设置其内容和格式/编码方式
mdp.setFileName( " xxf.txt " ); // 加上这句将作为附件发送,否则将作为信件的文本内容
mdp.setDataHandler(dh); // 给BodyPart对象设置内容为dh
mm.addBodyPart(mdp); // 将含有附件的BodyPart加入到MimeMultipart对象中
// 设置信件的附件2(用本地上的文件作为附件)
mdp = new MimeBodyPart();
FileDataSource fds = new FileDataSource( " g:/xx.txt " );
dh = new DataHandler(fds);
mdp.setFileName( " dd.txt " ); // 可以和原文件名不一致
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
// 设置信件的附件3(用远程文件作为附件)
mdp = new MimeBodyPart();
URLDataSource ur = new URLDataSource( new URL( " http://localhost:8080/jspstudy/email/xx.gif " ));
// 注:这里用的参数只能为URL对象,不能为URL字串,在前面类介绍时有误(请谅解),这里纠正一下.
dh = new DataHandler(ur);
mdp.setFileName( " ss.txt " );
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
message.setContent(mm); // 把mm作为消息对象的内容
message.saveChanges();
Transport transport = s.getTransport( " smtp " );
transport.connect( " 127.0.0.1 " , " xxf " , " coffee " );
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
< div align = " center " >
< p >< font color = " #FF6600 " > 发送成功 !</ font ></ p >
< p >< a href = " recmail.jsp " > 去看看我的信箱 </ a >< br >
< br >
< a href = " index.htm " > 再发一封 </ a > </ p >
</ div >
<%
} catch (MessagingException e){
out.println(e.toString());
}
%>
</ body >
</ html >
发送附件是不是很有趣呢?但是在这里由于没有修改撰写界面,要发送的附件只能在程序里固定,所以一点都不灵活.不用担心,下一次我们将重写撰写界面,当然同时也会修改邮件处理程序,到时我们将能灵活地发送自己的邮件.
编写灵活的发送程序
本小节没加什么新鲜的东西,但是综合了以前的所有内容,可以让你灵活地发送你想要发的邮件.看了本小节之后对你会感觉到非常有用.
更改后的撰写界面程序如下:
-------------------------------------------------------------------------------------------
<
html
>
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" >
< title > 撰写邮件 </ title >
</ head >
< body >
< form action ="testall.jsp" method ="post" name ="form1" >
< table width ="75" border ="0" align ="center" cellspacing ="1" bgcolor ="#006600" class ="black" >
< tr bgcolor ="#FFFFFF" >
< td width ="24%" > 收信人地址: </ td >
< td width ="76%" > < 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 ="18" colspan ="2" bgcolor ="#FFFFFF" > 信件类型
< select name ="emailtype" id ="emailtype" >
< option value ="text/plain" selected > Text </ option >
< option value ="text/html" > Html </ option >
</ select ></ 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" >
< td colspan ="2" bgcolor ="#FFFFFF" > 附件1(自定义):
< input name ="fj1" type ="text" id ="fj1" >
(输入文本信息) </ td >
</ tr >
< tr align ="center" valign ="bottom" >
< td colspan ="2" bgcolor ="#FFFFFF" > 附件2(本地):
< input name ="fj2" type ="file" id ="fj2" size ="10" ></ td >
</ tr >
< tr align ="center" >
< td colspan ="2" bgcolor ="#FFFFFF" > 附件3(远程):
< input name ="fj3" type ="text" id ="fj3" value ="http://" >
(输入URL) </ td >
</ tr >
< tr align ="center" >
< td colspan ="2" bgcolor ="#FFFFFF" > < input type ="submit" name ="Submit" value ="发送" >
< input type ="reset" name ="Submit2" value ="重置" ></ td >
</ tr >
</ table >
</ form >
</ body >
</ html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" >
< title > 撰写邮件 </ title >
</ head >
< body >
< form action ="testall.jsp" method ="post" name ="form1" >
< table width ="75" border ="0" align ="center" cellspacing ="1" bgcolor ="#006600" class ="black" >
< tr bgcolor ="#FFFFFF" >
< td width ="24%" > 收信人地址: </ td >
< td width ="76%" > < 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 ="18" colspan ="2" bgcolor ="#FFFFFF" > 信件类型
< select name ="emailtype" id ="emailtype" >
< option value ="text/plain" selected > Text </ option >
< option value ="text/html" > Html </ option >
</ select ></ 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" >
< td colspan ="2" bgcolor ="#FFFFFF" > 附件1(自定义):
< input name ="fj1" type ="text" id ="fj1" >
(输入文本信息) </ td >
</ tr >
< tr align ="center" valign ="bottom" >
< td colspan ="2" bgcolor ="#FFFFFF" > 附件2(本地):
< input name ="fj2" type ="file" id ="fj2" size ="10" ></ td >
</ tr >
< tr align ="center" >
< td colspan ="2" bgcolor ="#FFFFFF" > 附件3(远程):
< input name ="fj3" type ="text" id ="fj3" value ="http://" >
(输入URL) </ td >
</ tr >
< tr align ="center" >
< td colspan ="2" bgcolor ="#FFFFFF" > < input type ="submit" name ="Submit" value ="发送" >
< input type ="reset" name ="Submit2" value ="重置" ></ td >
</ tr >
</ table >
</ form >
</ body >
</ html >
处理邮件的JSP程序如下:
----------------------------------------------------------------------------------------
<%
@ page contentType
=
"
text/html;charset=GB2312
"
%>
<% request.setCharacterEncoding( " gb2312 " ); %>
<% @ page import = " java.util.*,javax.mail.* " %>
<% @ page import = " javax.mail.internet.* " %>
<% @ page import = " javax.activation.* " %><!-- 要发送附件必须引入该库 -->
<% @ page import = " java.net.* " %><!-- 要用到URL类 -->
< 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 emailtype = request.getParameter( " emailtype " ); // 获取email类型
String tcontent = request.getParameter( " content " );
String tfj1 = request.getParameter( " fj1 " );
String tfj2 = request.getParameter( " fj2 " );
String tfj3 = request.getParameter( " fj3 " );
Properties props = new Properties();
props.put( " mail.smtp.host " , " 127.0.0.1 " );
props.put( " mail.smtp.auth " , " true " );
Session s = Session.getInstance(props);
s.setDebug( true );
MimeMessage message = new MimeMessage(s);
// 给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from = new InternetAddress( " xxf@cafe.com " );
message.setFrom(from);
InternetAddress to = new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate( new Date());
Multipart mm = new MimeMultipart(); // 新建一个MimeMultipart对象用来存放多个BodyPart对象
// 设置信件文本内容
BodyPart mdp = new MimeBodyPart(); // 新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,emailtype + " ;charset=gb2312 " ); // 给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp); // 将含有信件内容的BodyPart加入到MimeMultipart对象中
// 设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
mdp = new MimeBodyPart(); // 新建一个存放附件的BodyPart
DataHandler dh = new DataHandler(tfj1, " text/plain;charset=gb2312 " );
// 新建一个DataHandler对象,并设置其内容和格式/编码方式
mdp.setFileName( " text.txt " ); // 加上这句将作为附件发送,否则将作为信件的文本内容
mdp.setDataHandler(dh); // 给BodyPart对象设置内容为dh
mm.addBodyPart(mdp); // 将含有附件的BodyPart加入到MimeMultipart对象中
// 设置信件的附件2(用本地上的文件作为附件)
mdp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(tfj2);
dh = new DataHandler(fds);
int ddd = tfj2.lastIndexOf( " / " );
String fname = tfj2.substring(ddd); // 提取文件名
String ffname = new String(fname.getBytes( " gb2312 " ), " ISO8859-1 " ); // 处理文件名是中文的情况
mdp.setFileName(ffname); // 可以和原文件名不一致,但最好一样
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
// 设置信件的附件3(用远程文件作为附件)
mdp = new MimeBodyPart();
URL urlfj = new URL(tfj3);
URLDataSource ur = new URLDataSource(urlfj);
// 注:这里用的参数只能为URL对象,不能为URL字串,在前面类介绍时有误(请谅解),这里纠正一下.
dh = new DataHandler(ur);
int ttt = tfj3.lastIndexOf( " / " );
String urlname = tfj3.substring(ttt);
// String urlfname=new String(urlname.getBytes("gb2312"),"ISO8859-1"); // 不知怎么回事,这里不能处理中文问题
mdp.setFileName(urlname);
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
message.setContent(mm); // 把mm作为消息对象的内容
message.saveChanges();
Transport transport = s.getTransport( " smtp " );
transport.connect( " 127.0.0.1 " , " xxf " , " coffee " );
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
< div align = " center " >
< p >< font color = " #FF6600 " > 发送成功 !</ font ></ p >
< p >< a href = " recmail.jsp " > 去看看我的信箱 </ a >< br >
< br >
< a href = " index.htm " > 再发一封 </ a > </ p >
</ div >
<%
} catch (MessagingException e){
out.println(e.toString());
}
%>
</ body >
</ html >
<% request.setCharacterEncoding( " gb2312 " ); %>
<% @ page import = " java.util.*,javax.mail.* " %>
<% @ page import = " javax.mail.internet.* " %>
<% @ page import = " javax.activation.* " %><!-- 要发送附件必须引入该库 -->
<% @ page import = " java.net.* " %><!-- 要用到URL类 -->
< 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 emailtype = request.getParameter( " emailtype " ); // 获取email类型
String tcontent = request.getParameter( " content " );
String tfj1 = request.getParameter( " fj1 " );
String tfj2 = request.getParameter( " fj2 " );
String tfj3 = request.getParameter( " fj3 " );
Properties props = new Properties();
props.put( " mail.smtp.host " , " 127.0.0.1 " );
props.put( " mail.smtp.auth " , " true " );
Session s = Session.getInstance(props);
s.setDebug( true );
MimeMessage message = new MimeMessage(s);
// 给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from = new InternetAddress( " xxf@cafe.com " );
message.setFrom(from);
InternetAddress to = new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate( new Date());
Multipart mm = new MimeMultipart(); // 新建一个MimeMultipart对象用来存放多个BodyPart对象
// 设置信件文本内容
BodyPart mdp = new MimeBodyPart(); // 新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,emailtype + " ;charset=gb2312 " ); // 给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp); // 将含有信件内容的BodyPart加入到MimeMultipart对象中
// 设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
mdp = new MimeBodyPart(); // 新建一个存放附件的BodyPart
DataHandler dh = new DataHandler(tfj1, " text/plain;charset=gb2312 " );
// 新建一个DataHandler对象,并设置其内容和格式/编码方式
mdp.setFileName( " text.txt " ); // 加上这句将作为附件发送,否则将作为信件的文本内容
mdp.setDataHandler(dh); // 给BodyPart对象设置内容为dh
mm.addBodyPart(mdp); // 将含有附件的BodyPart加入到MimeMultipart对象中
// 设置信件的附件2(用本地上的文件作为附件)
mdp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(tfj2);
dh = new DataHandler(fds);
int ddd = tfj2.lastIndexOf( " / " );
String fname = tfj2.substring(ddd); // 提取文件名
String ffname = new String(fname.getBytes( " gb2312 " ), " ISO8859-1 " ); // 处理文件名是中文的情况
mdp.setFileName(ffname); // 可以和原文件名不一致,但最好一样
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
// 设置信件的附件3(用远程文件作为附件)
mdp = new MimeBodyPart();
URL urlfj = new URL(tfj3);
URLDataSource ur = new URLDataSource(urlfj);
// 注:这里用的参数只能为URL对象,不能为URL字串,在前面类介绍时有误(请谅解),这里纠正一下.
dh = new DataHandler(ur);
int ttt = tfj3.lastIndexOf( " / " );
String urlname = tfj3.substring(ttt);
// String urlfname=new String(urlname.getBytes("gb2312"),"ISO8859-1"); // 不知怎么回事,这里不能处理中文问题
mdp.setFileName(urlname);
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
message.setContent(mm); // 把mm作为消息对象的内容
message.saveChanges();
Transport transport = s.getTransport( " smtp " );
transport.connect( " 127.0.0.1 " , " xxf " , " coffee " );
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
< div align = " center " >
< p >< font color = " #FF6600 " > 发送成功 !</ font ></ p >
< p >< a href = " recmail.jsp " > 去看看我的信箱 </ a >< br >
< br >
< a href = " index.htm " > 再发一封 </ a > </ p >
</ div >
<%
} catch (MessagingException e){
out.println(e.toString());
}
%>
</ body >
</ html >
到目前为止,我们基本上已学会了发各种类型的邮件了.但是,我们怎样去收自己的邮件呢,下一次再说吧:)!