当前位置: 技术问答>java相关
有可以发html的mail组件吗?
来源: 互联网 发布时间:2015-01-26
本文导语: 我要做一个电子报,要求可以发给很多用户,而且可以发html格式的,有这样的东东吗? | form.htm ======== 邮件例程 - JavaMail - 发送HTML邮件 SMTP主机: 发信人:...
我要做一个电子报,要求可以发给很多用户,而且可以发html格式的,有这样的东东吗?
|
form.htm
========
邮件例程 - JavaMail - 发送HTML邮件
SMTP主机:
发信人:
收信人:
抄送人:
暗送人:
主题:
内容:
send.jsp
========
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
========
邮件例程 - JavaMail - 发送HTML邮件
SMTP主机:
发信人:
收信人:
抄送人:
暗送人:
主题:
内容:
send.jsp
========
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
|
要设classpath
|
.jar放在什么地方都无所谓,关键在于要在环境变量中设
classpath=your .jar pathyour .jar;
classpath=your .jar pathyour .jar;
|
java.sun.com/products/javamail
|
在jguru上有个FAQ,不过我没试过 :)
Using the JavaMail API from JSP pages requires the JavaMail and JavaBeans Activation Framework JAR files (mail.jar and activation.jar respectively) to be in the CLASSPATH for the Java runtime of your JSP-enabled web server. While most servers allow you to configure their CLASSPATH, either through mucking with batch files or through some configuration program, the easiest way to configure the server is to take advantage of the standard Java Extensions Mechanism. Just copy the JAR files into the ext under your Java runtime directory. For instance, Windows users of Java 1.2.2 who installed to the default location would copy the JAR files to C:JDK1.2.2JRELIBEXT.
Using the JavaMail API from JSP pages requires the JavaMail and JavaBeans Activation Framework JAR files (mail.jar and activation.jar respectively) to be in the CLASSPATH for the Java runtime of your JSP-enabled web server. While most servers allow you to configure their CLASSPATH, either through mucking with batch files or through some configuration program, the easiest way to configure the server is to take advantage of the standard Java Extensions Mechanism. Just copy the JAR files into the ext under your Java runtime directory. For instance, Windows users of Java 1.2.2 who installed to the default location would copy the JAR files to C:JDK1.2.2JRELIBEXT.