邮件发送

1:整合springboot的发送参见:

https://blog.csdn.net/zyw_java/article/details/81635375

2:常见的邮箱服务器地址:

https://blog.csdn.net/chuanyu/article/details/46740287

3:如何查询公司的outlook邮箱服务器地址:

https://zhidao.baidu.com/question/39774692.html

 

关键点:

密码为三方授权码

待解决问题:

如何查询企业邮箱的服务地址?

 

demo

<dependency><!-- 4.引入email API-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

配置

spring:
  application:
    name: test-config-server
  profiles:
  mail:
    host: smtp.163.com #发送邮件服务器
    username: ppyun7c@163.com #163邮箱
    password: wdsqm01 #客户端授权码
    protocol: smtp #发送邮件协议
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 465 #端口号465或994
    properties.mail.display.sendmail: XIEDONGXU #可以任意
    properties.mail.display.sendname: Spring Boot Guide Email #可以任意
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8
    from: ppyun7c@163.com #与上面的username保持一致

 

代码

package com.test.domi.service;

import javax.mail.MessagingException;

public interface IMailService {
    /**
     * 发送文本邮件
     * @param to
     * @param subject
     * @param content
     */
    public void sendSimpleMail(String to, String subject, String content, String... cc);

    /**
     * 发送HTML邮件
     * @param to
     * @param subject
     * @param content
     * @throws MessagingException
     */
    public void sendHtmlMail(String to, String subject, String content, String... cc)throws MessagingException;

    /**
     * 发送带附件的邮件
     * @param to
     * @param subject
     * @param content
     * @param filePath
     * @throws MessagingException
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException;

    /**
     * 发送正文中有静态资源的邮件
     * @param to
     * @param subject
     * @param content
     * @param rscPath
     * @param rscId
     * @throws MessagingException
     */
    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc)throws MessagingException;

}

 实现类

package com.test.domi.service.impl;

import com.test.domi.service.IMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class IMailServiceImpl implements IMailService {

    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.from}")
    private String from;

    /**
     * 发送文本邮件
     * @param to
     * @param subject
     * @param content
     */
    @Override
    public void sendSimpleMail(String to, String subject, String content, String... cc) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setCc(cc);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    /**
     * 发送HTML邮件
     * @param to
     * @param subject
     * @param content
     */
    @Override
    public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException{
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setCc(cc);
        helper.setSubject(subject);
        helper.setText(content, true);
        mailSender.send(message);
    }


    /**
     * 发送带附件的邮件
     * @param to
     * @param subject
     * @param content
     * @param filePath
     * @throws MessagingException
     */
    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setCc(cc);
        helper.setSubject(subject);
        helper.setText(content, true);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, file);
        mailSender.send(message);
    }

    /**
     * 发送正文中有静态资源的邮件
     * @param to
     * @param subject
     * @param content
     * @param rscPath
     * @param rscId
     */
    @Override
    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setCc(cc);
        helper.setSubject(subject);
        helper.setText(content, true);
        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res);
        mailSender.send(message);
    }
}

 

 

controller

package com.test.domi.controller;

import com.test.domi.common.utils.ResultInfo;
import com.test.domi.common.utils.ResultUtil;
import com.test.domi.service.impl.IMailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private IMailServiceImpl mailService;//注入发送邮件的各种实现方法

    @GetMapping("/normal")
    public ResultInfo index(){
        try {
            mailService.sendSimpleMail("491431567@qq.com","SpringBoot Email","这是一封普通的SpringBoot测试邮件");
            String[] cc = {"491431567@qq.com","491431567@qq.com"};
            mailService.sendAttachmentsMail("491431567@qq.com","附件发送测试","这是附件","C:\\Users\\Domi\\Desktop\\ls.txt",cc);
            mailService.sendHtmlMail("491431567@qq.com","html测试","<body style=\"text-align: center;margin-left: auto;margin-right: auto;\">\n"
                    + "    <div id=\"welcome\" style=\"text-align: center;position: absolute;\" >\n"
                    +"        <h3>欢迎使用IJPay -By Javen</h3>\n"
                    +"        <span>https://github.com/Javen205/IJPay</span>"
                    + "        <div\n"
                    + "            style=\"text-align: center; padding: 10px\"><a style=\"text-decoration: none;\" href=\"https://github.com/Javen205/IJPay\" target=\"_bank\" ><strong>IJPay 让支付触手可及,欢迎Start支持项目发展:)</strong></a></div>\n"
                    + "        <div\n" + "            style=\"text-align: center; padding: 4px\">如果对你有帮助,请任意打赏</div>\n"
                    + "        <img width=\"180px\" height=\"180px\"\n"
                    + "            src=\"https://javen205.gitbooks.io/ijpay/content/assets/wxpay.png\">\n"
                    + "    </div>\n" + "</body>",cc);
        }catch (Exception ex){
            ex.printStackTrace();
            return ResultUtil.getFailResult("999999","失败",null);
        }
        return ResultUtil.getSuccessResult(null);
    }
}

 

posted @ 2018-10-06 23:36  老人与JAVA  阅读(236)  评论(0编辑  收藏  举报