pop3_用Java发送图文并茂的HTML邮件
package com.syj; import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart; import com.sun.istack.internal.ByteArrayDataSource; /**
* <P>
* Title:用java发送邮件的例子
* </P>
*
* <P>
* Description:发送图片附件并在html中使用该图片
* </P>
*
* <P>
* Copyright: Copyright (c) 2007
* </P>
*
* @main sunyujia@yahoo.cn
* @date Jun 10, 2008 12:35:26 AM
*/
public class SendMail {
private static String username = "xxxx";
private static String password = "xxxx";
private static String smtpServer = "smtp.163.com";
private static String fromMailAddress = "xxxx@163.com";
private static String toMailAddress = "sunyujia@yahoo.cn"; public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpServer);
// 获得邮件会话对象
Session session = Session.getDefaultInstance(props,
new SmtpAuthenticator(username, password));
/** *************************************************** */
// 创建MIME邮件对象
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
toMailAddress));// 收件人
mimeMessage.setSubject("主题");
mimeMessage.setSentDate(new Date());// 发送日期
Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
/** *************************************************** */
BodyPart bodyPart = new MimeBodyPart();// 正文
bodyPart.setDataHandler(new DataHandler("测<img src="cid:IMG1" />试",
"text/html;charset=GBK"));// 网页格式
/** *************************************************** */
BodyPart attachBodyPart = new MimeBodyPart();// 普通附件
FileDataSource fds = new FileDataSource("c:/boot.ini");
attachBodyPart.setDataHandler(new DataHandler(fds));
attachBodyPart.setFileName("=?GBK?B?"
+ new sun.misc.BASE64Encoder().encode(fds.getName().getBytes())
+ "?=");// 解决附件名中文乱码
mp.addBodyPart(attachBodyPart);
/** *************************************************** */
MimeBodyPart imgBodyPart = new MimeBodyPart(); // 附件图标
byte[] bytes = readFile("C:/button.gif");
ByteArrayDataSource fileds = new ByteArrayDataSource(bytes,
"application/octet-stream");
imgBodyPart.setDataHandler(new DataHandler(fileds));
imgBodyPart.setFileName("button.gif");
imgBodyPart.setHeader("Content-ID", "<IMG1></IMG1>");// 在html中使用该图片方法src="cid:IMG1"
mp.addBodyPart(imgBodyPart);
/** *************************************************** */
mp.addBodyPart(bodyPart);
mimeMessage.setContent(mp);// 设置邮件内容对象
Transport.send(mimeMessage);// 发送邮件 } /**
* 读取文件
*
* @param file
* 文件路径
* @return 返回二进制数组
*/
public static byte[] readFile(String file) {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
int bytesRead;
byte buffer[] = new byte[1024 * 1024];
while ((bytesRead = fis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
Arrays.fill(buffer, (byte) 0);
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bos.toByteArray();
}
} /**
* Smtp认证
*/
class SmtpAuthenticator extends Authenticator {
String username = null;
String password = null; // SMTP身份验证
public SmtpAuthenticator(String username, String password) {
this.username = username;
this.password = password;
} public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
} }
pop3_用Java发送图文并茂的HTML邮件的更多相关文章
- java发送带附件的邮件
/** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...
- java发送内嵌图片邮件
前言: 博客系统中需要邮件服务的功能,以前写过类似的功能,不过功能太简单了,仅仅是发送文本内容,现在尝试一下发送内嵌图片邮件! 准备工作: 请参考:http://www.cnblogs.com/huj ...
- 用java发送email邮件例子
package com.hzk.mail; import java.net.MalformedURLException; import java.net.URL; import java.text.S ...
- Java发送QQ邮件
面试的时候被问到这个问题,别人问我用Java发过邮件没有,被问得一脸懵逼.然后就研究了一下,不是很难,按照网上的方法折腾了几天就搞出来了. 首先,使用QQ邮箱发送邮件之前需要在邮箱里面配置,开启pop ...
- 用Java发送HTML格式邮件测试类(支持中文)
代码由纯Java写成,支持中文,一目了然,只要将Main函数中的相关信息填写正确就直接用了,便于修改,可以在此类基础上任意扩展成自己的类. 注意做HTML形式的邮件,最好把HTML,CSS都写全,只写 ...
- java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)
java 无法发送邮件问题 java 发送qq邮件 报错:java mail Received fatal alert: handshake_failure (使用ssl) javax.mail.M ...
- java解析邮箱中的邮件信息
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import ...
- [SpringBoot] - 发送带附件的邮件
<!--发送email依赖--> <dependency> <groupId>org.springframework.boot</groupId> &l ...
- java发送email一般步骤
java发送email一般步骤 一.引入javamail的jar包: 二.创建一个测试类,实现将要发送的邮件内容写入到计算机本地,查看是否能够将内容写入: public static void mai ...
随机推荐
- MySql数据库表的查询操作
http://www.cnblogs.com/whgk/p/6149009.html 优化:http://www.ihref.com/read-16422.html MYSQL常用的几种连接查询方法
- [转载]AngularJS 开发者最常犯的 10 个错误
http://www.oschina.net/translate/top-10-mistakes-angularjs-developers-make
- 转:我是否该放弃VB.Net?
我是否该放弃VB.Net呢?这个问题一次次的出现在我的脑海里,而且这种想法越来越强烈.放弃VB.Net至少能让我的生活变得轻松些.如果你是个C#程序员,那拷贝粘贴代码会很容易,因为可以找到的例子代码如 ...
- HDU 2544 最短路 最短路问题
解题报告: 这题就是求两个单源点之间的最小距离,属于最短路问题,由于数据量很小,只有100,所以这题可以用弗洛伊德也可以用迪杰斯特拉,都可以过,但是用迪杰斯特拉会快一点,但用弗洛伊德的代码会稍短一点, ...
- Python2的object和type
前言: Python在2.2和3.0之间,把继承了object的类叫做新式类,如果我们定义了一个类,他没有继承object,则不是新式类,则没有__class__,__bases__等属性,而用typ ...
- A - Jugs ZOJ - 1005 (模拟)
题目链接:https://cn.vjudge.net/contest/281037#problem/A 题目大意:给你a,b,n.a代表第一个杯子的容量,b代表第二个杯子的容量,然后一共有6种操作.让 ...
- 在maven 2工程中加入iTextAsian支持(maven添加自定义jar包到本地仓库)
最近需要在工程中加入JasperReports,其中要用到把报表导出为pdf文件的功能.JasperReports内部使用iText来输出pdf文档,而iText对中文是放在单独的包iTextAsia ...
- C语言内存分布
C语言内存分布 典型的C语言程序内存表示分区共有5个部分: 正文段 Text segment 已初始化数据段(数据段)Initialized data segment 未初始化数据段(bss)Unin ...
- Linux本地解析文件/etc/hosts说明【原创】
windows的域名本地解析文件hosts是可以一个域名对多个IP,如果有一个IP有问题,可以去解析到其他IP Linux的本地解析文件/etc/hosts,是否也可以这样呢.下面做了个测试 先看一下 ...
- 获取同一接口多个实现类的bean
@Service("taskExecutorFactory") public class TaskExecutorFactory implements ApplicationCon ...