发送邮箱和短信

发送邮箱和短信

一、邮箱:(这里用QQ邮箱eg)

步骤1、设置QQ邮箱

在这里插入图片描述
在这里插入图片描述
开启并获得(秘钥)ptrylchonikrbcXX(不是真的)

步骤2、导入依赖(未用Spring Boot框架)

 <!-- 邮件发送 -->
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>

步骤3、Spring整合email发送 (applicationContext-emial.xml)

 <!-- JavaMail相关配置   邮件发送配置 -->
	    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <property name="host" value="smtp.qq.com" />
            
            <!-- 发送方邮箱  应该写易购商城企业邮箱-->
            <property name="username" value="742558797@qq.com" />
            
            <!-- 发送秘钥  不是 邮箱密码 也不是QQ密码  -->
            <property name="password" value="ptrylchonikrbcXX" />
            
            <!-- <property name="password" value="nvmabflfbikubcgh" /> -->
            
            <!-- 协议 -->
            <property name="protocol" value="smtp" />
            
            <property name="defaultEncoding" value="utf-8"></property>
            
            <property name="javaMailProperties">
                <props>
                    <prop key="mail.smtp.auth">true</prop>
                    <prop key="mail.smtp.from">742558797@qq.com</prop>
                    <prop key="mail.debug">true</prop>
                </props>
            </property>
		
	</bean>

步骤4、测试类

  @Autowired
    JavaMailSenderImpl sender;
    @SneakyThrows
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);//将本类中的所有所有属性对象传入Spring框架中
        
        System.out.println("正在发送邮件"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        List<Product> products = productService.list(new QueryWrapper<Product>().eq("pdelete", 0)
                .inSql("pid", "select pid from t_product where pwarn>pcount"));

        this.excel(products);//这个方法创建一个Excel文件(D:\\商品信息统计表.xls)

       /* System.out.println("文件开始发送");下面是简单格式,不加附件
        SimpleMailMessage smm=new SimpleMailMessage();
        smm.setFrom(sender.getUsername());
        smm.setTo("742558797@qq.com");
        smm.setSubject("库存不足的商品");
        smm.setText("赵青松,库存不足商品有"+products);
        sender.send(smm);*/
        //使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容(加附件传输)
        MimeMessage msg = sender.createMimeMessage();
        //创建MimeMessageHelper对象,处理MimeMessage的辅助类
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);
        //使用辅助类MimeMessage设定参数
        helper.setFrom(sender.getUsername());
        helper.setTo("742558797@qq.com");
        helper.setSubject("库存商品");
        helper.setText("赵青松,库存不足商品");

        FileSystemResource file = new FileSystemResource("D:\\商品信息统计表.xls");
        //helper.addInline("file", file);
        helper.addAttachment("商品信息统计表.xls", file);
        sender.send(msg);
    }

得到的结果为:
在这里插入图片描述

一、短信:(这里选择互亿无线)(免费10条)

1、导入依赖

 <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>

    <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1</version>
    </dependency>

2、获取接口

在这里插入图片描述

package com.System.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.IOException;

/**
 * @BelongsProject: WarnSystem
 * @BelongsPackage: com.warnsystem.utils
 * @CreateTime: 2020-08-05 10:24
 * @Description: 短信发送工具类
 */
public class MessageUtils {

    //接口地址
    private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

    public static void main(String [] args) {

        //远程调用工具类
        HttpClient client = new HttpClient();
        //发送POST请求
        PostMethod method = new PostMethod(Url);
        //数据编码
        client.getParams().setContentCharset("GBK");
        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=GBK");

        //随机数。模拟验证码
        int mobile_code = (int)((Math.random()*9+1)*100000);

        //短信内容(如果是试用用户,短信内容模板不能修改,只能测试!)
        String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");
        System.out.println(mobile_code);

        NameValuePair[] data = {//提交短信
                new NameValuePair("account", "C58470566"), //查看用户名是登录用户中心->验证码短信->产品总览->APIID
                new NameValuePair("password", " de23530d8570be5214ff4e17998xxxx"),  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
                //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
                new NameValuePair("mobile", "19981855234"),//短信发送给谁
                new NameValuePair("content", content), //短信发送内容
        };
        method.setRequestBody(data);

        try {
            client.executeMethod(method);

            //调用短信接口之后响应
            String SubmitResult =method.getResponseBodyAsString();

            //System.out.println(SubmitResult);

            Document doc = DocumentHelper.parseText(SubmitResult);
            Element root = doc.getRootElement();

            String code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");

            System.out.println(code);
            System.out.println(msg);
            System.out.println(smsid);
            if("2".equals(code)){
                System.out.println("短信提交成功");
            }
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值