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 ...
随机推荐
- 8 Productivity hacks for Data Scientists & Business Analysts
8 Productivity hacks for Data Scientists & Business Analysts Introduction I was catching up with ...
- Linux 网络操作
Linux 基础网路操作 ifconfig eth0 down # 禁用网卡 ifconfig eth0 up # 启用网卡 ifup eth0: # 启用网卡 mii-tool em1 # 查看网 ...
- waven 常用构建命令
常用命令 mvn compile : 编译maven项目 mvn test : 运行项目测试用例 mvn package : 将项目打成jar包 mvn clean : 删除target目录下生成的文 ...
- not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AV ...
- Java笔记之java.lang.String#trim
String的trim()方法是使用频率频率很高的一个方法,直到不久前我不确定trim去除两端的空白符时对换行符是怎么处理的点进去看了下源码的实现,才发现String#trim的实现跟我想像的完全不一 ...
- Linux如何解决动态库的版本控制
引用自:http://www.linuxidc.com/Linux/2012-04/59071.htm (换句话说,soname不是真实存在的文件,只是在此库中和将来调用此库的文件中保存的一个名字,在 ...
- 高通Trustzone and QSEE介绍
http://blog.csdn.net/iamliuyanlei/article/details/52625968
- kali linux2.0安装vega
1.到官网下载安装包(选择版本:我这里以Linux64位为例) vega下载地址:https://subgraph.com/vega/download/ 2.解压到指定目录: unzip VegaBu ...
- Python_oldboy_自动化运维之路_面向对象(十)
面向对象编程 OOP编程是利用“类”和“对象”来创建各种模型来实现对真实世界的描述,使用面向对象编程的原因一方面是因为它可以使程序的维护和扩展变得更简单,并且可以大大提高程序开发效率 ,另外,基于面向 ...
- Android方法引用数超过65535优雅解决
随着应用不断迭代更新,业务线的扩展,应用越来越大(比如:集成了各种第三方SDK或者公共开源的Library文件.jar文件)这样一来,项目耦合性就很高,重复作用的类就越来越多了,SO:问题就来了.相信 ...