`
lianshisheng
  • 浏览: 27648 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java发email

    博客分类:
  • Java
阅读更多

一个简单的文本邮件

我们的第一个例子是利用你本地的邮件服务器向"John Doe"发送一个基本邮件信息。

import org.apache.commons.mail.SimpleEmail;

...

  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();

调用setHostName("mail.myserver.com")来设置发送信息的SMTP服务器地址。如果你不设置,将会使用系统属性"mail.host"。

○发送带附件的邮件

为了向一个邮件添加附件,你需要使用MultiPartEmail这个类。这个类的工作方式和SimpleEmail类似,但是其重载了attach()方法使其可以向邮件添加附件。你可以通过附加或者内联来添加无限数量的附件,这些附件将采用MIME编码。

最简单的方式是使用EmailAttachment类引用你得附件来添加附件。

下面的例子我们将创建一个图片的附件。把图片附加在邮件中并发送它。

import org.apache.commons.mail.*;

...

  // 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();

你也可以使用EmailAttachment引用一个非本地的URL文件。当发送邮件时,URL文件会被自动下载下来附件在邮件中。

下面的例子演示如何把apache的徽标附件在邮件中发送给John 。

import org.apache.commons.mail.*;

...

  // Create the attachment

  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");

  // 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 logo");

  email.setMsg("Here is Apache's logo");

  

  // add the attachment

  email.attach(attachment);

  // send the email

  email.send();

 

○发送HTML格式的邮件

发送HTML格式邮件要使用HtmlEmail类。这个类的工作方式很像使用附加方法设置html内容的MultiPartEmail类类似,如果接收者不支持HTML邮件将替换成普通的文本和内联图片。

在下面例子中,我们使用HTML格式内容和以俄国内联图像。

import org.apache.commons.mail.HtmlEmail;

...

  // 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();

首先,注意,调用embed函数返回一个字符串。这个字符串是一个随机的标识符用来引用一个图像标签中的图像。下一步,在这个例子中我们没有调用setMsg()函数。这个方法在HtmlEmail中仍然可用,但是如果你使用内联图像时就不应该使用,应该使用setHtmlMsg()函数和setTextMsg()函数。

○调试

JavaMail API支持一个调试选项,如果你遇到了问题这个可能非常有用。你可以通过调用setDebug(true)来激活调试模式,调试信息将被输出到标准输出中。

○验证

如果SMTP服务器需要验证,你可以在发送邮件前调用setAuthentication(userName,password)设置帐号和密码。这个将会创建一个DefaultAuthenticator实例,JavaMail API在发送邮件时会使用它。你的服务器必须支持RFC2554标准。

你可以执行更复杂的验证方法,例如通过创建javax.mail.Authenticator对象的子类向用户显示一个对话框。你需要覆盖getPasswordAuthentication()方法来处理用户的信息。要使用新的Authenticator类,需要使用Email.setAuthenticator方法。

○处理退回信息

通常,不能被传输的邮件将被返回给发送者。然而,某些情况,你想把它发送到一个不同的地址,你只需在发送邮件前调用setBounceAddress(emailAddressString)方法。

技术提示: 当SMTP服务器不能传输邮件时,它们不会关心邮件的信息来决定错误信息发送到哪里,而是引用SMTP "envelope sender"的值。JavaMail基于在JavaMail Session上的mail.smtp.from属性来设置这个值。 (Commons Email使用System.getProperties()初始化JavaMail Session) 如果这个属性没有设置,JavaMail使用"from"地址.如果你的邮件已经设置了 bounceAddress 属性, 当Session初始化的时候Commons Email使用它来设置mail.smtp.from的值,会覆盖你曾经对mail.smtp.from的设置。

 

提示:这个仅是用来处理返回邮件的方法。特别地,"Errors-to:" SMTP头是被废弃的,用来控制退回信息是不被信任的。也要注意,使用一个不受信任的"from"地址被认为是一个最差的方式,除非你也设置了返回邮件地址。如果你的应用程序允许用户在邮件中输入"from"地址,你应用设置一个受信任的退回地址。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics