- 2023-04-11 16:58:52
- 2801 热度
- 0 评论
之前已经看到,如果消息包指定了他们之间的关系,增加一个文件进去,邮箱就会显示成普通附件
但是那毕竟不是附件,附件就是附件不能含糊
其实增加附件很简单
指定消息包内元素为混合关系
增加附件并指定附件的名称
package com.mail; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; 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; public class Html_File_InnerFile { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.put( "mail.smtp.host ", "smtp.163.com "); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props); Message message = new MimeMessage(session); InternetAddress from = new InternetAddress("test20120711120200@163.com"); from.setPersonal(MimeUtility.encodeText("风中落叶<test20120711120200@163.com>")); message.setFrom(from); InternetAddress to = new InternetAddress("test20120711120200@163.com"); message.setRecipient(Message.RecipientType.TO, to); message.setSubject(MimeUtility.encodeText("强哥邀请,谁敢不从!")); message.setSentDate(new Date()); MimeMultipart msgMultipart = new MimeMultipart("mixed");// 指定为混合关系 message.setContent(msgMultipart); // 邮件内容 MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent( "<body background='http://dl.iteye.com/upload/picture/pic/110267/e244bda9-9034-36e3-87fd-807629b84222.jpg'>" + "<div style='position: absolute; left: 390px; top: 150px;height: " + "100px;width: 200px;' align='center'>" + "<font color='red'>这是测试邮件,请勿回复</font>" + "</div></body>", "text/html;charset=UTF-8"); // TODO 组装的顺序非常重要,一定要先组装文本域,再组装文件 msgMultipart.addBodyPart(htmlPart); // 组装附件 MimeBodyPart file = new MimeBodyPart(); FileDataSource file_datasource = new FileDataSource("D:\\img201008031058340.zip"); DataHandler dh = new DataHandler(file_datasource); file.setDataHandler(dh); // 附件区别内嵌内容的一个特点是有文件名,为防止中文乱码要编码 file.setFileName(MimeUtility.encodeText(dh.getName())); msgMultipart.addBodyPart(file); message.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect("smtp.163.com", 25, "test20120711120200", "test123456"); transport.sendMessage(message, message.getAllRecipients()); transport.close(); System.out.println("发送完毕"); } }
主要组装元素的时候一定要先组装邮件内容再组装附件
无论在 客户端工具还是邮箱,都是提示附件,邮件还是带背景图片的
附件名称是你自己指定的
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Java(82)
- Cloud(82)
- Spring Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- IDE(24)
- Dalston(24)
- JDBC(22)
- IDEA(22)
- mongoDB(22)
- MVC(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- SpringBoot(17)
- Docker(17)
- Eclipse(16)
- Vue(16)
- Git(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Oracle(14)
- jdk(14)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- XML(13)
- JdbcTemplate(13)
- OAuth(13)
- Nacos(13)
- Pro(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- stream(11)
- int(11)
- Myeclipse(11)
- not(10)
- Bug(10)
- maven(9)
- Map(9)
- Hystrix(9)
- ast(9)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- Swagger(8)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- read(6)
- Freemarker(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- Server(6)
- nginx(6)
- ueditor(6)
- jar(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- star(6)
- and(6)
- Struts(5)
- string(5)
- script(5)
- Syntaxhighlighter(5)
- Tool(5)
- Controller(5)
- swagger2(5)
- ldquo(5)
- input(5)
- Servlet(5)
- Config(5)
- discuz(5)