Commons-Email发送邮件(翻译自官网)
来源:互联网 发布:黑五值得买 知乎 编辑:程序博客网 时间:2024/06/05 18:31
所需jar包:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.3.1</version></dependency>
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version></dependency>
1、发送简单文本
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地址,也可以在system.property系统变量mail.host中设置
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的附件,当邮件发送时会自动进行附件下载,以下是例子
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();
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();
embed()返回值为一个随机生成的字符串,用来表示引用的图片标签;同时相应的取消了setmsg ,改成 setHtmlMsg与setTextMsg区分html与纯文本
4、发送格式为html及内嵌附件
解决上3中需要提供cid并拼接图片的问题,使用模板化html ImageHtmlEmail 发出,解决了需要将外部图片转换为inline内联图片的问题
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();
首先我们声明了一个html模板并包含图片url,所有图片都会自动转化为内联图片,通过DataSourceResolver解析器
最后附上自己使用的例子:
HtmlEmail email = new HtmlEmail (); email.setHostName("mail.xxxx.com"); email.setAuthentication("leo.li@xxxx.com", "1qaz!QAZ"); email.setCharset("UTF-8"); try { String[] receivers = new String[]{"leo.li@xxxx.com", "zs.zhou@xxxx.com"}; for (String receiver : receivers) { email.addTo(receiver.trim()); } email.setFrom("leo.li@xxxx.com", "leoeleoeleole"); email.setSubject("test"); email.setMsg("<html><div style=\"background:red;\">草草草擦凑啊</div></html>");List<String> list = new ArrayList<String>(); list.add("C:\\Users\\Administrator\\Desktop\\0010164 xxxx表.xls"); if (!CollectionUtils.isEmpty(list)) { for (String attachment : list) { EmailAttachment attach = new EmailAttachment(); File f = new File(attachment); attach.setName(MimeUtility.encodeText(f.getName())); attach.setPath(attachment); email.attach(attach); } } String result = email.send();
0 0
- Commons-Email发送邮件(翻译自官网)
- commons-email 发送邮件
- 通过commons-email发送邮件
- Apache-commons-email发送邮件
- 使用commons email发送邮件
- Apache Commons Email邮件发送
- 使用Apache Commons-email组件发送邮件
- 使用Apache Commons-email组件发送邮件
- 利用Apache commons-email组件发送邮件
- apache commons-email发送邮件乱码问题
- commons-email 轻松发送无乱码邮件
- SCA构建commons-email发送邮件服务
- 利用Commons-email组件发送邮件
- 基于commons-email,web端发送邮件
- 利用Apache Commons Email发送邮件
- 利用apache commons-email发送邮件
- Commons: 封装Apache Commons Email简化邮件发送
- 用apache的Mail包(commons-email-1.2.jar),发送邮件
- PHP 强大IDE vim版phpstrom配置 (第二版)
- 软键盘的弹出隐藏监听及软键盘高度
- 原码, 反码, 补码 详解
- fresco源码分析-内存回收
- 5款常用移动Html5场景页面制作工具推荐
- Commons-Email发送邮件(翻译自官网)
- iOS开发深刻理解MVC模式
- 把数组排成最小的数
- Linux下软件源码包安装问题解决方法
- 技术分享】CSV/XLS Injection Vulnerability 分析利用和YY
- 鱼眼图像畸变校正——透视变换
- 联合:节省空间的类
- js PC QQ拖拽效果
- JAVA动态代理(JDK和CGLIB)