现在的位置: 首页 > 综合 > 正文

Commons-Email收发邮件[apache]

2013年03月19日 ⁄ 综合 ⁄ 共 3561字 ⁄ 字号 评论关闭

原文地址:http://commons.apache.org/proper/commons-email/userguide.html

一个简单的文本电子邮件

Our first example will create a basic email message to "John Doe" and send it through your Google Mail (GMail) account.

我们的的第一个例子中将创建一个基本的电子邮件到"John Doe",并通过您的Google的邮件上(GMail的)帐户发送给它。

Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

调用setHostName(“mail.myserver.com”)设置发送邮件的SMTP服务器将用于发送邮件的地址。如果不设置,系统属性的“mail.host”将被使用。

发送带附件的邮件

将附件添加到电子邮件中,你会需要使用MultiPartEmail类。这个类的工作就像SimpleEmail的,除了它增加了几个重载attach()方法将附件添加到电子邮件。您可以添加无限数量的附件,无论是内嵌或附加。将进行MIME编码的附件。
添加附件,最简单的方法是通过使用的EmailAttachment类引用您的附件。
在下面的例子中,我们将创建一个附件图片。然后,我们将附加到电子邮件中的图片,并将其发送。

import org.apache.commons.mail.*;
...

  // 创建附件
  EmailAttachment attachment = new EmailAttachment();
  attachment.setPath("mypictures/john.jpg");
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  attachment.setDescription("Picture of John");
  attachment.setName("John");

  // 创建电子邮件
  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");

  // 添加附件
  email.attach(attachment);

  // 发送邮件
  email.send();

您也可以使用任何有效的URL EmailAttachment参考,你不必在本地的文件。发送消息时,该文件会被下载并自动附加到邮件中。
下一个例子显示我们如何能够将apache的标志发送给“John”。

import org.apache.commons.mail.*;
...

  // 创建附件
  EmailAttachment attachment = new EmailAttachment();
  attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  attachment.setDescription("Apache logo");
  attachment.setName("Apache logo");

  // 创建电子邮件
  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 logo");
  email.setMsg("Here is Apache's logo");
  
  // 添加附件
  email.attach(attachment);

  // 发送电子邮件
  email.send();

发送HTML格式的邮件

发送HTML格式的邮件是通过使用HtmlEmail类。这个类可以通过MultiPartEmail类的其他方法来设置HTML内容,替代文字内容,如果收件人不支持HTML电子邮件,并且添加内嵌图像。

在这个例子中,我们将发送一封电子邮件,内嵌图像与格式化的HTML内容。

import org.apache.commons.mail.HtmlEmail;
...

  // 创建一个电子邮件
  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");
  
  // 嵌入图像
  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
  String cid = email.embed(url, "Apache logo");
  
  // 设置HTML内容
  email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

  // 设置Text
  email.setTextMsg("Your email client does not support HTML messages");

  // 发送邮件
  email.send();

首先,请注意 embed() 调用返回一个String。这个String是一个引用图像在图像标签,必须使用随机生成的标识符。
接着,这里并没有使用setMsg()在这个例子中。方法仍然是可用的在HtmlEmail,但它不应该被使用,如果你将要使用内嵌图像。相反,setHtmlMsg()和setTextMsg() 方法被使用。

带有嵌入式图像的HTML格式的电子邮件发送

前面的示例演示如何创建一个带有嵌入式图像的HTML电子邮件,但使用HTML电子邮件模板时,你需要知道的所有图像的前期,这是不方便的。ImageHtmlEmail帮助你解决这个问题,将所有外部图像内嵌图像。
import org.apache.commons.mail.HtmlEmail;
...

  
  String htmlEmailTemplate = ....

  // 定义基础URL来解决资源相对位置
  URL url = new URL("http://www.apache.org");

  // 创建电子邮件
  HtmlEmail email = new ImageHtmlEmail();
  email.setDataSourceResolver(new DataSourceResolverImpl(url));
  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");
  
  // 设置HTML内容
  email.setHtmlMsg(htmlEmailTemplate);

  
  email.setTextMsg("Your email client does not support HTML messages");

  // 发送邮件
  email.send();

抱歉!评论已关闭.