当前位置: 代码网 > it编程>编程语言>Java > SpringBoot发送各种复杂格式邮件的示例详解

SpringBoot发送各种复杂格式邮件的示例详解

2024年11月26日 Java 我要评论
在spring boot中发送带有附件、嵌入资源(如图片)、抄送(cc)和密送(bcc)的复杂邮件,你可以使用javamailsender接口和mimemessagehelper类。邮件格式一封电子邮

在spring boot中发送带有附件、嵌入资源(如图片)、抄送(cc)和密送(bcc)的复杂邮件,你可以使用javamailsender接口和mimemessagehelper类。

邮件格式

一封电子邮件主要由以下几个关键部分组成,这些部分共同构成了邮件的整体结构和内容:

1.收件人(to)

这是邮件发送到的电子邮箱地址。确保你输入的地址是正确的,以避免邮件发送错误或丢失。

2.抄送(cc)(可选)

如果你希望其他人也能收到这封邮件,可以将他们的电子邮箱地址添加到抄送栏。抄送的人可以看到邮件的所有其他收件人。

3.密送(bcc)(可选)

密送允许你将邮件发送给其他人,但这些人的邮箱地址不会显示在邮件的收件人或抄送列表中。这通常用于发送敏感信息,同时保护收件人的隐私。

4.主题(subject)

邮件的主题是邮件内容的简短描述,应该准确反映邮件的要点或目的。一个清晰的主题有助于收件人快速了解邮件内容,并决定是否立即阅读。

5.正文(body)

正文是邮件的主要内容,包括开头问候、具体信息或请求、行动要求以及结尾感谢等。正文应该清晰、简洁、有条理,避免使用过多的行业术语或复杂的句子结构。

6.附件(attachments)(可选)

如果邮件中包含文件(如文档、图片、表格等),你可以将它们作为附件添加到邮件中。确保附件的大小不超过收件人邮箱的限制,并提醒收件人检查附件是否存在病毒或恶意软件。

7.签名(signature)

邮件签名通常位于正文的底部,包括你的全名、职位(如果适用)、公司名称、联系方式(如电话、邮箱等)以及可能的公司logo或社交媒体链接。签名有助于建立你的专业形象,并方便收件人联系你。

8.发送时间(sent time)

这不是由你手动填写的部分,而是邮件系统自动记录的。它显示了邮件发送的确切时间,有助于收件人了解邮件的时效性。

9.优先级(priority)(可选)

一些邮件系统允许你设置邮件的优先级(如高、中、低)。这有助于收件人根据邮件的重要性来安排阅读顺序。但请注意,不要滥用高优先级标记,以免降低其效果。

10.阅读回执(read receipt)(可选)

阅读回执是一种功能,当你发送邮件时,可以请求收件人在阅读邮件后发送一个确认回执。这有助于你了解邮件是否已被阅读,但请注意尊重收件人的隐私和意愿。

复杂邮件的发送

发送带抄送和密送的邮件

/**
 * 发送带抄送和密送的邮件
 *
 * @return "sendwithccandbcc"
 */
@getmapping("/sendwithccandbcc")
public string sendwithccandbcc() {
    simplemailmessage message = new simplemailmessage();
    message.setto("1508787838@qq.com");
    message.setcc("374785621@qq.com"); // 抄送
    message.setbcc("2426497890@qq.com"); // 密送
    message.setsubject("sendwithccandbcc");
    message.settext("sendwithccandbcc text");
    message.setfrom("morris131@163.com");
    javamailsender.send(message);
    return "sendwithccandbcc";
}

抄送的邮箱会显示在邮件的抄送列表,但是密送的邮箱不会显示在邮件的收件人或抄送列表。

发送带html格式的邮件

/**
 * 发送带html格式的邮件
 *
 * @return "sendwithhtml"
 */
@getmapping("/sendwithhtml")
public string sendwithhtml() throws messagingexception {
    mimemessage mimemessage = javamailsender.createmimemessage();

    mimemessagehelper mimemessagehelper = new mimemessagehelper(mimemessage);
    string htmlcontent = "<html><body><h1>sendwithhtml test</h1></body></html>";
    // 邮件发送来源
    mimemessagehelper.setfrom("morris131@163.com");
    // 邮件发送目标
    mimemessagehelper.setto("1508787838@qq.com");
    // 设置标题
    mimemessagehelper.setsubject("sendwithhtml");
    // 设置内容,并设置内容 html 格式为 true
    mimemessagehelper.settext(htmlcontent, true);

    javamailsender.send(mimemessage);
    return "sendwithhtml";
}

mimemessagehelper.settext(htmlcontent, true);的第二个参数必须设置为true,才会把第一个参数的内容当成html处理,否则会当成普通文本处理。

注意:邮件html中不能写javascript内容,会被忽略。

发送带内嵌图片的html格式邮件

/**
 * 发送带内嵌图片的html格式邮件
 *
 * @return "sendwithinlineimagehtml"
 */
@getmapping("/sendwithinlineimagehtml")
public string sendwithinlineimagehtml() throws messagingexception {
    mimemessage mimemessage = javamailsender.createmimemessage();

    string htmlcontent = "<html><body>" +
            "<h1>欢迎来到 spring boot 的世界</h1>" +
            "<image width='200' height='300' src='cid:java'>图片1 </image>" +//cid:是约定好的固定格式,只需要修改后面的变量
            "<image width='200' height='300' src='cid:springboot'>图片2 </image>" +
            "</body></html>";
    // 数组中的cid要和上面html中image中的cid一致,否则图片将设置失败
    map<string, string> map = new hashmap<>();
    map.put("java", "images/java.png");
    map.put("springboot", "images/springboot.jpg");
    // multipart参数要设置为true,代表要支持附件
    mimemessagehelper mimemessagehelper = new mimemessagehelper(mimemessage, true, standardcharsets.utf_8.name());
    // 邮件发送来源
    mimemessagehelper.setfrom("morris131@163.com");
    // 邮件发送目标
    mimemessagehelper.setto("1508787838@qq.com");
    // 设置标题
    mimemessagehelper.setsubject("sendwithimagehtml");
    // 设置内容,并设置内容 html 格式为 true
    mimemessagehelper.settext(htmlcontent, true);

    // 设置 html 中内联的图片
    for (map.entry<string, string> entry : map.entryset()) {
        // addinline() 方法 cid 需要 html 中的 cid (content id) 对应,才能设置图片成功,
        mimemessagehelper.addinline(entry.getkey(), new classpathresource(entry.getvalue()));
    }

    javamailsender.send(mimemessage);
    return "sendwithinlineimagehtml";
}

发送带有附件、内嵌资源时,需要将mimemessagehelper构造方法的第二个参数设置为true,否则会抛出如下异常:

java.lang.illegalstateexception: not in multipart mode - create an appropriate mimemessagehelper via a constructor that takes a 'multipart' flag if you need to set alternative texts or add inline elements or attachments.
	at org.springframework.mail.javamail.mimemessagehelper.getmimemultipart(mimemessagehelper.java:414) ~[spring-context-support-5.3.19.jar:5.3.19]

发送带附件的邮件

/**
 * 发送带附件的邮件
 *
 * @return "sendwithenclosure"
 */
@getmapping("/sendwithenclosure")
public string sendwithenclosure() throws messagingexception {

    map<string, string> map = new hashmap<>();
    map.put("test1", "files/test1.txt");
    map.put("test2", "files/test2.7z");

    mimemessage mimemessage = javamailsender.createmimemessage();

    // multipart参数要设置为true,代表要支持附件
    mimemessagehelper mimemessagehelper = new mimemessagehelper(mimemessage, true, standardcharsets.utf_8.name());
    // 邮件发送来源
    mimemessagehelper.setfrom("morris131@163.com");
    // 邮件发送目标
    mimemessagehelper.setto("1508787838@qq.com");
    // 设置标题
    mimemessagehelper.setsubject("sendwithwithenclosure");
    // 设置内容
    mimemessagehelper.settext("sendwithwithenclosure text");

    // 添加附件
    for (map.entry<string, string> entry : map.entryset()) {
        mimemessagehelper.addattachment(entry.getkey(), new classpathresource(entry.getvalue()));
    }
    javamailsender.send(mimemessage);
    return "sendwithenclosure";
}

发送freemarker模板邮件

maven中引入包:

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-freemarker</artifactid>
</dependency>

gradle中引入:

implementation 'org.springframework.boot:spring-boot-starter-freemarker:2.6.7'
// org.springframework.web.servlet.view.freemarker;
@resource
private freemarkerconfigurer freemarkerconfigurer;

/**
 * 发送freemarker模板邮件
 *
 * @return "sendwithfreemarker"
 */
@getmapping("/sendwithfreemarker")
public string sendwithfreemarker() throws messagingexception, ioexception, templateexception {
    mimemessage mimemessage = javamailsender.createmimemessage();
    mimemessagehelper messagehelper = new mimemessagehelper(mimemessage, true, "utf-8");
    // 邮件发送来源
    messagehelper.setfrom("morris131@163.com");
    // 邮件发送目标
    messagehelper.setto("1508787838@qq.com");
    // 设置邮件主题
    messagehelper.setsubject("sendwithfreemarker");
    // 获取模板
    template template = freemarkerconfigurer.getconfiguration().gettemplate("email.ftl", "utf-8");

    hashmap<string, object> map = new hashmap<>();
    map.put("username", "morris131");
    map.put("age", 18);

    // 填充数据并把模板转为字符串
    string content = freemarkertemplateutils.processtemplateintostring(template, map);
    // 设置邮件内容,第二个参数为true表示将发送内容设置为"text/html"
    messagehelper.settext(content, true);

    javamailsender.send(mimemessage);

    return "sendwithfreemarker";
}

html模板的位置:默认在resource/templates/目录下,模板文件名为email.ftl

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>hello email</title>
</head>
<body>
<p>用户:${username}</p>
<p>年龄:${age}</p>
</body>
</html>

到此这篇关于springboot发送各种复杂格式邮件的示例详解的文章就介绍到这了,更多相关springboot发送邮件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com