spring----邮件发送

概念

  邮件发送遵循SMTP协议。邮件的发送是邮件服务器进行发送的。我们可以自己建立邮件服务器(百度可以下载资源【易邮邮件服务器(eyoumailserver)】),或者我们直接使用qq的邮件服务器。

  如果需要读取我们创建的eyoumailserver邮件服务器中接受到的邮件信息,需要下载一个foxmail客户端,里面绑定邮件服务器的地址。

 

pom.xml 依赖

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.5</version>
</dependency>

使用

官网:http://commons.apache.org/proper/commons-email/userguide.html

Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com"); //qq邮箱的是  smtp.qq.com
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password")); //密码是qq邮箱授权码 
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com"); //发件人
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");  //收件人
email.send();

 

使用spring内置的发送邮箱

  添加spring包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring-webmvc.version}</version>
</dependency>
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>${javax.mail.version}</version>
</dependency>

  

示例

mail.host=smtp.qq.com
#发送人的邮箱和密码
mail.username=184xxxxx@qq.com
mail.password=lpgxxxxxxxxx  //授权码(qq邮箱需要)

  

@Service
public class MailServiceImpl implements IMailService {

    @Value("${mail.host}")
    private String host;

    @Value("${mail.username}")
    private String username;

    @Value("${mail.password}")
    private String password;

    @Override
    public void sendMail(String target, String title, String content) {
        try {
            JavaMailSenderImpl sender = new JavaMailSenderImpl();
            // 设置SMTP服务器地址
            sender.setHost(host);
            // 创建好一个邮件对象
            MimeMessage message = sender.createMimeMessage();
            // 创建一个邮件助手
            MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");
            // 通过heloper设置邮件相关内容
            // 设置目标
            helper.setTo(target);
            // 设置from
            //helper.setFrom("Admin@xmg.com"); // 这个地方一定要和properties中的username一样
            // 这里有两个参数,第一个form是用来规定发件人的邮箱地址的,第二个是发件人的名字,可以自定义自定义
            helper.setFrom("1847003070@qq.com", "系统管理员");

            // 设置邮件标题
            helper.setSubject(title);
            // 设置邮件内容
            helper.setText(content, true);

            // 设置发送邮件的账号和密码
            sender.setUsername(username);
            sender.setPassword(password);

            //
            Properties prop = new Properties();
            prop.put("mail.smtp.auth", "true");// 设置发送邮件需要身份认证
            prop.put("mail.smtp.timeout", "25000");// 设置发送超时时间
            sender.setJavaMailProperties(prop);

            // 发送邮件
            sender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("发送邮件失败!");
        }
    }
}

  

 

posted @ 2019-06-09 08:55  小名的同学  阅读(143)  评论(0编辑  收藏  举报