java发送邮件内容含图片不能显示_javamail发送邮件-正文中包含文本和图片、包含附件...
package javamail;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
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.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class SendMailTest {
public static void main(String[] args) throws AddressException, MessagingException, UnsupportedEncodingException {
/** 环境属性 */
final Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.host", "smtp.163.com");//邮件发送服务器的主机名
props.setProperty("mail.user", "test@163.com");//发送邮件的邮箱账号,这里要是可用的真实的账号
props.setProperty("mail.password", "11111111");//发送邮件的邮箱的密码,这里要是可用的真实的密码
/** 授权信息 */
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(props.getProperty("mail.user"),
props.getProperty("mail.password"));
}
};
/** 使用环境属性和授权信息构建邮件会话 */
Session mailSession = Session.getInstance(props, authenticator);
/** 创建邮件消息 */
MimeMessage message = new MimeMessage(mailSession);
/** 设置发件人 */
message.setFrom(new InternetAddress(props.getProperty("mail.user")));
/** 设置收件人 */
message.setRecipient(RecipientType.TO, new InternetAddress("test@qq.com"));
/** 设置要抄送给的人 */
message.setRecipient(RecipientType.CC, new InternetAddress("test@sina.com"));
/** 设置蜜送,其他的收件人不能看到密送的人的邮箱地址 */
message.setRecipient(RecipientType.BCC, new InternetAddress("test@qq.com"));
/** 设置邮件标题 */
message.setSubject("test send mail 1");
/** 设置邮件内容 */
Multipart all = new MimeMultipart("mixed");
/** 设置邮件正文-文本 + 图片
-
要想在邮件正文中包含图片,需要注意以下3点
-
1.MimeMultipart mmp = new MimeMultipart("related"); 构造函数中要是"related"
-
2.bp_img.setHeader("Content-ID", "hihihi");后面的值可以任意设置,只要在要显示的图片的地方src属性值中的cid保持一致就好
-
3.在想要展示图片的地方加上
*/
MimeBodyPart bp1 = new MimeBodyPart();
MimeMultipart mmp = new MimeMultipart("related");
MimeBodyPart bp_img = new MimeBodyPart();
FileDataSource fds2 = new FileDataSource("C:\ test.jpg");
bp_img.setDataHandler(new DataHandler(fds2));
bp_img.setFileName(MimeUtility.encodeText("测试图片"));
bp_img.setHeader("Content-ID", "hihihi");
mmp.addBodyPart(bp_img);
MimeBodyPart bp_text = new MimeBodyPart();
bp_text.setContent("Hello, I am text" + "",
"text/html;charset=utf-8");
mmp.addBodyPart(bp_text);
bp1.setContent(mmp);
all.addBodyPart(bp1);
/** 设置邮件的附件 */
BodyPart attachFile1 = new MimeBodyPart();
FileDataSource fds3 = new FileDataSource("C:\ test.jpg");
attachFile1.setFileName(fds3.getName());
attachFile1.setDataHandler(new DataHandler(fds3));
all.addBodyPart(attachFile1);
message.setContent(all);
/** 发送邮件 */
Transport.send(message);
}
}
