【Java】【32】发邮件

功能介绍:前端填写某些信息后,提交到后台,后台保存信息到数据库,并发送一份有相关信息的邮件给管理员

正文:

1,Controller层

@ResponseBody
@RequestMapping(value = "submit", method = RequestMethod.POST)
public Result submit(SomeInfo someInfo){   // SomeInfo:要传输的值的实体类
    Result result = new Result(); //返回值自己定义即可
    result = TheService.submit(someInfo); //保存到数据库
    if(result.isSuccess){ //保存成功,发送邮件
        Sendmail send = new Sendmail(someInfo);
        //启动线程,线程启动之后就会执行run方法来发送邮件
        send.start();
    }
    return result;
}

2,Sendmail类

package com.bfweb.util;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import com.bfweb.view.SomeInfo;

public class Sendmail extends Thread{    
    private String from = "xxxMail@test.com"; //用于给管理员发送邮件的邮箱
    private String username = "xxxMailName"; //邮箱的用户名
    private String password = "password"; //邮箱的密码
    private String host = "mail.xx.com"; //发送邮件的服务器地址,可询问运维,我是用的我们公司的

    private SomeInfo someInfo;
    public Sendmail(SomeInfo someInfo){
        this.someInfo = someInfo;
    }
  
    /* 重写run方法的实现,在run方法中发送邮件给指定的用户
    * @see java.lang.Thread#run()
    */
    public void run() {
      try{
          Properties prop = new Properties();
          prop.setProperty("mail.host", host);
          prop.setProperty("mail.transport.protocol", "smtp");
          prop.setProperty("mail.smtp.auth", "true");

          Session session = Session.getInstance(prop);
          session.setDebug(true);
          Transport ts = session.getTransport();
          ts.connect(host, username, password);
          Message message = createEmail(session,operationView);
          ts.sendMessage(message, message.getAllRecipients());
          ts.close();
      }catch (Exception e) {
          throw new RuntimeException(e);
      }
    }

    public Message createEmail(Session session, SomeInfo someInfo) throws Exception{
      
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));
      message.setRecipient(Message.RecipientType.TO, new InternetAddress("yyyMail@test.com")); //info@bfsuma.com
      message.setSubject(MimeUtility.encodeText("邮件提醒", MimeUtility.mimeCharset("gb2312"), null));
      
      String info = "Code : %s<br>" +
            "Name : %s<br>" +
            "Gender : %s<br>" +
            "Email: %s<br>" +
            "Admin Url : <a href='%s'>%s</a><br>";
      info = String.format(info, someInfo.getCode(), someInfo.getName(), someInfo.getGender(), someInfo.getEmail(), "https://www.baidu.com/", "点击进入后台");
      message.setContent(info,"text/html;charset=UTF-8");
      message.saveChanges();
      return message;
    }
}

参考博客:

1,JavaWeb学习总结(五十二)——使用JavaMail创建邮件和发送邮件 - 孤傲苍狼 - 博客园
https://www.cnblogs.com/xdp-gacl/p/4216311.html

2,java程序向指定邮箱发邮件 - v_yang_guang_v的专栏 - CSDN博客
https://blog.csdn.net/v_yang_guang_v/article/details/45001075

3,邮件服务返回代码含义 - weixin_34256074的博客 - CSDN博客
https://blog.csdn.net/weixin_34256074/article/details/86168513

posted @ 2019-06-26 18:56  花生喂龙  阅读(176)  评论(0编辑  收藏  举报