java - HtmlEmail发送html邮件
2011-09-13 16:23
399 查看
之前没做过用JavaMail来发送邮件,在网上搜索点资料,自己写了2方法测试了下:
在自己的项目中,引入三个包 mail.jar ,commons-email-X.X.jar ,activation.jar。下载地址
http://u.115.com/file/bhaewa0d
下面是我的代码类:
public class EmailMain {
/**
* 方法测试一个简单拼凑的html
*/
public static void send() {
try {
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.163.com");// 发送方smtp
email.setAuthentication("username", "password");//发送方的邮箱用户名,密码
email.addTo("address", "username");//接受方的邮箱地址,邮箱用户名
email.setFrom("address@163.com", "username");//发送方的邮箱地址,邮箱用户名
email.setSubject("主题:该邮件包括html格式内容");
email.setCharset("UTF-8");// 编码格式
// 注意这里:embed 将帮助我们创建标签如:cid:xxx url
URL url = new URL("http://www.redbutton.cn/rb/images/3q.jpg");
String cid = email.embed(url, "Apache logo");
/**
* 我们看到HtmlEmail extends
* Email的,它依然有setMsg(),但是这里发送的邮件包括了插入在邮件内容中的图片,
* 所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代码
**/
email.setHtmlMsg("<html>发送第一封邮件,The apache logo - <img src=\"cid:"
+ cid + "\"></html>");
// set the alternative message
email
.setTextMsg("Your email client does not support HTML messages");
// set mail
email.send();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试读取本地html页面,发送邮件(页面内容过多,最好用此方法)
*/
public static void sendFileContext() {
try {
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.163.com");// 发送方smtp
email.setAuthentication("username", "password");//发送方的邮箱用户名,密码
email.addTo("address", "username");//接受方的邮箱地址,邮箱用户名
email.setFrom("address@163.com", "username");//发送方的邮箱地址,邮箱用户名
email.setSubject("主题:该邮件包括html格式内容");
File file = new File(
"D:\\Documents and Settings\\wanghuijiao\\textSendEmail.jsp");
BufferedReader reader = null;
StringBuilder msg = new StringBuilder("");
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
msg.append(tempString);
}
} catch (Exception e) {
}
String message = msg.toString();
message = new String(message.getBytes("utf-8"), "ISO-8859-1");// 处理中文乱码转码
email.setHtmlMsg(message);
email.send();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] arg) {
sendFileContext();
}
textSendEmail.jsp內容如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <body> <div id="zxgg_div" style="text-align: center;"> <h1>测试成功了,wowo?~~~~~~</h1>
<img id="gg_bb" src="http://www.redbutton.cn/rb/images/3q.jpg"/>
<a style="color:red" href="http://www.redbutton.cn/rb/contact.html">www.redbutton.cn</a>
</div> </body> </html>
}
在自己的项目中,引入三个包 mail.jar ,commons-email-X.X.jar ,activation.jar。下载地址
http://u.115.com/file/bhaewa0d
下面是我的代码类:
public class EmailMain {
/**
* 方法测试一个简单拼凑的html
*/
public static void send() {
try {
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.163.com");// 发送方smtp
email.setAuthentication("username", "password");//发送方的邮箱用户名,密码
email.addTo("address", "username");//接受方的邮箱地址,邮箱用户名
email.setFrom("address@163.com", "username");//发送方的邮箱地址,邮箱用户名
email.setSubject("主题:该邮件包括html格式内容");
email.setCharset("UTF-8");// 编码格式
// 注意这里:embed 将帮助我们创建标签如:cid:xxx url
URL url = new URL("http://www.redbutton.cn/rb/images/3q.jpg");
String cid = email.embed(url, "Apache logo");
/**
* 我们看到HtmlEmail extends
* Email的,它依然有setMsg(),但是这里发送的邮件包括了插入在邮件内容中的图片,
* 所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代码
**/
email.setHtmlMsg("<html>发送第一封邮件,The apache logo - <img src=\"cid:"
+ cid + "\"></html>");
// set the alternative message
.setTextMsg("Your email client does not support HTML messages");
// set mail
email.send();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试读取本地html页面,发送邮件(页面内容过多,最好用此方法)
*/
public static void sendFileContext() {
try {
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.163.com");// 发送方smtp
email.setAuthentication("username", "password");//发送方的邮箱用户名,密码
email.addTo("address", "username");//接受方的邮箱地址,邮箱用户名
email.setFrom("address@163.com", "username");//发送方的邮箱地址,邮箱用户名
email.setSubject("主题:该邮件包括html格式内容");
File file = new File(
"D:\\Documents and Settings\\wanghuijiao\\textSendEmail.jsp");
BufferedReader reader = null;
StringBuilder msg = new StringBuilder("");
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
msg.append(tempString);
}
} catch (Exception e) {
}
String message = msg.toString();
message = new String(message.getBytes("utf-8"), "ISO-8859-1");// 处理中文乱码转码
email.setHtmlMsg(message);
email.send();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] arg) {
sendFileContext();
}
textSendEmail.jsp內容如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <body> <div id="zxgg_div" style="text-align: center;"> <h1>测试成功了,wowo?~~~~~~</h1>
<img id="gg_bb" src="http://www.redbutton.cn/rb/images/3q.jpg"/>
<a style="color:red" href="http://www.redbutton.cn/rb/contact.html">www.redbutton.cn</a>
</div> </body> </html>
}
相关文章推荐
- Java HtmlEmail 邮件发送的简单实现代码
- java HtmlEmail发送邮件工具类
- Java HtmlEmail 使用QQ邮箱发送邮件报错 530 Error: A secure connection is requiered(such as ssl)
- Java - HtmlEmail 邮件发送
- EBS中使用JAVA方式发送HTML格式邮件
- 用Java发送图文并茂的HTML邮件
- android使用javaemail发送邮件常见错误
- 解决用Email发送HTML邮件显示乱码问题,并用大字符串类型:StringBuilder
- JAVA EMAIL发送邮件 参数可配置,模板可配置
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html
- Spring发送邮件-java发送email
- Apache公司的HtmlEmail发送邮件
- java实现发送邮件可多个附件内嵌图片-commons-email使用(未测试)
- EBS中使用JAVA方式发送HTML格式邮件
- Java使用Velocity模板发送HTML格式邮件并解决中文乱码问题
- java调用html模板发送html内容的邮件
- Java发送带html标签内容的邮件实例代码
- Java -发送邮件 email
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
- 总结一下java email发送邮件