前言:现在基本都是SpringBoot开发项目,所以要先在yml文件配置一些密钥之类的;
email:
emailFrom: 22**5@qq.com
host: smtp.**com
authorizationCode: *******
emailContent:
emailTo: lwd****06@163.com
sendSMS:
signName: **
endpoint: ******
templateCode: *****
accessKeyId: ***********
accessKeySecret: ******************
一.发送短信SMSUtil
@Slf4j
@Component
public class SMSUtil {
@Value("${sendSMS.accessKeyId}")
private String accessKeyId;
@Value("${sendSMS.accessKeySecret}")
private String accessKeySecret;
@Value("${sendSMS.signName}")
private String signName;
@Value("${sendSMS.templateCode}")
private String templateCode;
@Value("${sendSMS.endpoint}")
private String endpoint;
public SendSmsResponse sendSMS(String phoneNumber, String code) throws Exception {
com.aliyun.dysmsapi20170525.Client client = createClient(accessKeyId, accessKeySecret);
HashMap<String, String> map = new HashMap<>();
map.put("code", code);
code = new JSONObject(map).toString();
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(phoneNumber)
.setSignName(signName)
.setTemplateCode(templateCode)
.setTemplateParam(code);
return client.sendSms(sendSmsRequest);
}
public com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
config.endpoint = endpoint; // 域名
return new com.aliyun.dysmsapi20170525.Client(config);
}
}
二.发送邮箱MailUtil
@Slf4j
@Component
public class MailUtil {
@Value("${email.emailFrom}")
String emailFrom;
@Value("${email.host}")
String host;
@Value("${email.authorizationCode}")
String authorizationCode;
@Value("${email.emailContent}")
String emailContent;
@Value("${email.emailTo}")
String emailTo;
public void sendNoticeEmails(String description) throws Exception {
// 1.创建连接对象javax.mail.Session
// 2.创建邮件对象 javax.mail.Message
// 3.发送一封激活邮件
String from = emailFrom;// 发件人电子邮箱
Properties properties = System.getProperties();// 获取系统属性
properties.setProperty("mail.smtp.host", host);// 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
properties.setProperty("mail.smtp.auth", "true");// 打开认证
//QQ邮箱需要下面这段代码,163邮箱不需要
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 1.获取默认session对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailFrom, authorizationCode); // 发件人邮箱账号、授权码
}
});
// 2.创建邮件对象
Message message = new MimeMessage(session);
// 2.1设置发件人
message.setFrom(new InternetAddress(from));
// 2.2设置接收人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));
// 2.3设置邮件主题
message.setSubject("系统异常通知");
// 2.4设置邮件内容
String content = "<html><head></head><body><h1>系统发生异常通知,请前往后台处理</h1><h3>通知信息为:" + description + "</h3></body></html>";
message.setContent(content, "text/html;charset=UTF-8");
// 3.发送邮件
Transport.send(message);
}
}