javax.mail用smtp服务器发送带附件的邮件
jar包:
javax.mail-1.5.5.jar
maven配置:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
代码:
package com.test; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; import org.apache.log4j.Logger;
import com.test.MailAuthenticator; public class SendMail { // 日志记录
private static Logger logger = Logger.getLogger(SendMail.class); public static MailAuthenticator authenticator;
private MimeMessage message;
private Session session;
private Transport transport;
private Properties properties = new Properties(); private String mailHost = null;
private String sender_username = null;
private String sender_password = null; /**
* 构造方法
*/
public SendMail() {
super();
} /**
* 供外界调用的发送邮件接口
*/
public boolean sendEmail(String title, String content, List<String> receivers, List<File> fileList) {
try {
// 初始化smtp发送邮件所需参数
initSmtpParams(); // 发送邮件
doSendHtmlEmail(title, content, receivers, fileList); } catch (Exception e) {
logger.error(e);
}
return true;
} /**
* 初始化smtp发送邮件所需参数
*/
private boolean initSmtpParams() { mailHost = "邮箱smtp服务器"; // 邮箱类型不同值也会不同
sender_username = "发件人邮箱";
sender_password = "发件人邮箱密码"; properties.put("mail.smtp.host", mailHost);// mail.envisioncitrix.com
properties.put("mail.smtp.auth", "true");
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.checkserveridentity", "false");
properties.put("mail.smtp.ssl.trust", mailHost); authenticator = new MailAuthenticator(sender_username, sender_password);
session = Session.getInstance(properties, authenticator);
session.setDebug(false);// 开启后有调试信息
message = new MimeMessage(session); return true;
} /**
* 发送邮件
*/
private boolean doSendHtmlEmail(String title, String htmlContent, List<String> receivers, List<File> fileList) {
try {
// 发件人
InternetAddress from = new InternetAddress(sender_username);
message.setFrom(from); // 收件人(多个)
InternetAddress[] sendTo = new InternetAddress[receivers.size()];
for (int i = 0; i < receivers.size(); i++) {
sendTo[i] = new InternetAddress(receivers.get(i));
}
message.setRecipients(MimeMessage.RecipientType.TO, sendTo); // 邮件主题
message.setSubject(title); // 添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart(); // 添加邮件正文
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(htmlContent, "text/html;charset=UTF-8");
multipart.addBodyPart(contentPart); // 遍历添加附件
if (fileList != null && fileList.size() > 0) {
for (File file : fileList) {
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(file.getName());
multipart.addBodyPart(attachmentBodyPart);
}
} // 将多媒体对象放到message中
message.setContent(multipart); // 保存邮件
message.saveChanges(); // SMTP验证,就是你用来发邮件的邮箱用户名密码
transport = session.getTransport("smtp");
transport.connect(mailHost, sender_username, sender_password); // 发送邮件
transport.sendMessage(message, message.getAllRecipients()); System.out.println(title + " Email send success!");
} catch (Exception e) {
logger.error(e);
} finally {
if (transport != null) {
try {
transport.close();
} catch (MessagingException e) {
logger.error(e);
}
}
}
return true;
} /**
* 测试main
*/
public static void main(String[] args) {
// 邮件主题
String title = "邮件主题"; // 邮件正文
String htmlContent = "邮件内容"; // 收件人
List<String> receivers = new ArrayList<String>();
receivers.add("收件人邮箱1");
receivers.add("收件人邮箱2"); // 附件
String fileName1 = "附件路径1";
File file1 = new File(fileName1);
String fileName2 = "附件路径2";
File file2 = new File(fileName2);
List<File> fileList = new ArrayList<File>();
fileList.add(file1);
fileList.add(file2); // 执行发送
new SendMail().sendEmail(title, htmlContent, receivers, fileList);
} }
代码:
package com.test; import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication; /**
* 通过账号密码进行身份验证
*/
public class MailAuthenticator extends Authenticator {
private String userName;
private String password; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
} public MailAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
}
javax.mail用smtp服务器发送带附件的邮件的更多相关文章
- java发送带附件的邮件
/** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...
- 利用Python+163邮箱授权码发送带附件的邮件
背景 前段时间写了个自动爬虫的脚本,定时在阿里云服务器上执行,会从某个网站上爬取链接保存到txt文本中,但是脚本不够完善,我需要爬虫完毕之后通过邮件把附件给我发送过来,之前写过一个<利用Pyth ...
- [SpringBoot] - 发送带附件的邮件
<!--发送email依赖--> <dependency> <groupId>org.springframework.boot</groupId> &l ...
- C#发送带附件的邮件的代码
如下的代码是关于C#发送带附件的邮件的代码. MailMessage m = new MailMessage();m.Subject = "File attachment!";m. ...
- 使用JavaMail发送带附件的邮件
所需jar包 链接:http://pan.baidu.com/s/1dFo4cDz 密码:akap 工具类: package com.javamail.utils; import java.util. ...
- (转)用javamail发送带附件的邮件
本文转载自:http://redleaf.iteye.com/blog/78217 mail.java 代码 package mail; import java.util.* ; import jav ...
- 接口测试基础——第2篇smtplib发送带附件的邮件
我先给大家补充一个用QQ发送纯文本电子邮件的代码,用QQ的朋友可以参考一下: # coding=utf-8 import smtplib from email.mime.text import MIM ...
- spring boot:发送带附件的邮件和html内容的邮件(以163.com邮箱为例/spring boot 2.3.2)
一,网站哪些情况下需要发送电子邮件? 作为一个电商网站,以下情况需要发邮件通知用户: 注册成功的信息 用邮箱接收验证码 找回密码时发链接 发送推广邮件 下单成功后的订单通知 给商户的对账单邮件 说明: ...
- 【Mail】JavaMail发送带附件的邮件(二)
上一篇讲了使用JavaMail发送普通邮件([Mail]JavaMail介绍及发送邮件(一)),本例讲发送复杂的邮件(带有附件的邮件) 生成一封复杂的邮件 新建一个JavaWeb的Maven工程,引入 ...
随机推荐
- IO队列和IO调度
IO体系概览 先看看本文主题IO调度和IO队列处于整个IO体系的哪个位置,这个IO体系是非常重要的,了解IO体系我们可以对整个IO过程有个全面的认识.虽然一下两下并不清楚IO体系各个部分的细节,但是我 ...
- 学艺不精,又被shell的管道给坑了
我用过bash shell,而且时间不短了.但我从来没学过shell,至少没有像C++这么认真去学.平时写些基本的脚本没问题,不懂也可以google.百度.可在2014最后一天,掉坑里了. 其实脚本也 ...
- python json基础学习01
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import json #全称(javascript object ...
- 集成支付宝SDK遇到的坑
一.首先我先把集成过程说一下.小编想说的话:支付宝是我做支付中觉得坑最多的一个,各种编译不过,各种出问题. 废话不多说,进入主题:1.首先当前是下载官方SDK啦,当前你也可以通过cocopods进行导 ...
- java技术学习网址收藏
Bootstrap:http://www.runoob.com/bootstrap/bootstrap-intro.html AngularJS : http://www.runoob.com/ang ...
- extjs tree check 级联选择
extjs4 tree check 级联选择 实现效果: 关键代码: function changeAllNode(node, isCheck) { allChild(node, isCheck); ...
- 【网络协议】TCP连接的建立和释放
转载请注明出处:http://blog.csdn.net/ns_code/article/details/29382883 TCP首部格式 先看TCP报文段的格式,例如以下; TCP报文段首部的前20 ...
- leetcode先刷_Unique Paths II
那么上述问题,假设这个矩阵堵塞障碍,不能在若干组合前面所用的方法,因为这么多的位置实际上是没有办法的事儿. 还有剩下的唯一合理的解决方案dp该.与最低要求,并且等,从右下角以前突起,对于位置(i, j ...
- java中的二进制
(1)按位与运算 & 1 & 1 = 1, 0 & 1 = 0 51 & 5 即 0011 0011 & 0000 0101 =0000 0001 = 1 ...
- JQuery hover(over,out) 使用笔记
转载自:http://www.douban.com/note/202404884/ JQuery hover(over,out) 使用笔记 JavaScript 下.onmouseover() 和 o ...