[Java] JavaMail 发送 html 格式、带附件的邮件
本案例演示发送 html 格式,可带附件的邮件发送。发送纯文本邮件的例子可参照上一篇博文JavaMail 简单案例。
EmailHelper, Email 的帮助类,向帮助类提供 SMTP 服务器域名、用户名、密码、发送人邮箱、收件人邮箱、邮件主题、html 格式的内容(可选)、附件(可选),便可发送一份邮件。
SendEmailDemo, 演示发送邮件。
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; public class EmailHelper { private String host;
private String username;
private String password;
private String from; private String to;
private String subject;
private String htmlContent;
private String attachedFileName; public EmailHelper(String host, String username, String password, String from) throws AddressException, MessagingException{
this.host = host;
this.username = username;
this.password = password;
this.from = from;
} public void send() throws Exception{ Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host); final String username1 = username;
final String password1 = password; Session session = Session.getInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username1, password1);
}
}); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); Multipart multipart = new MimeMultipart(); if (htmlContent != null){
System.out.println(" has html "); BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlContent, "text/html");
multipart.addBodyPart(htmlPart);
} if (attachedFileName != null){
System.out.println(" has attached file "); BodyPart attchmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachedFileName);
attchmentPart.setDataHandler(new DataHandler(source));
attchmentPart.setFileName(attachedFileName);
multipart.addBodyPart(attchmentPart);
} message.setContent(multipart);
Transport.send(message); System.out.println(" Sent ");
} public void setTo(String to) {
this.to = to;
} public void setSubject(String subject) {
this.subject = subject;
} public void setHtmlContent(String htmlContent) {
this.htmlContent = htmlContent;
} public void setAttachedFileName(String attachedFileName) {
this.attachedFileName = attachedFileName;
}
}
演示代码,
public class SendEmailDemo {
public static void main(){
String host = "smtp.163.com"; // use your smtp server host
final String username = "sender@163.com"; // use your username
final String password = "password"; // use your password
String from = "sender@163.com"; // use your sender email address
String to = "reciever@foxmail.com"; // use your reciever email address
try {
EmailHelper emailHelper = new EmailHelper(host, username, password, from);
emailHelper.setTo(to);
emailHelper.setSubject("subject ttt test");
emailHelper.setHtmlContent("<h1> This is html </h1>");
emailHelper.setAttachedFileName("/Users/grs/Documents/Java/mavenEmail/test/src/main/resource/attachment3.txt");
emailHelper.send();
} catch (Exception e) {
e.printStackTrace();
}
}
}
需要读取邮箱邮件,可参考另一篇博文 JavaMail 查询邮件。
参考资料
[Java] JavaMail 发送 html 格式、带附件的邮件的更多相关文章
- python+selenium实现发送一封带附件的邮件
163邮件登录首页 登录成功断言是否有退出按钮 点击退出退出登录 from selenium import webdriver import unittest import time class Vi ...
- [Java] JavaMail 发送带图片的 html 格式的邮件
JavaMail 发送的邮件正文和附件是相互独立的,但是内置图片需要定位图片在正文中的位置,所以内置图片和邮件正文是互相依赖的. 发送带附件的邮件可参考JavaMail 发送 html 格式.带附件的 ...
- 使用JavaMail发送带附件的邮件
所需jar包 链接:http://pan.baidu.com/s/1dFo4cDz 密码:akap 工具类: package com.javamail.utils; import java.util. ...
- java发送带附件的邮件
/** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...
- [SpringBoot] - 发送带附件的邮件
<!--发送email依赖--> <dependency> <groupId>org.springframework.boot</groupId> &l ...
- 利用Python+163邮箱授权码发送带附件的邮件
背景 前段时间写了个自动爬虫的脚本,定时在阿里云服务器上执行,会从某个网站上爬取链接保存到txt文本中,但是脚本不够完善,我需要爬虫完毕之后通过邮件把附件给我发送过来,之前写过一个<利用Pyth ...
- EBS中使用JAVA方式发送HTML格式邮件
转自huan.gu专栏:http://blog.csdn.net/gh320/article/details/17174769 EBS中使用JAVA方式发送HTML格式邮件 一.开发工具:JDevel ...
- C#发送带附件的邮件的代码
如下的代码是关于C#发送带附件的邮件的代码. MailMessage m = new MailMessage();m.Subject = "File attachment!";m. ...
- [Xcode 实际操作]八、网络与多线程-(7)使用MessageUI框架,创建并发送一封带有附件的邮件
目录:[Swift]Xcode实际操作 本文将演示如何使用MessageUI框架,创建并发送一封带有附件的邮件. 使用邮件编辑视图控制器(MFMailComposeViewController)实现邮 ...
随机推荐
- Android之提交数据到服务端方法简单封装
在Android应用中,除了单机版的应用,其余的应用免不了需要频繁地与服务端进行数据交互,如果每一种方法都独立写一段代码,那会造成代码大量重复,冗余,这不是我们所希望的,所以我们可以对其进行一些封装, ...
- mysql locktables
SELECT r.trx_id waiting_trx_id, r.trx_mysql_thread_id waiting_thread, TIMESTAMPDIFF( ...
- SQL SERVER 存储过程基础
一.注释 -- 单行注释,从这到本行结束为注释,类似C++,c#中// /* … */ 多行注释,类似C++,C#中/* … */ 二.变量 (int, smallint, tinyint, deci ...
- 自己动手写一个iOS 网络请求库的三部曲[转]
代码示例:https://github.com/johnlui/Swift-On-iOS/blob/master/BuildYourHTTPRequestLibrary 开源项目:Pitaya,适合大 ...
- Java Swing 使用总结(转载)
随笔转载自:此去经年ぢ 地址:http://www.cnblogs.com/FLFL/p/5369756.html 1. GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在 ...
- C++中extern关键字使用(转)
参考文章:http://blog.csdn.net/sruru/article/details/7951019 chapter1.如何混合编译C语言和C++ 实际开发过程中,C++中会调用C与语言编写 ...
- POJ 1236.Network of Schools (强连通)
首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...
- 将fastjson元素转化为String[]
在fastjson中如果JSONObject中添加了 String[] 类型的元素 例如 JSONObject jo = new JSONObject(); String[] array = {&qu ...
- phpcms(3) V9 常用函数 及 代码整理(转)
转自http://www.cnblogs.com/Braveliu/p/5103918.html 常用函数 及 常用代码 总结如下 <;?php //转换字符串或者数组的编码 str_chars ...
- Content-Type实体首部字段
现代互联网下,每天都会有数以亿计的各种媒体对象经由HTTP传输,如图像,文本,影视以及软件程序等.这些数据都包含在HTTP报文的实体内容中,如果把HTTP报文想像成一份快递,HTTP实体就是快递实 ...