java代码发送html邮件,使用Java发送图文并茂的HTML邮件代码

本文提供了一个使用Java发送带有图片附件及HTML内容邮件的示例代码。通过MIME邮件对象实现复杂的邮件格式,包括如何设置SMTP认证、邮件正文、附件及内嵌图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Java发送图文并茂的HTML邮件代码

爱搞机 2008-06-12 16:25

不多说了,直接放代码.

view plaincopy to clipboardprint?

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 [email protected]

* @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 = "[email protected]";

private static String toMailAddress = "[email protected]";

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("测试",

"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 ByteArr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值