java--邮件与短信
发布日期:2021-05-04 19:53:57 浏览次数:30 分类:精选文章

本文共 7877 字,大约阅读时间需要 26 分钟。

邮件

一 :依赖

JavaMail API 和Java Activation Framework (JAF)

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

javax.mail
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邮箱授权码

在这里插入图片描述
2.模板调用

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
com.aliyun
aliyun-java-sdk-core
4.5.3
*/public class SendSms { public static void main(String[] args) { DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "
", "
"); 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();        }    }}

在这里插入图片描述

上一篇:Spring--05--AOP原生实现方式
下一篇:Spring--03--AOP简介

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年03月31日 08时19分32秒