admin管理员组

文章数量:1559088

  • 如果配置写在application.properties 

就直接引入使用即可

@resource
private javamailsenderimpl javamailsender;
  • 如果配置在apollo需要写一个配置类

配置如下:

spring.mail.username = xxxx@xx.xx
spring.mail.password = xxxxx
spring.mail.host = smtp.exmail.qq
spring.mail.protocol = smtps
spring.mail.properties.mail.smtps.ssl.enable = true
spring.mail.port = 465
category.mgt = xxxxxxxx@xx.xx

配置类如下:


import lombok.getter;
import lombok.setter;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.cloud.context.config.annotation.refreshscope;
import org.springframework.context.annotation.bean;
import org.springframework.mail.javamail.javamailsenderimpl;
import org.springframework.stereotype.component;
import java.nio.charset.standardcharsets;
import java.util.properties;
@getter
@setter
@component(mailconfig.bean_name)
@refreshscope
@configurationproperties(prefix = mailconfig.bean_config_prefix)
public class mailconfig {
    public static final string bean_name = "mailconfig";
    public static final string bean_config_prefix = "spring.mail";
    private string username;
    private string password;
    private string host;
    private int port;
    private string protocol;
    @bean
    public javamailsenderimpl mailsender() {
        javamailsenderimpl javamailsender = new javamailsenderimpl();
        javamailsender.setusername(username);
        javamailsender.setpassword(password);
        javamailsender.sethost(host);
        javamailsender.setport(port);
        javamailsender.setprotocol(protocol);
        properties properties = new properties();
        properties.put("mail.smtps.ssl.enable", true);
        javamailsender.setdefaultencoding(standardcharsets.utf_8.name());
        javamailsender.setjavamailproperties(properties);
        return javamailsender;
    }
}

发送邮件的util


import org.springframework.mail.javamail.javamailsenderimpl;
import org.springframework.mail.javamail.mimemessagehelper;
import javax.mail.messagingexception;
import javax.mail.internet.mimemessage;
public class mailutil {
    public static boolean sendemail(javamailsenderimpl javamailsender, string sendfromemail, string[] sendtoemail, string msg, string title) {
        try {
            if (sendtoemail.length == 0) {
                return false;
            }
            mimemessage mimemessage = javamailsender.createmimemessage();
            mimemessagehelper mimemessagehelper = new mimemessagehelper(mimemessage);
            mimemessagehelper.setfrom(sendfromemail);
            mimemessagehelper.setto(sendtoemail);
            mimemessagehelper.settext(msg, true);
            mimemessagehelper.setsubject(title);
            javamailsender.send(mimemessagehelper.getmimemessage());
            return true;
        } catch (messagingexception e) {
            e.printstacktrace();
            return false;
        }
    }
}

使用:

        boolean result = mailutil.sendemail(javamailsender, mailconfig.getusername(), sendtoemail, msg, "邮件提醒");

本文标签: 腾讯企业邮箱springboot