Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7598363 建立时间:2006年5月29日 |

| |
[Apache(jakarta)]用Commons-Email来发邮件 软件技术
lhwork 发表于 2006/8/23 9:54:20 |
Commons-Email项目旨在提供邮件发送的API,它是在JavaMail API的基础上构建的。
Some of the mail classes that are provided are as follows:
SimpleEmail - This class is used to send basic text based emails. MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached. HtmlEmail
- This class is used to send HTML formatted emails. It has all of the
capabilities as MultiPartEmail allowing attachments to be easily added.
It also supports embedded images. EmailAttachment
- This is a simple container class to allow for easy handling of
attachments. It is for use with instances of MultiPartEmail and
HtmlEmail.
下面是几个使用例子:
1. 发送简单的文本邮件
SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
2. 发送附件
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("mypictures/john.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("John");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// add the attachment
email.attach(attachment);
// send the email
email.send();
3. 发送HTML邮件
// Create the email message
HtmlEmail email = new HtmlEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test email with inline image");
// embed the image and get the content id
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = email.embed(url, "Apache logo");
// set the html message
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");
// send the email
email.send(); |
|
|