摘抄自别人

  RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封简单的邮件包含邮件头和邮件体两个部分,邮件头和邮件体之间使用空行分隔。

  邮件头包含的内容有:

  1. from字段   --用于指明发件人
  2. to字段       --用于指明收件人
  3. subject字段  --用于说明邮件主题
  4. cc字段      -- 抄送,将邮件发送给收件人的同时抄送给另一个收件人,收件人可以看到邮件抄送给了谁
  5. bcc字段    -- 密送,将邮件发送给收件人的同时将邮件秘密发送给另一个收件人,收件人无法看到邮件密送给了谁

  一个简单的邮件

package me.gacl.main;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; /**
* @ClassName: Sendmail
* @Description: 发送Email
* @author: 孤傲苍狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用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("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createSimpleMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} /**
* @Method: createSimpleMail
* @Description: 创建一封只包含文本的邮件
* @Anthor:孤傲苍狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createSimpleMail(Session session)
throws Exception {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com"));
//邮件的标题
message.setSubject("只包含文本的简单邮件");
//邮件的文本内容
message.setContent("你好啊!", "text/html;charset=UTF-8");
//返回创建好的邮件对象
return message;
}
}

   包含内镶图片的邮件

package me.gacl.main;

import java.io.FileOutputStream;
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; /**
* @ClassName: Sendmail
* @Description: 发送Email
* @author: 孤傲苍狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、连上邮件服务器,需要发件人提供邮箱的用户名和密码进行验证
ts.connect("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createImageMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} /**
* @Method: createImageMail
* @Description: 生成一封邮件正文带图片的邮件
* @Anthor:孤傲苍狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createImageMail(Session session) throws Exception {
//创建邮件
MimeMessage message = new MimeMessage(session);
// 设置邮件的基本信息
//发件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
//邮件标题
message.setSubject("带图片的邮件"); // 准备邮件数据
// 准备邮件正文数据
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是一封邮件正文带图片<img src='cid:xxx.jpg'>的邮件", "text/html;charset=UTF-8");
// 准备图片数据
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\1.jpg"));
image.setDataHandler(dh);
image.setContentID("xxx.jpg");
// 描述数据关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related"); message.setContent(mm);
message.saveChanges();
//将创建好的邮件写入到E盘以文件的形式进行保存
message.writeTo(new FileOutputStream("E:\\ImageMail.eml"));
//返回创建好的邮件
return message;
}
}

  包含附件的邮件

package me.gacl.main;

import java.io.FileOutputStream;
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; /**
* @ClassName: Sendmail
* @Description: 发送Email
* @author: 孤傲苍狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、连上邮件服务器
ts.connect("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createAttachMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} /**
* @Method: createAttachMail
* @Description: 创建一封带附件的邮件
* @Anthor:孤傲苍狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createAttachMail(Session session) throws Exception{
MimeMessage message = new MimeMessage(session); //设置邮件的基本信息
//发件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
//邮件标题
message.setSubject("JavaMail邮件发送测试"); //创建邮件正文,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码
MimeBodyPart text = new MimeBodyPart();
text.setContent("使用JavaMail创建的带附件的邮件", "text/html;charset=UTF-8"); //创建邮件附件
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\2.jpg"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName()); // //创建容器描述数据关系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed"); message.setContent(mp);
message.saveChanges();
//将创建的Email写入到E盘存储
message.writeTo(new FileOutputStream("E:\\attachMail.eml"));
//返回生成的邮件
return message;
}
}

  一个复杂的邮件

package me.gacl.main;

import java.io.FileOutputStream;
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; /**
* @ClassName: Sendmail
* @Description: 发送Email
* @author: 孤傲苍狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、连上邮件服务器
ts.connect("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createMixedMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} /**
* @Method: createMixedMail
* @Description: 生成一封带附件和带图片的邮件
* @Anthor:孤傲苍狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createMixedMail(Session session) throws Exception {
//创建邮件
MimeMessage message = new MimeMessage(session); //设置邮件的基本信息
message.setFrom(new InternetAddress("gacl@sohu.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
message.setSubject("带附件和带图片的的邮件"); //正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("xxx这是女的xxxx<br/><img src='cid:aaa.jpg'>","text/html;charset=UTF-8"); //图片
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src\\3.jpg")));
image.setContentID("aaa.jpg"); //附件1
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\4.zip"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName()); //附件2
MimeBodyPart attach2 = new MimeBodyPart();
DataHandler dh2 = new DataHandler(new FileDataSource("src\\波子.zip"));
attach2.setDataHandler(dh2);
attach2.setFileName(MimeUtility.encodeText(dh2.getName())); //描述关系:正文和图片
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
mp1.addBodyPart(image);
mp1.setSubType("related"); //描述关系:正文和附件
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(attach);
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("E:\\MixedMail.eml"));
//返回创建好的的邮件
return message;
}
}

emil 的使用的更多相关文章

  1. HTML表单元素Emil和密码

    <form action="" method="post" name="myform"><p>E-mail:< ...

  2. Android中的沉浸式状态栏效果

    无意间了解到沉浸式状态栏,感觉贼拉的高大上,于是就是试着去了解一下,就有了这篇文章.下面就来了解一下啥叫沉浸式状态栏.传统的手机状态栏是呈现出黑色条状的,有的和手机主界面有很明显的区别.这一样就在一定 ...

  3. 在配有英特尔® Iris™ 显卡的系统上通过优化对 Just Cause 3 进行增强

    高端 PC 继续通过高性能显卡驱动桌面游戏. 一流的"梦想机器"基于第六代智能 英特尔® 酷睿™ 处理器i7-6700K等 CPU,通常与高端独立显卡配合使用以运行要求最严苛的游戏 ...

  4. git详解

    Git使用教程   source: http://www.cnblogs.com/tugenhua0707/p/4050072.html 一:Git是什么? Git是目前世界上最先进的分布式版本控制系 ...

  5. sql server中对xml进行操作

    一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和参数.为了更好地支持 XM ...

  6. MongoDB【第三篇】MongoDB基本操作

    MongoDB的基本操作包括文档的创建.删除.和更新 文档插入 1.插入 #查看当前都有哪些数据库 > show dbs; local 0.000GB tim 0.000GB #使用 tim数据 ...

  7. jieba中文分词的.NET版本:jieba.NET

    简介 平时经常用Python写些小程序.在做文本分析相关的事情时免不了进行中文分词,于是就遇到了用Python实现的结巴中文分词.jieba使用起来非常简单,同时分词的结果也令人印象深刻,有兴趣的可以 ...

  8. ASP.NET Core的配置(4):多样性的配置来源[中篇]

    我们在本篇文章中会介绍三种针对物理文件的ConfiguationProvider,它们分别是针对JSON文件的JsonConfiguationProvider,针对XML文件的XmlConfiguat ...

  9. 再谈Newtonsoft.Json高级用法

    上一篇Newtonsoft.Json高级用法发布以后收到挺多回复的,本篇将分享几点挺有用的知识点和最近项目中用到的一个新点进行说明,做为对上篇文章的补充. 阅读目录 动态改变属性序列化名称 枚举值序列 ...

随机推荐

  1. uoj 30 tourists

    题目大意: 一个无向图 每个点有权值 支持两个操作 1 修改某个点的权值 2 查询a-b所有简单路径的点上的最小值 思路: 可以把图变成圆方树 然后树链剖分 维护 对于每个方点使用可删堆维护 #inc ...

  2. bzoj 1127 KUP —— 最大子矩形+答案构造

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1127 首先,把权值 > 2*k 的点作为“坏点”,然后在图中用悬线法找权值最大的子矩形 ...

  3. Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割

    ylbtech-Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割 1.返回顶部 1. Java 实例 - 字符串分割  Java 实例 以下实例使用了 split ...

  4. EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间

    在SQL server 中,有两种日期时间类型: 1.DateTime 它的范围是 1753-1-1 至 9999-12-31 2.DateTime2 它的满园是 0001-01-01 至 9999- ...

  5. Linux的gnu c下itoa的代替函数用sprintf(转载)

    转自:http://www.linuxidc.com/Linux/2011-01/31600.htm int number = 12345; char string[25]; // itoa(numb ...

  6. Swift4 函数, 元组, 运算符

    创建: 2018/02/19 完成: 2018/02/19 更新: 2018/02/25 修改标题 [Swift4 函数] -> [Swift4 函数, 元组, 运算符] 更新 :2018/03 ...

  7. vue2.0构建单页应用最佳实战

    链接: https://www.tuicool.com/articles/me6RJfF

  8. 【插件开发】—— 10 JFace开发详解

    前文回顾: 1 插件学习篇 2 简单的建立插件工程以及模型文件分析 3 利用扩展点,开发透视图 4 SWT编程须知 5 SWT简单控件的使用与布局搭配 6 SWT复杂空间与布局搭配 7 SWT布局详解 ...

  9. python删除列表中元素的方法

    删除列表中元素的三种方法-remove.pop.del 1 1.remove: 删除单个元素,删除首个符合条件的元素,按值删除 2 举例说明: 3 >>> str=[1,2,3,4, ...

  10. MFC中利用CString和Format成员函数将数字格式化输出

    str.Format("格式控制字符串”,输出列表): 格式控制字符串包括格式字符串和非格式字符串,用双引号括起来.其中非格式字符串原样输出. 格式字符串是以%开头的字符串:%[标识][输出 ...