原文地址: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();
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com