
java--邮件与短信
2.模板调用
发布日期:2021-05-04 19:53:57
浏览次数:30
分类:精选文章
本文共 7877 字,大约阅读时间需要 26 分钟。
邮件
一 :依赖
JavaMail API 和Java Activation Framework (JAF)


javax.mail 1.4.7 javax.activation activation 1.1.1
二:配置模板
@gmail
// 文件名 SendEmail.java import java.util.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*; public class SendEmail{ public static void main(String [] args) { // 收件人电子邮箱 String to = "abcd@gmail.com"; // 发件人电子邮箱 String from = "web@gmail.com"; // 指定发送邮件的主机为 localhost String host = "localhost"; // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认session对象 Session session = Session.getDefaultInstance(properties); try{ // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject("This is the Subject Line!"); // 设置消息体 message.setText("This is actual message"); // 发送消息 Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } }}
@qq.com
/ 需要用户名密码邮件发送实例//文件名 SendEmail2.java//本实例以QQ邮箱为例,你需要在qq后台设置 import java.util.Properties; import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage; public class SendEmail2{ public static void main(String [] args) { // 收件人电子邮箱 String to = "xxx@qq.com"; // 发件人电子邮箱 String from = "xxx@qq.com"; // 指定发送邮件的主机为 smtp.qq.com String host = "smtp.qq.com"; //QQ 邮件服务器 // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true"); // 获取默认session对象 Session session = Session.getDefaultInstance(properties,new Authenticator(){ public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("xxx@qq.com", "qq邮箱授权码"); //发件人邮件用户名、授权码 } }); try{ // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject("This is the Subject Line!"); // 设置消息体 message.setText("This is actual message"); // 发送消息 Transport.send(message); System.out.println("Sent message successfully....from runoob.com"); }catch (MessagingException mex) { mex.printStackTrace(); } }}
三:项目实例
1.获取qq邮箱授权码

public class SendEmailUtils { public static void Sendemail (String errormessage){ // 收件人电子邮箱 String to = "xxxxxx@qq.com"; // 发件人电子邮箱 String from = "xxxxxx@qq.com"; // 指定发送邮件的主机为 smtp.qq.com String host = "smtp.qq.com"; //QQ 邮件服务器 // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true"); // 获取默认session对象 Session session = Session.getDefaultInstance(properties,new Authenticator(){ public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("xxxxxx@qq.com", "xxxxxx"); //发件人邮件用户名、授权码 } }); try{ // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject("动吧-系统异常!!!!"); // 设置消息体 message.setText(String.valueOf(errormessage)); // 发送消息 Transport.send(message); System.out.println("Sent message successfully....from runoob.com"); }catch (MessagingException mex) { mex.printStackTrace(); } } }
3.生成utils工具类 静态调用

短信
官网查询 —阿里云短信服务
申请签名 和 模板
添加依赖
com.aliyun aliyun-java-sdk-core 4.5.3 com.aliyun aliyun-java-sdk-dysmsapi 1.1.0
查看APidemo
import com.aliyuncs.CommonRequest;import com.aliyuncs.CommonResponse;import com.aliyuncs.DefaultAcsClient;import com.aliyuncs.IAcsClient;import com.aliyuncs.exceptions.ClientException;import com.aliyuncs.exceptions.ServerException;import com.aliyuncs.http.MethodType;import com.aliyuncs.profile.DefaultProfile;/*pom.xml*/public class SendSms { public static void main(String[] args) { DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", " com.aliyun aliyun-java-sdk-core 4.5.3 ", " "); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setSysMethod(MethodType.POST); request.setSysDomain("dysmsapi.aliyuncs.com"); request.setSysVersion("2017-05-25"); request.setSysAction("SendSms"); request.putQueryParameter("RegionId", "cn-hangzhou"); request.putQueryParameter("TemplateParam", "{\"code\":\"message\"}"); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } }}
注意 参数需要json格式
实际案例
public class SendSms { public static void sendsms (String message) { System.out.println("短信接收异常参数= "+message); DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "......accessKeyId", ".......accessSecret"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setSysMethod(MethodType.POST); request.setSysDomain("dysmsapi.aliyuncs.com"); request.setSysVersion("2017-05-25"); request.setSysAction("SendSms"); request.putQueryParameter("RegionId", "cn-hangzhou"); request.putQueryParameter("PhoneNumbers", "187xxxxxx"); request.putQueryParameter("SignName", "签名名字"); request.putQueryParameter("TemplateCode", "......模板号"); request.putQueryParameter("TemplateParam","{\"code\":\"" + message + "\"}" ) ; try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } }}
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年03月31日 08时19分32秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
稀疏数组
2019-03-06
js的严格模式
2019-03-06
idea的安装和无限期试用
2019-03-06
Oracle VM VirtualBox安装PVE虚拟机
2019-03-06
【转】如何用css限制文字长度,使溢出的内容用省略号…显示
2019-03-06
Android MediaPlayer setDataSource failed
2019-03-06
ASP.NET Core 实战:Linux 小白的 .NET Core 部署之路
2019-03-06
【nodejs原理&源码杂记(8)】Timer模块与基于二叉堆的定时器
2019-03-06
大前端的自动化工厂(1)——Yeoman
2019-03-06
数据仓库建模方法论
2019-03-06
虚拟机搭建hadoop环境
2019-03-06
DataStax Bulk Loader教程(四)
2019-03-06
物联网、5G世界与大数据管理
2019-03-06
Cassandra与Kubernetes
2019-03-06
.NET应用框架架构设计实践 - 概述
2019-03-06
Rust 内置 trait :PartialEq 和 Eq
2019-03-06
Hibernate(十四)抓取策略
2019-03-06
[菜鸟的设计模式之旅]观察者模式
2019-03-06
Spring-继承JdbcDaoSupport类后简化配置文件内容
2019-03-06