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
j9九游会老哥俱乐部交流区的版权声明:本文标题:springboot 发送腾讯企业邮箱 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1727421829a1113667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论