您的位置:首页 > 运维架构 > Apache

Apache Common Email的几个例子

2016-09-11 22:40 302 查看

Apache Common Email的几个例子

最近用到了发邮件,于是就看到了JavaMail和Apache Common Email,先开始试的是JavaMail,后来发现一个不知道认证失败的错误,于是干脆就直接使用Common Email,觉得还是挺简单容易使用的。一般只用第一种,记录下来。以后有需要了再来看

最简单的Text mail

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


Sending emails with attachments

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


Attachment 也可以指向一个合法的url,发送时就会把这个url内容给download下来进行发送.

Sending HTML formatted email

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


Sending HTML formatted email with embedded images

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

// load your HTML email template
String htmlEmailTemplate = ".... <img src=\"http://www.apache.org/images/feather.gif\"> ....";

// define you base URL to resolve relative resource locations
URL url = new URL("http://www.apache.org");

// create the email message
ImageHtmlEmail email = new ImageHtmlEmail();
email.setDataSourceResolver(new DataSourceUrlResolver(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");

// set the html message
email.setHtmlMsg(htmlEmailTemplate);

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

// send the email
email.send();


PS:

在本地使用common mail的时候没有任何问题,但是放到了服务器不知道为什么就一直报connection reset,通过telnet手动发stmp的命令已经成功发送了邮件,无奈只能换回了JavaMail,没有出错。可能是某一项没有设置导致的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: