spring boot发送邮件

1、在pom文件中引入依赖

spring boot发送邮件_第1张图片

2、application.properties文件中配置发送方的配置信息

spring.mail.host=smtp.sina.com

[email protected] //发送邮件的邮件地址

spring.mail.password=****** //发送邮件的登陆密码

spring.mail.port=25

spring.mail.protocol=smtp

3、示例参考代码

@RestController

public classSendMailController {

Loggerlogger= LoggerFactory.getLogger(SendMailController.class);

@Autowired

privateJavaMailSenderImplmailSender;

/**

*发送纯文本内容

*/

@RequestMapping(value="/sendTxtMail")

public voidsendTxtMail() {

SimpleMailMessage simpleMailMessage =newSimpleMailMessage();

//设置收件人,寄件人

simpleMailMessage.setTo(newString[] {"接收邮件的邮件地址"});

simpleMailMessage.setFrom("发送邮件的邮件地址");

simpleMailMessage.setSubject("Spring Boot Mail邮件测试【文本】");

simpleMailMessage.setText("这里是一段简单文本。");

//发送邮件

mailSender.send(simpleMailMessage);

logger.info("邮件已发送");

}

/**

*发送html内容

*@throwsException

*/

@RequestMapping(value="/sendHtmlMail")

public voidsendHtmlMail()throwsException {

MimeMessage mimeMessage =mailSender.createMimeMessage();

MimeMessageHelper mimeMessageHelper =newMimeMessageHelper(mimeMessage,true);

mimeMessageHelper.setTo(newString[] {"接收邮件的邮件地址"});

mimeMessageHelper.setFrom("发送邮件的邮件地址");

mimeMessageHelper.setSubject("Spring Boot Mail邮件测试【HTML】");

StringBuilder sb =newStringBuilder();

sb.append("");

sb.append("

这是一封测试激活的邮件

http://localhost:8080/regeinsterController/"+UUIDUtils.getUuid()+"

");

sb.append("");

//启用html

mimeMessageHelper.setText(sb.toString(), true);

//发送邮件

mailSender.send(mimeMessage);

logger.info("邮件已发送");

}

/**

*发送包含内嵌图片的邮件

*@throwsException

*/

@RequestMapping(value="/sendAttachedImageMail")

public voidsendAttachedImageMail()throwsException {

MimeMessage mimeMessage =mailSender.createMimeMessage();

// multipart模式

MimeMessageHelper mimeMessageHelper =newMimeMessageHelper(mimeMessage, true);

mimeMessageHelper.setTo("接收邮件的邮件地址");

mimeMessageHelper.setFrom("发送邮件的邮件地址");

mimeMessageHelper.setSubject("Spring Boot Mail邮件测试【图片】");

StringBuilder sb =newStringBuilder();

sb.append("");

sb.append("

spring邮件测试

hello!this is spring mail test。

");

// cid为固定写法,imageId指定一个标识

sb.append("");

sb.append("");

//启用html

mimeMessageHelper.setText(sb.toString(), true);

//设置imageId

FileSystemResource img =newFileSystemResource(newFile("D:/11.jpg"));

mimeMessageHelper.addInline("imageId",img);

//发送邮件

mailSender.send(mimeMessage);

logger.info("邮件已发送");

}

/**

*发送包含附件的邮件

*@throwsException

*/

@RequestMapping(value="/sendAttendedFileMail")

public voidsendAttendedFileMail()throwsException {

MimeMessage mimeMessage =mailSender.createMimeMessage();

// multipart模式

MimeMessageHelper mimeMessageHelper =newMimeMessageHelper(mimeMessage, true,"utf-8");

mimeMessageHelper.setTo(newString[] {"接收邮件的邮件地址"});

mimeMessageHelper.setFrom("发送邮件的邮件地址");

mimeMessageHelper.setSubject("Spring Boot Mail邮件测试【附件】");

StringBuilder sb =newStringBuilder();

sb.append("");

sb.append("

spring邮件测试

hello!this is spring mail test。

");

sb.append("");

//启用html

mimeMessageHelper.setText(sb.toString(), true);

//设置附件

FileSystemResource img =newFileSystemResource(newFile("D:/11.jpg"));

mimeMessageHelper.addAttachment("image.jpg",img);

//发送邮件

mailSender.send(mimeMessage);

logger.info("邮件已发送");

}

}

你可能感兴趣的:(spring boot发送邮件)