阿里云短信
导入jar包
<!-- 阿里云短信-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
抽离成工具类
// 短信发送工具类
@Component
public class SmsUtils {
@Async
public void sendSms(String phoneNums){
DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIdYswPUVgOZSA", "");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("PhoneNumbers", phoneNums);
request.putQueryParameter("SignName", "无为微商");
request.putQueryParameter("TemplateCode", "SMS_142953308");
String code=randCode(4);
request.putQueryParameter("TemplateParam", "{\"code\":"+code+"}");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
System.out.println("发送短信成功");
} catch (ServerException e) {
System.out.println("发送短信失败");
e.printStackTrace();
} catch (ClientException e) {
System.out.println("发送短信失败");
e.printStackTrace();
}
}
// 生成随机号码
public String randCode(int length){
Random random =new Random();
StringBuilder stringBuilder=new StringBuilder();
for (int i = 0; i <length ; i++) {
int code=random.nextInt(10);
stringBuilder.append(code);
}
return stringBuilder.toString();
}
}
邮箱工具类
package cn.itsource.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.mail.internet.MimeMessage;
@Component // 组件注解 交给spring管理
public class EmailUtils {
@Value("${email.username}")
String username;
@Autowired
JavaMailSenderImpl emailSender;
// 简单的文本邮件
@Async // 代表异步方法 底层 线程 new thread
public void SimpleMailMessage(String sendto,String title,String content) throws Exception {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(username);// 发送人
mailMessage.setTo(username);// 收件人
mailMessage.setSubject(title);// 标题
mailMessage.setText(content); // 内容
emailSender.send(mailMessage);
System.err.println("发送成功");
}
// html
public void htmlMailMessage(String sendto,String title,String content) {
MimeMessage mimeMsg = emailSender.createMimeMessage();
try {
//String html = "<h1>隐世集团祝福你</h1><img src='https://rescdn.qqmail.com/qqmail/images/newstart2019.jpg'>";
// 复杂邮件邮件对象
MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true);
helper.setTo(sendto); // 收件人
helper.setFrom(username); // 发件人
helper.setSubject(title);
helper.setText(content, true);
emailSender.send(mimeMsg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
邮件集成Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<!-- 支持异步方法执行 -->
<task:executor id="myexecutor" pool-size="10" />
<!--开启这个配置,spring才能识别@Scheduled注解-->
<task:annotation-driven executor="myexecutor"/>
<!-- 邮件的配置-->
<bean id="emailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!--邮件服务器-->
<property name="host" value="${email.host}"/>
<property name="protocol" value="${email.protocol}"/>
<!--邮箱号码-->
<property name="username" value="${email.username}"/>
<!--邮箱授权码-->
<property name="password" value="${email.password}"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
<!--Spring模拟HTTP请求类 RestTemplate-->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
</bean>
</beans>
# webPath 开发环境
email.host=smtp.qq.com
email.protocol=smtp
email.username=464303529@qq.com
email.password=pjotckuxfgyubgfh