1.附件的名称只能为英文,中文乱码
2.友好名称的显示。
我们使用163等邮箱发送邮件时,我们经常可以看到收件人一栏中会是:张益达 <[email protected]>这种方式。在上一版本的代码中并没有使用这样的方式。
下面解决这两个问题
1. 对于中文乱码,是因为邮件要求所有的字符必须是ascII字符,中文字符当然不可以。因此我们只需要把我们的中文全部转码就可以了。
而JavaMail提供了这样一个类:MimeUtility。使用这个类的encodeText方法就可以对中文进行编码。
2. 对于显示友好名称,我们首先要了解收件人填写时的格式。格式一般为:友好名称 <邮箱地址>,如果有多个收件人要以逗号分割
如:张益达sohu <[email protected]>,张益达qq <[email protected]>,张益达163 <[email protected]>
package com.zyh.demo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Message.RecipientType; import javax.mail.Address; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import javax.mail.util.ByteArrayDataSource; /** * 创建一个复杂的邮件,这个邮件包括一段正文和两个附件 * 其中正文要包含一张图片 * @author Administrator * */ public class Demo4 { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); Session session = Session.getInstance(props); session.setDebug(true); Message msg = new MimeMessage(session); /*邮件消息头设置*/ //使用MimeUtility.encodeText()方法对中文进行编码(base64或QP) msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("张益达sss") + "\" <[email protected]>")); msg.setReplyTo(new Address[]{new InternetAddress("[email protected]")}); //友好名称和邮箱地址之间不要忘了有一空格,邮箱地址之间用逗号分隔 //如:张益达sohu <[email protected]>,张益达qq <[email protected]>,张益达163 <[email protected]> msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("张益达sohu") + " <[email protected]>," + MimeUtility.encodeText("张益达qq") + " <[email protected]>," + MimeUtility.encodeText("张益达sina") + " <[email protected]>," + MimeUtility.encodeText("张益达163") + " <[email protected]>")); msg.setSubject("From 163 这是一封复杂邮件"); /*邮件消息内容设置,包括两个附件和一段正文*/ Multipart msgPart = new MimeMultipart("mixed"); msg.setContent(msgPart); MimeBodyPart body = new MimeBodyPart(); //表示正文 MimeBodyPart attach1 = new MimeBodyPart(); //表示附件1 MimeBodyPart attach2 = new MimeBodyPart(); //表示附件2 msgPart.addBodyPart(body); msgPart.addBodyPart(attach1); msgPart.addBodyPart(attach2); /*以下为设置正文*/ /*正文是文字和图片混合的*/ Multipart contentPart = new MimeMultipart("related"); body.setContent(contentPart); MimeBodyPart content = new MimeBodyPart(); //文字 MimeBodyPart img = new MimeBodyPart(); //图片 contentPart.addBodyPart(content); contentPart.addBodyPart(img); DataSource fileds = new ByteArrayDataSource(new FileInputStream("D:\\picture\\jpg\\touxiang.jpg"),"image/jpeg"); DataHandler imgDataHandler = new DataHandler(fileds); img.setDataHandler(imgDataHandler); img.setHeader("Content-ID", "<touxiang.jpg>"); img.setFileName(MimeUtility.encodeText("头像.jpg")); //设置文字内容 content.setContent("<div style=‘color:red;font-size:18px;‘>从163发来的邮件</div>:我这里有一张图片<img src=‘cid:touxiang.jpg‘ alt=‘touxiang‘ width=\"100px\" height=‘100px‘ />,好看吗?", "text/html;charset=utf-8"); /*正文内容设置结束*/ /*下面为设置附件*/ attach1.setDataHandler(new DataHandler(new FileDataSource("E:\\others\\firefox.txt"))); attach1.setFileName(MimeUtility.encodeText("文件1.txt")); attach2.setDataHandler(new DataHandler(new FileDataSource("E:\\others\\java.txt"))); attach2.setFileName(MimeUtility.encodeText("file文件2.txt")); msg.saveChanges(); OutputStream os = new FileOutputStream("E:\\demo4.eml"); msg.writeTo(os); os.close(); Transport trans = session.getTransport(); trans.connect("smtp.163.com", 25, "zyh5540", "test"); trans.sendMessage(msg,msg.getAllRecipients()); } }
https://blog.csdn.net/zyh5540/article/details/19019397
原文地址:https://www.cnblogs.com/achengmu/p/10955765.html
时间: 2024-11-07 01:47:32