package com;
import java.io.File;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
/**
* 本類測試郵件中嵌套圖片
* @author sunny
*
*/
public
class AttachedImageMail {
public
static
void main(String[] args)
throws Exception{
JavaMailSenderImpl senderImpl =
new JavaMailSenderImpl();
//設定mail server
senderImpl.setHost(
"192.168.1.2");
senderImpl.setUsername(
"username");
senderImpl.setPassword(
"pssword");
//創建郵件消息,發送簡單郵件和html郵件的區別
MimeMessage mailMessage = senderImpl.createMimeMessage();
//注意這裏的boolean,等於真的時候才能嵌套圖片,在構建MimeMessageHelper時候,所給定的值是true表示啓用,
multipart模式
MimeMessageHelper messageHelper =
new MimeMessageHelper(mailMessage,
true);
//設置收件人,寄件人
messageHelper.setTo(
"[email]sun111@163.com[/email]");
messageHelper.setFrom(
"[email]webadmin@163.com[/email]");
messageHelper.setSubject(
"測試郵件中嵌套圖片!!");
//true 表示啓動HTML格式的郵件
messageHelper.setText(
"<html><head></head><body><h1>hello!!zhangjian</h1>" +
"<img src=\"cid:aaa\"/></body></html>",
true);
FileSystemResource img =
new FileSystemResource(
new File(
"c:/aaa.jpg"));
messageHelper.addInline(
"aaa",img);
//發送郵件
senderImpl.send(mailMessage);
System.out.println(
"郵件發送成功.....");
}
}