美文网首页
spring boot发送邮件

spring boot发送邮件

作者: fangzy0723 | 来源:发表于2017-09-29 22:03 被阅读0次

    1、在pom文件中引入依赖

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

    spring.mail.host=smtp.sina.com

    spring.mail.username=aaa@sina.com //发送邮件的邮件地址

    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发送邮件

          本文链接:https://www.haomeiwen.com/subject/oujwextx.html