技术介绍(二)阿里云短信工具类,腾讯邮箱工具类

阿里云短信

导入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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]中的阿里云短信服务官方文档,您可以通过以下步骤来创建一个阿里云发送短信工具类: 1. 开通阿里云账号并登录,然后申请一个子权限账户,并授予该账户短信服务管理的权限。 2. 在短信服务控制台中申请签名和模板,具体的规则可以参考审核标准。 3. 根据短信接收对象的不同,选择相应的签名和模板。 4. 调用阿里云短信服务的接口来发送短信。 在编写工具类时,您可以使用引用\[2\]中提供的阿里云短信依赖,即aliyun-java-sdk-core和aliyun-java-sdk-dysmsapi。您可以在您的项目中添加这两个依赖,并按照官方文档提供的示例代码来调用阿里云短信服务的接口。 如果您需要同时向多个不同的手机号码发送不同签名的短信,可以参考引用\[3\]中的文档,该文档提供了相关的参考信息。 请注意,以上是一个简要的概述,具体的实现细节和代码编写需要根据您的具体需求和项目环境进行调整。建议您参考阿里云短信服务的官方文档和示例代码来完成您的工具类的编写。 #### 引用[.reference_title] - *1* *3* [阿里云短信服务工具类](https://blog.csdn.net/qq_40147276/article/details/93724745)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【阿里云短信服务 无认证 测试版本 附带短信发送工具类](https://blog.csdn.net/SPX113/article/details/125862420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值