JSP与JavaMail (二)

王朝java/jsp·作者佚名  2008-05-30
窄屏简体版  字體: |||超大  

5.发送HTML格式的邮件

所谓HTML格式,就是超文本格式.你的邮件可以用HTML代码编写,发给对方后,对方收到的将是信息将是超文本,超文本比纯文本好看多了.下以面是在以前例子的基础上修改的程序:

发送成功

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();

%

发送成功!

去看看我的信箱

再发一封

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航