java发送内嵌图片邮件_JavaMail发送嵌入图片的邮件

package com.what21.network.mail;

import java.io.IOException;

import java.util.Date;

import java.util.Map;

import java.util.Properties;

import java.util.Set;

import javax.mail.Authenticator;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

public class EmbeddedImageEmailUtil {

/**

* Sends an HTML e-mail with inline images.

* @param host SMTP host

* @param port SMTP port

* @param userName e-mail address of the sender's account

* @param password password of the sender's account

* @param toAddress e-mail address of the recipient

* @param subject e-mail subject

* @param htmlBody e-mail content with HTML tags

* @param mapInlineImages key: Content-ID value: path of the image file

* @throws AddressException

* @throws MessagingException

*/

public static void send(String host, String port,

final String userName, final String password, String toAddress,

String subject, String htmlBody,

Map mapInlineImages)

throws AddressException, MessagingException {

// sets SMTP server properties

Properties properties = new Properties();

properties.put("mail.smtp.host", host);

properties.put("mail.smtp.port", port);

properties.put("mail.smtp.auth", "true");

properties.put("mail.smtp.starttls.enable", "true");

properties.put("mail.user", userName);

properties.put("mail.password", password);

// creates a new session with an authenticator

Authenticator auth = new Authenticator() {

public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(userName, password);

}

};

Session session = Session.getInstance(properties, auth);

// creates a new e-mail message

Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(userName));

InternetAddress[] toAddresses = { new InternetAddress(toAddress) };

msg.setRecipients(Message.RecipientType.TO, toAddresses);

msg.setSubject(subject);

msg.setSentDate(new Date());

// creates message part

MimeBodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setContent(htmlBody, "text/html");

// creates multi-part

Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messageBodyPart);

// adds inline image attachments

if (mapInlineImages != null && mapInlineImages.size() > 0) {

Set setImageID = mapInlineImages.keySet();

for (String contentId : setImageID) {

MimeBodyPart imagePart = new MimeBodyPart();

imagePart.setHeader("Content-ID", "");

imagePart.setDisposition(MimeBodyPart.INLINE);

String imageFilePath = mapInlineImages.get(contentId);

try {

imagePart.attachFile(imageFilePath);

} catch (IOException ex) {

ex.printStackTrace();

}

multipart.addBodyPart(imagePart);

}

}

msg.setContent(multipart);

Transport.send(msg);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值