package com.assess.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
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 javax.mail.internet.MimeUtility; public class SendmailUtil {
/*
* 参考此文章
* http://www.cnblogs.com/xdp-gacl/p/4216311.html
*
*
* smtp.sohu.com 搜狐邮箱主机
* smtp.163.com 163邮箱主机,默认端口25
* smtp.qq.com qq邮箱主机
* */ /**
* from 发件人邮箱
* passwd 发件人邮箱密码
* to 收件人邮箱
* subject 主题
* txt 内容
* files 附件
* host 服务主机
* protocol 协议 (smtp)
* auth 身份验证(true)
* */
public static boolean send(String from,String passwd,String to,
String subject, String txt,List<File> files,
String host,String protocol,String auth) { try {
Properties prop = new Properties();
prop.setProperty("mail.host", host);
prop.setProperty("mail.transport.protocol", protocol);
prop.setProperty("mail.smtp.auth", auth);
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、使用邮箱的用户名和密码连上邮件服务器,
// 发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,
// 用户名和密码都通过验证之后才能够正常发送邮件给收件人。
ts.connect(host, from, passwd);
//4、创建邮件
Message message = createMixedMail(session, from, to, subject, txt, files);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close(); }catch(Exception e) {
return false;
}
return true;
} /**
* 发送文字、附件的邮件
* */
public static MimeMessage createMixedMail(Session session,String from,String to,String subject,String txt,List<File> files) throws Exception {
//创建邮件
MimeMessage message = new MimeMessage(session); //设置邮件的基本信息
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject); //正文
MimeBodyPart text = new MimeBodyPart();
text.setContent(txt,"text/html;charset=UTF-8"); //图片
// MimeBodyPart image = new MimeBodyPart();
// image.setDataHandler(new DataHandler(new FileDataSource("src\\check.png")));
// image.setContentID("aaa.jpg"); //附件
List<MimeBodyPart> mimeBodyParts = new ArrayList<MimeBodyPart>();
for(int i=;i<files.size();i++) {
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(files.get(i)));
attach.setDataHandler(dh);
attach.setFileName(MimeUtility.encodeWord(dh.getName()));
mimeBodyParts.add(attach);
} //描述关系:正文和图片
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
// mp1.addBodyPart(image);
mp1.setSubType("related"); //描述关系:正文和附件
MimeMultipart mp2 = new MimeMultipart();
for(int i=;i<mimeBodyParts.size();i++) {
mp2.addBodyPart(mimeBodyParts.get(i));
}
// mp2.addBodyPart(attach2); //代表正文的bodypart
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp1);
mp2.addBodyPart(content);
mp2.setSubType("mixed"); message.setContent(mp2);
message.saveChanges(); // message.writeTo(new FileOutputStream("D:\\MixedMail.eml"));
//返回创建好的的邮件
return message;
}
}
package com.assess.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List; public class Main { private static final String HOST = "smtp.163.com";
private static final String PROTOCOL = "smtp";
private static final String AUTH = "true"; private static final String USER_NAME = "135****920@163.com";//发件人邮箱
private static final String PASSWORD = "";//密码
private static final String RECRIVER ="139****893@qq.com";//接收人邮箱 public static void main(String[] args) {
List<File> files = new ArrayList<File>();
File f = new File("src\\备忘.rar");
File f2 = new File("src\\check.png");
files.add(f);
files.add(f2);
if(SendmailUtil.send(USER_NAME, PASSWORD, RECRIVER, "主题信息", "内容信息", files, HOST, PROTOCOL, AUTH)) { System.out.println("success");
}
} }

java发送email的更多相关文章

  1. java发送email一般步骤

    java发送email一般步骤 一.引入javamail的jar包: 二.创建一个测试类,实现将要发送的邮件内容写入到计算机本地,查看是否能够将内容写入: public static void mai ...

  2. Java发送email的端口问题

    Could not connect to SMTP host: smtp.***.com, port: 465, response: -1 使用Java发送email 的端口问题.一般使用25端口即可 ...

  3. java发送 email

    public class EmailUtils implements IAction { private static Logger logger = Logger.getLogger(EmailUt ...

  4. java发送email(含代理方式,ssl方式,传统方式)

    package spring.vhostall.com; import java.security.Security; import java.util.Date; import java.util. ...

  5. 用java发送email邮件例子

    package com.hzk.mail; import java.net.MalformedURLException; import java.net.URL; import java.text.S ...

  6. Java发送Email邮件及SpringBoot集成

    一:普通方式发送 1.导包 <!--Java MAil 发送邮件API--> <dependency> <groupId>javax.mail</groupI ...

  7. 关于java发送email

    转载:https://blog.csdn.net/qq_32371887/article/details/72821291 1:使用JavaMail发送邮件 // 1.创建一个程序与邮件服务器会话对象 ...

  8. 廖雪峰Java13网络编程-2Email编程-1发送email

    1.邮件发送 1.1传统邮件发送: 传统的邮件是通过邮局投递,从一个邮局到另一个邮局,最终到达用户的邮箱. 1.2电子邮件发送: 与传统邮件类似,它是从用户电脑的邮件软件(如outlook)发送到邮件 ...

  9. 使用spring 并加载模板发送Email 发邮件 java 模板

    以下例子是使用spring发送email,然后加载到固定的模板,挺好的,大家可以试试 需要使用到spring-context 包 和 com.springsource.org.apache.veloc ...

随机推荐

  1. navicate怎么用sql语句插入一条语句

    1.打开数据库:找到表,双击要插入的表打开: 2.打开之后点击文件->查询表 3.输入要查询的语句,点击运行.成功后会有提示.

  2. Windows Server 2008设置远程桌面连接的最大数量

    远程桌面连接的默认数量是2,当有多个用户需要同时远程桌面连接时很不方便,可以设置远程桌面连接的最大数量. 1. 运行gpedit.msc: 2. 选择计算机配置-->管理模板-->Wind ...

  3. ZHA profile与ZLL profile的一个例子

    ZHA Coordinator 如何控制ZLL Light/Philips Hue Light 缩写: ZHA: ZigBee Home Automation profile ZLL:  ZigBee ...

  4. 什么是Servlet?

    HTML只能用来保存静态内容,而通常情况下,静态页面很难满足实际应用的需要,鉴于此,动态页面被引入.所谓动态页面,指的是能够根据不同时间,不同用户而显示不同内容的页面,例如常见的论坛.留言板.电子商务 ...

  5. file_get_contents()/file_put_contents()

    PHP file_get_contents() 函数 定义和用法 file_get_contents() 把整个文件读入一个字符串中. 该函数是用于把文件的内容读入到一个字符串中的首选方法.如果服务器 ...

  6. POJ-3061

    算法: 1. 定义两个整数N和S,输入序列长度到N,输入最小子序列和下界到S. 2. 定义一个数组arr[100002],从arr[1]开始依次输入N个序列元素到arr. 3. 定义一个整数ans,初 ...

  7. Jsp静态包含和动态包含的区别

    1 <%@include file="xxx.jsp"%>为jsp中的编译指令,其文件的包含是发生在jsp向servlet转换的时期,而<jsp:include ...

  8. js 数组去重(7种)

    第一次写技术博客,之前总是认为写这些会很浪费时间,还不如多看几篇技术博文.但...但昨天不知道受了什么刺激,好像有什么在驱使着自己要写一样,所以才有了今天的第一篇博文.总觉得应该要坚持这样写下去.初次 ...

  9. Docker dockerfile创建Eclipse镜像初试

    抽空初步阅读了Docker技术入门与实战 [Kindle电子书] http://www.cnblogs.com/2018/p/4600116.html 现在想首先在开发环境下引入统一的环境,由于开发中 ...

  10. list集合的排序Comparator和Collections.sort

    一个例子 package sortt; import java.util.ArrayList; import java.util.Collections; import java.util.Compa ...