spring mail 发送html,使用spring mail发送html邮件的示例代码

本文展示一下如何使用spring mail来发送html邮件。

maven

org.springframework.boot

spring-boot-starter-mail

发送图片

public void send(string from, string[] tomails, string subject, string text,

map inlines) throws exception{

mimemessage mimemessage = mailsender.createmimemessage();

mimemessagehelper helper = new mimemessagehelper(mimemessage, true);

helper.setfrom(from);

helper.setto(tomails);

helper.setsubject(subject);

helper.settext(text, true); //支持html

// 增加inline

if(inlines != null){

for(map.entry entry: inlines.entryset()){

if(entry.getvalue() instanceof classpathresource){

helper.addinline(entry.getkey(), (resource) entry.getvalue());

}

}

}

mailsender.send(mimemessage);

}

测试

发送实例

classpathresource classpathresource = new classpathresource("image_2.png");

map att = new hashmap<>();

att.put("image",classpathresource);

string content = "

spring mail发送实例

";

try{

mailservice.send(new string[]{"xxxxx@163.com"},"spring mail发送实例",content,att);

}catch (exception e){

e.printstacktrace();

}

异常

org.springframework.mail.mailsendexception: failed messages: com.sun.mail.smtp.smtpsendfailedexception: 554 dt:spm 126 smtp7,dsmowab3u6x1_ldzjiz+aw--.26008s3 1505230070,please see http://mail.163.com/help/help_spam_16.htm?ip=123.65.107.103&hostid=smtp7&time=1505230070

; message exception details (1) are:

failed message 1:

com.sun.mail.smtp.smtpsendfailedexception: 554 dt:spm 126 smtp7,dsmowab3u6x1_ldzjiz+aw--.26008s3 1505230070,please see http://mail.163.com/help/help_spam_16.htm?ip=123.65.107.103&hostid=smtp7&time=1505230070

at com.sun.mail.smtp.smtptransport.issuesendcommand(smtptransport.java:2267)

at com.sun.mail.smtp.smtptransport.finishdata(smtptransport.java:2045)

at com.sun.mail.smtp.smtptransport.sendmessage(smtptransport.java:1260)

at org.springframework.mail.javamail.javamailsenderimpl.dosend(javamailsenderimpl.java:448)

at org.springframework.mail.javamail.javamailsenderimpl.send(javamailsenderimpl.java:345)

at org.springframework.mail.javamail.javamailsenderimpl.send(javamailsenderimpl.java:340)

错误码554

554 dt:spm 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;

被网易邮箱识别为垃圾邮件了,有个歪招,就是把发送邮箱添加到cc里头

helper.setcc(from);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是一个简单的示例代码,演示了如何使用Spring Boot Starter Mail发送电子邮件: 首先,确保在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 然后,在application.properties文件中配置邮件服务器的相关信息: ```properties # 邮件服务器主机名 spring.mail.host=your-mail-server-hostname # 邮件服务器端口号 spring.mail.port=your-mail-server-port # 邮件服务器用户名 spring.mail.username=your-username # 邮件服务器密码 spring.mail.password=your-password # 邮件传输协议 spring.mail.protocol=smtp # 是否启用调试模式 spring.mail.properties.mail.debug=true ``` 接下来,创建一个邮件发送服务类,例如MailService.java: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class MailService { @Autowired private JavaMailSender mailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); mailSender.send(message); } } ``` 最后,在需要发送邮件的地方调用MailService的sendEmail方法即可: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplication { @Autowired private MailService mailService; public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } public void sendEmail() { String to = "recipient@example.com"; String subject = "Hello"; String text = "This is a test email."; mailService.sendEmail(to, subject, text); } } ``` 这是一个简单的示例代码,演示了如何使用Spring Boot Starter Mail发送电子邮件。你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值