开启并获得(秘钥)ptrylchonikrbcXX(不是真的)
<!-- 邮件发送 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- 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>
@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);
}
得到的结果为:
<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>
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();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183996.html原文链接:https://javaforall.cn