用Java发送图文并茂的HTML邮件
来源:互联网 发布:ubuntu 16.04国内镜像 编辑:程序博客网 时间:2024/05/09 14:41
- 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>
- *
- * @author 孙钰佳
- * @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);
- }
- }
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;/** ** Title:用java发送邮件的例子 *
* ** Description:发送图片附件并在html中使用该图片 *
* ** Copyright: Copyright (c) 2007 *
* * @author 孙钰佳 * @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", "");// 在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);}}
- 用Java发送图文并茂的HTML邮件
- 用Java发送图文并茂的HTML邮件
- 用Java发送图文并茂的HTML邮件
- 用Java发送图文并茂的HTML邮件
- 用Java Mail发送带有图片附件的html格式邮件
- 用java实现发送html格式的邮件
- Java发送HTML格式的邮件
- Java邮件开发(二):使用JMail发送一封图文并茂且包含附件的邮件(上)
- Java邮件开发(二):使用JMail发送一封图文并茂且包含附件的邮件(下)
- Java邮件开发(二):使用JMail发送一封图文并茂且包含附件的邮件(上)
- Java邮件开发(二):使用JMail发送一封图文并茂且包含附件的邮件(下)
- 用JavaMail发送HTML格式的邮件
- 用ASP发送HTML格式的邮件
- java - HtmlEmail发送html邮件
- sendmail 发送html 的邮件
- Java发送包含网络和本地图片的HTML邮件
- java 邮件发送 --- 简单的邮件发送
- 用phpmailer发送HTML邮件
- 合并两个ICON
- MS08-011 Microsoft Works 文件转换器中的漏洞分析
- 装修日记(12)关于吊顶
- 二、Web Service开发
- 地图切割算法
- 用Java发送图文并茂的HTML邮件
- C源码通过IP地址查找主机名
- 和ajax相关
- 我10年来的学习和生活
- MS08-012 Vulnerabilities in Microsoft Office Publisher Could Allow Remote Code exploit
- bug管理工具bugfree配置之一:php mysql apache 配置篇
- 挂钩 NtResumeThread 实现全局Hook
- 对联锦集
- .NET 设计模式的学习笔记(1)- - - 面向对象基础