使用Apache的Commons Email发送邮件
首先在pom中添加依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.2</version>
</dependency>
1、发送简单邮件:
Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setTLS(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();
2、发送带附件的邮件:
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();
3、发送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();
4、发送带图片的HTML邮件:
import org.apache.commons.mail.HtmlEmail;
...
// load your HTML email template
String htmlEmailTemplate = ....
// Create the email message
HtmlEmail email = new ImageHtmlEmail();
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(htmlEmailTemplate, new File("").toURI().toURL(), false);
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
引用
分享到:
相关推荐
java发送Email邮件
demo介绍:http://blog.csdn.net/love_xiolan/article/details/56016576 java发送email邮件 1.发送普通email邮件 2.发送带附件的email邮件 3.邮件抄送和密送 4.发送html邮件 5.发送模板文件
支持多人发送,解决了发送邮件时乱码问题.
需要依赖jar: <groupId>javax.mail <artifactId>mail <version>1.4.7 <groupId>javax.activation <artifactId>activation <version>1.1.1 </dependency>
用Java 发送Email邮件
java发送电子邮件,程序和jar包都在里面,使用方法也有,程序可直接使用,直接改成自己的邮箱和密码就能发送了,解决了很多人java发送邮件不成功的烦恼
用java开发Email工具之发送邮件
使用java发送邮件的方法,直接可以拿来用,最近找了个例子,稍微整理了一下
java实现的根据html模版内容发送邮件,这是一个完整的eclipse项目,包含jar包,导入即可运行
满足所有Java邮件Email发送所需的Jar包。
java发送邮件,可以带附件,java技术不是很好,写的不是很好,还请大家见谅!
发送email邮件的java源码 发送email邮件的java源码发送email邮件的java源码
该程序包为完整程序包,通过java编程来实现邮件的发送功能,导入Myeclipse或Eclipse后可直接运行测试
java中email邮件发送接受和处理excel文件(xls)实例 实现2种功能的例子。值得一看
java 实现发送邮件功能,内含网页版发送邮件,校验验证码功能。希望能帮助大家
java邮件发送例子 apache email demo
实现java发送邮件功能,以正式gmail,hotmail等主流邮箱邮件都可接收到
用java发送电子邮件的最简单实例,包括jar
如果 用myEclipse进行开发时,如果程序中需要发送邮件,运行时可能会出现以下的异常: Exception in thread "main" java.lang.NoClassDefFound Error: com/sun/mail/util/LineInputStream 原因:邮件发送的jar包...
java发送邮件的包email.jar java发送邮件的包email.jar java发送邮件的包email.jar