急!急!急!怎么在jsp中用javamail发送html格式的邮件???
源代码:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page language="java" import="java.util.*,javax.mail.*,javax.mail.internet.*"%>
<%
try{
String host = "邮件发送服务器地址";
String from = "发送邮箱名";
String to = "接收邮箱名";
String username = "发送邮箱的发送验证用户名";
String password = "发送邮箱的发送验证口令";
// Get system properties
// Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true"); //这样才能通过验证
// Get session
Session sessionDLT = Session.getDefaultInstance(props);
// watch the mail commands go by to the mail server
sessionDLT.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(sessionDLT);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail"); //此处怎么写成html格式,并能在邮件接收方显示。。。
// Send message
message.saveChanges();
Transport transport = sessionDLT.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
out.println("您的邮件发送成功!");
}catch(Exception e){
out.println(e.getMessage());
}
%>
期待哪位大虾拔刀相助!
问题点数:100、回复次数:8Top
1 楼Andrawu(晓彬)回复于 2002-07-05 16:37:14 得分 0
我没有写过email,
下面的方法供你参考。
private void send(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, ServletOutputStream servletoutputstream, HttpSession httpsession)
throws IOException
{
String s = httpservletrequest.getParameter("to");
String s1 = httpservletrequest.getParameter("cc");
String s2 = httpservletrequest.getParameter("subject");
String s3 = httpservletrequest.getParameter("text");
try
{
MailUserData mailuserdata = getMUD(httpsession);
if(mailuserdata == null)
throw new Exception("trying to send, but not logged in");
MimeMessage mimemessage = new MimeMessage(mailuserdata.getSession());
Object obj = null;
Object obj1 = null;
if(s != null)
{
InternetAddress ainternetaddress[] = InternetAddress.parse(s, false);
mimemessage.setRecipients(javax.mail.Message.RecipientType.TO, ainternetaddress);
} else
{
throw new MessagingException("No \"To\" address specified");
}
if(s1 != null)
{
InternetAddress ainternetaddress1[] = InternetAddress.parse(s1, false);
mimemessage.setRecipients(javax.mail.Message.RecipientType.CC, ainternetaddress1);
}
if(s2 != null)
mimemessage.setSubject(s2);
URLName urlname = mailuserdata.getURLName();
mimemessage.setFrom(new InternetAddress(urlname.getUsername() + "@" + urlname.getHost()));
if(s3 != null)
mimemessage.setText(s3);
Transport.send(mimemessage);
servletoutputstream.println("<h1>Message sent successfully</h1></body></html>");
servletoutputstream.close();
}
catch(Exception exception)
{
servletoutputstream.println("<h1>Error sending message.</h1>");
servletoutputstream.println(exception.toString());
servletoutputstream.println("<br></body></html>");
}
}
private String getDisplayAddress(Address address)
{
String s = null;
String s1 = null;
if((address instanceof InternetAddress) && (s = ((InternetAddress)address).getPersonal()) != null)
s1 = s + " " + "<" + ((InternetAddress)address).getAddress() + ">";
else
s1 = address.toString();
return s1;
}
Top
2 楼beyond_xiruo(CorruptionException)回复于 2002-07-05 16:40:43 得分 0
http://www.21active.com/DataStorage/TechInfo/Read.asp?ID=230Top
3 楼beyond_xiruo(CorruptionException)回复于 2002-07-05 19:12:31 得分 50
转贴出来,我实验成功了!
邮件例程 - JavaMail - 发送HTML邮件
作者:何志强 主页:http:// 发布方式:转载
form.htm
========
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>邮件例程 - JavaMail - 发送HTML邮件</title>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0">
<form method="post" action="send.jsp">
<tr>
<td>SMTP主机:</td>
<td><input type="text" name="smtp" size="80"></td>
</tr>
<tr>
<td>发信人:</td>
<td><input type="text" name="from" size="80"></td>
</tr>
<tr>
<td>收信人:</td>
<td><input type="text" name="to" size="80"></td>
</tr>
<tr>
<td>抄送人:</td>
<td><input type="text" name="cc" size="80"></td>
</tr>
<tr>
<td>暗送人:</td>
<td><input type="text" name="bcc" size="80"></td>
</tr>
<tr>
<td>主题:</td>
<td><input type="text" name="subject" size="80"></td>
</tr>
<tr>
<td valign="top">内容:</td>
<td><textarea name="body" rows="5" cols="80"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="发送"></td>
</tr>
</form>
</table>
</body>
</html>
send.jsp
========
<%--
作者:何志强[hhzqq@21cn.com]
日期:2000-08-16
版本:1.0
功能:邮件例程 - JavaMail - 发送HTML邮件
--%>
<%
//变量声明
java.lang.String smtp,from,to,cc,bcc,subject,body;
//获得用户输入数据
smtp = request.getParameter("smtp");
from = request.getParameter("from");
to = request.getParameter("to");
cc = request.getParameter("cc");
bcc = request.getParameter("bcc");
subject = request.getParameter("subject");
if(subject!=null){
subject = new java.lang.String(subject.getBytes("iso-8859-1"));
}
body = request.getParameter("body");
//发送邮件
pipi.mail.HTML.send(smtp,from,to,cc,bcc,subject,body);
%>
pipi.jaf.StringDataSource.java
==============================
/*
作者:何志强[hhzqq@21cn.com]
日期:2000-08-16
功能:字符串型数据源
*/
package pipi.jaf;
public class StringDataSource implements javax.activation.DataSource{
private java.lang.String data;
private java.lang.String type;
public StringDataSource(java.lang.String data,java.lang.String type){
this.data = data;
this.type = type;
}
public java.io.InputStream getInputStream() throws java.io.IOException{
return new java.io.StringBufferInputStream(data);
}
public java.io.OutputStream getOutputStream() throws java.io.IOException{
throw new java.io.IOException("it does not support this method now!");
}
public java.lang.String getContentType(){
return type;
}
public java.lang.String getName(){
return "pipi";
}
}
pipi.mail.HTML.java
===================
/*
作者:何志强[hhzqq@21cn.com]
日期:2000-08-16
功能:发送HTML邮件
*/
package pipi.mail;
public final class HTML{
public static void send(
java.lang.String smtp, /*SMTP主机地址*/
java.lang.String from, /*发信人*/
java.lang.String to, /*收信人*/
java.lang.String cc, /*抄送人*/
java.lang.String bcc, /*暗送人*/
java.lang.String subject, /*主题*/
java.lang.String body /*内容*/
) throws java.lang.Exception{
//变量声明
java.util.Properties props; //系统属性
javax.mail.Session mailSession; //邮件会话对象
javax.mail.internet.MimeMessage mimeMsg; //MIME邮件对象
//设置系统属性
props = java.lang.System.getProperties(); //获得系统属性对象
props.put("mail.smtp.host",smtp); //设置SMTP主机
//获得邮件会话对象
mailSession = javax.mail.Session.getDefaultInstance(props,null);
//创建MIME邮件对象
mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
//设置发信人
mimeMsg.setFrom(new javax.mail.internet.InternetAddress(from));
//设置收信人
if(to!=null){
mimeMsg.setRecipients(javax.mail.Message.RecipientType.TO,javax.mail.internet.InternetAddress.parse(to));
}
//设置抄送人
if(cc!=null){
mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC,javax.mail.internet.InternetAddress.parse(cc));
}
//设置暗送人
if(bcc!=null){
mimeMsg.setRecipients(javax.mail.Message.RecipientType.BCC,javax.mail.internet.InternetAddress.parse(bcc));
}
//设置邮件主题
//mimeMsg.setSubject(subject);
mimeMsg.setSubject(subject,"gb2312");
//设置邮件内容
mimeMsg.setDataHandler(new javax.activation.DataHandler(new pipi.jaf.StringDataSource(body,"text/html")));
//发送邮件
javax.mail.Transport.send(mimeMsg);
}
}
本套程序使用到JavaMail和JAVABEANS(TM) ACTIVATION FRAMEWORK(JAF):
JavaMail
http://java.sun.com/products/javamail/
JAVABEANS(TM) ACTIVATION FRAMEWORK(JAF)
http://java.sun.com/products/javabeans/glasgow/jaf.html
Top
4 楼ebstar(大胡子)回复于 2002-07-08 15:50:17 得分 0
有没有已经编译好的beans文件,请发到我的邮箱:bimzhou@263.netTop
5 楼Andrawu(晓彬)回复于 2002-07-08 16:05:44 得分 0
http://www.csdn.net/expert/topic/784/784039.xml?temp=6.794375E-02Top
6 楼beyond_xiruo(CorruptionException)回复于 2002-07-08 16:16:02 得分 50
已经发送
三个文件,两个是编译好的class,mailtest.jsp是测试文件Top
7 楼ebstar(大胡子)回复于 2002-07-08 17:14:48 得分 0
To:beyond_xiruo(希偌)
能不能把mailtest.jsp文件写全了,引入、调用等Top
8 楼ebstar(大胡子)回复于 2002-07-08 17:41:27 得分 0
to:beyond_xiruo(希偌)
mailtest.jsp文件不完整,没有部署和引入调用beans的部分,难道你真的试过。。。。。。
Top





