Java发送内嵌图片的邮件
使用Java发送邮件时,既需要发送html文档又需要在邮件中内嵌图片(非附件形式),这里封装了一个类使用JavaMail组织的邮件发送,支持多张图片发送,并指定图片位置。注意img标签src要如下写<img src=\"cid:abcd\">"其中abcd对应mageMailDto#imageCid,可以自行定义,不要出现重复。
发送方法,注意替换几个邮件参数,如host、user、password
/*** 发送内嵌图片的邮件** @param fromEmailAddress 发件人邮箱地址* @param fromEmailName 发件人邮箱显示名称* @param subject 主题* @param content 邮件正文* @param toEmailAddressList 收件人邮箱集合(支持群发)* @return true:成功;false:失败*/@Overridepublic boolean sendImageHtmlEmail(String fromEmailAddress, String fromEmailName, String subject, String content, List<String> toEmailAddressList, ImageMailDto[] imageData) throws RuntimeException {if (CollectionUtils.isEmpty(toEmailAddressList)) {throw new RuntimeException("toEmailAddressList is empty!");}if (toEmailAddressList.size() > MAX_SEND_SIZE) {throw new RuntimeException("toEmailAddressList is more than [" + MAX_SEND_SIZE + "]!");}try {Properties props = new Properties();props.put("mail.smtp.host", hostName);props.put("mail.smtp.auth", "true");Session sendMailSession = Session.getDefaultInstance(props, authenticator);MimeMessage mimeMessage = new MimeMessage(sendMailSession);mimeMessage.setFrom(new InternetAddress(fromEmailAddress));InternetAddress[] address = new InternetAddress().parse(toEmailAddressList.toString());mimeMessage.setRecipients(Message.RecipientType.TO, address);mimeMessage.setSubject(subject);mimeMessage.setSentDate(new Date());BodyPart mdp = new MimeBodyPart();//新建一个存放信件内容的BodyPart对象mdp.setContent(content, "text/html;charset=utf-8");//给BodyPart对象设置内容和格式/编码方式Multipart mm = new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)if (imageData != null) {for (ImageMailDto dto : imageData) {MimeBodyPart image = new MimeBodyPart();image.setDataHandler(new DataHandler(new ByteArrayDataSource(dto.getImageData(), dto.getMimeType()))); //javamail jafimage.setContentID(dto.getImageCid());mm.addBodyPart(image);}}mimeMessage.setContent(mm);//把mm作为消息对象的内容mimeMessage.saveChanges();Transport.send(mimeMessage);return true;} catch (Exception e) {log.error("邮件发送异常邮件地址" + toEmailAddressList, e);throw new RuntimeException(e);}}
鉴权类写法:
Authenticator authenticator = new DefaultAuthenticator(props.getProperty("mail.smtp.user"),props.getProperty("mail.smtp.password"));
图片存储类(自定义,可以修改替换):
import java.io.Serializable;/*** 邮件发送图片** @author wangqing* @since 16-3-2 下午4:39*/
public class ImageMailDto implements Serializable {private static final long serialVersionUID = 1L;private byte[] imageData;private String imageCid;//"好好工作<img src=\"cid:abcd\">"private String mimeType;//like image/jpeg、image/bmp、image/gifpublic byte[] getImageData() {return imageData;}public void setImageData(byte[] imageData) {this.imageData = imageData;}public String getImageCid() {return imageCid;}public void setImageCid(String imageCid) {this.imageCid = imageCid;}public String getMimeType() {return mimeType;}public void setMimeType(String mimeType) {this.mimeType = mimeType;}}
Java发送内嵌图片的邮件
使用Java发送邮件时,既需要发送html文档又需要在邮件中内嵌图片(非附件形式),这里封装了一个类使用JavaMail组织的邮件发送,支持多张图片发送,并指定图片位置。注意img标签src要如下写<img src=\"cid:abcd\">"其中abcd对应mageMailDto#imageCid,可以自行定义,不要出现重复。
发送方法,注意替换几个邮件参数,如host、user、password
/*** 发送内嵌图片的邮件** @param fromEmailAddress 发件人邮箱地址* @param fromEmailName 发件人邮箱显示名称* @param subject 主题* @param content 邮件正文* @param toEmailAddressList 收件人邮箱集合(支持群发)* @return true:成功;false:失败*/@Overridepublic boolean sendImageHtmlEmail(String fromEmailAddress, String fromEmailName, String subject, String content, List<String> toEmailAddressList, ImageMailDto[] imageData) throws RuntimeException {if (CollectionUtils.isEmpty(toEmailAddressList)) {throw new RuntimeException("toEmailAddressList is empty!");}if (toEmailAddressList.size() > MAX_SEND_SIZE) {throw new RuntimeException("toEmailAddressList is more than [" + MAX_SEND_SIZE + "]!");}try {Properties props = new Properties();props.put("mail.smtp.host", hostName);props.put("mail.smtp.auth", "true");Session sendMailSession = Session.getDefaultInstance(props, authenticator);MimeMessage mimeMessage = new MimeMessage(sendMailSession);mimeMessage.setFrom(new InternetAddress(fromEmailAddress));InternetAddress[] address = new InternetAddress().parse(toEmailAddressList.toString());mimeMessage.setRecipients(Message.RecipientType.TO, address);mimeMessage.setSubject(subject);mimeMessage.setSentDate(new Date());BodyPart mdp = new MimeBodyPart();//新建一个存放信件内容的BodyPart对象mdp.setContent(content, "text/html;charset=utf-8");//给BodyPart对象设置内容和格式/编码方式Multipart mm = new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)if (imageData != null) {for (ImageMailDto dto : imageData) {MimeBodyPart image = new MimeBodyPart();image.setDataHandler(new DataHandler(new ByteArrayDataSource(dto.getImageData(), dto.getMimeType()))); //javamail jafimage.setContentID(dto.getImageCid());mm.addBodyPart(image);}}mimeMessage.setContent(mm);//把mm作为消息对象的内容mimeMessage.saveChanges();Transport.send(mimeMessage);return true;} catch (Exception e) {log.error("邮件发送异常邮件地址" + toEmailAddressList, e);throw new RuntimeException(e);}}
鉴权类写法:
Authenticator authenticator = new DefaultAuthenticator(props.getProperty("mail.smtp.user"),props.getProperty("mail.smtp.password"));
图片存储类(自定义,可以修改替换):
import java.io.Serializable;/*** 邮件发送图片** @author wangqing* @since 16-3-2 下午4:39*/
public class ImageMailDto implements Serializable {private static final long serialVersionUID = 1L;private byte[] imageData;private String imageCid;//"好好工作<img src=\"cid:abcd\">"private String mimeType;//like image/jpeg、image/bmp、image/gifpublic byte[] getImageData() {return imageData;}public void setImageData(byte[] imageData) {this.imageData = imageData;}public String getImageCid() {return imageCid;}public void setImageCid(String imageCid) {this.imageCid = imageCid;}public String getMimeType() {return mimeType;}public void setMimeType(String mimeType) {this.mimeType = mimeType;}}