waterye

use jakarta-commons email

requires jar: commons-email-1.0.jar, mail.jar, activation.jar

1. send text mail
SimpleEmail email = new SimpleEmail();
email.setHostName(
"211.154.104.29");
email.setAuthentication(
"water@itorgan.com", password);
email.addTo(
"waterye@gmail.com""Water Ye");
email.setFrom(
"water@itorgan.com""Water Ye");
email.setSubject(
"Test message");
email.setMsg(
"This is a simple test of commons-email");
email.send();
中文问题:
// email.setMsg("测试邮件");
email.setCharset("UTF-8");
email.setContent(
"测试邮件""text/plain;charset=GBK");
SimpleEmail封得太过简单, 看代码就知道了.

2. Sending email with attachments
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(
"C:/mail/hello.groovy");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(
"hello.groovy");
attachment.setName(
"hello.groovy");

// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName(
"211.154.104.29");
email.setAuthentication(
"water@itorgan.com", password);
email.addTo(
"waterye@gmail.com""Water Ye");
email.setFrom(
"water@itorgan.com""Water Ye");
email.setSubject(
"hello groovy");
email.setMsg(
"groovy hello world");

email.attach(attachment);   
// add the attachment
email.send();   // send the email

3. send html email
// Create the email message
HtmlEmail email = new HtmlEmail();
email.setHostName(
"211.154.104.29");
email.setAuthentication(
"water@itorgan.com", password);
email.addTo(
"waterye@gmail.com""Water Ye");
email.setFrom(
"water@itorgan.com""Water Ye");
email.setSubject(
"Test email with inline image");

// embed the image and get the content id
URL url = new URL("http://www.itorgan.com/images/index/top1.gif");
String cid 
= email.embed(url, "Itorgan logo");

email.setHtmlMsg(
"<html>The itorgan logo - <img src=\"cid:" + cid + "\"></html>"); // set the html message

email.setTextMsg(
"Your email client does not support HTML messages"); // set the alternative message

email.send();


martin xus已写过, 就不发布到首页了

posted on 2005-09-30 16:42 waterye 阅读(566) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航: