赞
踩
- <!--阿里云短信 -->
- <dependency>
- <groupId>com.aliyun</groupId>
- <artifactId>aliyun-java-sdk-core</artifactId>
- <version>4.1.0</version>
- <exclusions>
- <exclusion>
- <artifactId>commons-codec</artifactId>
- <groupId>commons-codec</groupId>
- </exclusion>
- <exclusion>
- <artifactId>activation</artifactId>
- <groupId>javax.activation</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.aliyun</groupId>
- <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
- <version>1.1.0</version>
- </dependency>
- <!--阿里云邮件 -->
- <dependency>
- <groupId>com.aliyun</groupId>
- <artifactId>aliyun-java-sdk-dm</artifactId>
- <version>3.1.0</version>
- </dependency>
-
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.7</version>
- </dependency>
- import com.aliyuncs.DefaultAcsClient;
- import com.aliyuncs.IAcsClient;
- import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
- import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
- import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
- import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
- import com.aliyuncs.exceptions.ClientException;
- import com.aliyuncs.http.MethodType;
- import com.aliyuncs.profile.DefaultProfile;
- import com.aliyuncs.profile.IClientProfile;
- import com.jointech.pushserver.constants.AlibabaConstants;
- import lombok.extern.slf4j.Slf4j;
-
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.*;
- import javax.mail.internet.*;
- import java.util.Properties;
-
- /**
- * 阿里服务方法封装
- * @author lenny
- */
- @Slf4j
- public class AlibabaUtil {
- /**
- * 邮件发送
- * @param toAddress 接收邮箱地址
- * @param fromAlias 发送邮箱名称
- * @param htmlBody 邮箱内容消息体
- * @param subject 邮箱主题
- * @return
- * @throws ClientException
- */
- public static SingleSendMailResponse sendEmail(String toAddress, String fromAlias, String htmlBody, String subject) {
- try {
- if (fromAlias == null || fromAlias.length() <= 0) {
- fromAlias = AlibabaConstants.ALI_EMAIL_CN_FROM_ALIAS;
- }
- IClientProfile profile = DefaultProfile.getProfile(AlibabaConstants.ALI_EMAIL_REGION_ID, AlibabaConstants.ALI_EMAIL_ACCESS_KEY_ID, AlibabaConstants.ALI_EMAIL_SECRET);
- IAcsClient client = new DefaultAcsClient(profile);
- SingleSendMailRequest request = new SingleSendMailRequest();
- request.setAccountName(AlibabaConstants.ALI_EMAIL_ACCOUNT_NAME);
- request.setFromAlias(fromAlias);
- request.setAddressType(1);
- request.setToAddress(toAddress);
- request.setReplyToAddress(true);
- //可以给多个收件人发送邮件,收件人之间用逗号分开
- request.setSubject(subject);
- //如果采用byte[].toString的方式的话请确保最终转换成utf-8的格式再放入htmlbody和textbody,若编码不一致则会被当成垃圾邮件。
- //注意:文本邮件的大小限制为3M,过大的文本会导致连接超时或413错误
- request.setHtmlBody(htmlBody);
- //若textBody、htmlBody或content的大小不确定,建议采用POST方式提交,避免出现uri is not valid异常
- request.setMethod(MethodType.POST);
- SingleSendMailResponse sendMailResponse = client.getAcsResponse(request);
- log.info("邮件发送失败:给邮箱为{},发送的内容为{},主题为{}", toAddress, htmlBody, subject);
- return sendMailResponse;
- }catch (Exception e) {
- log.error(e.getMessage());
- return null;
- }
- }
-
- /**
- * 给指定的手机号发送短信
- * @param phone 电话号码
- * @param signName 签名
- * @param templateCode 模板编码
- * @param content 发送内容
- * @return
- * @throws ClientException
- */
- public static SendSmsResponse sendSms(String phone, String signName, String templateCode, String content) {
- try {
- /* 超时时间,可自主调整 */
- System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
- System.setProperty("sun.net.client.defaultReadTimeout", "10000");
- /* 初始化acsClient,暂不支持region化 */
- IClientProfile profile = DefaultProfile.getProfile(AlibabaConstants.ALI_SMS_REGION_ID, AlibabaConstants.ALI_SMS_ACCESS_KEY_ID, AlibabaConstants.ALI_SMS_SECRET);
- DefaultProfile.addEndpoint(AlibabaConstants.ALI_SMS_REGION_ID, AlibabaConstants.ALI_SMS_PRODUCT, AlibabaConstants.ALI_SMS_DOMAIN);
- IAcsClient acsClient = new DefaultAcsClient(profile);
- /* 组装请求对象-具体描述见控制台-文档部分内容 */
- SendSmsRequest request = new SendSmsRequest();
- /* 必填:待发送手机号 */
- request.setPhoneNumbers(phone);
- /* 必填:短信签名-可在短信控制台中找到 */
- request.setSignName(signName);
- /* 必填:短信模板code-可在短信控制台中找到 */
- request.setTemplateCode(templateCode);
- /* 可选:模板中的变量替换JSON串,如模板内容为"亲爱的用户,您的验证码为${code}"时,此处的值为 */
- request.setTemplateParam(content);
- // hint 此处可能会抛出异常,注意catch
- SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
- if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
- log.info("短信发送成功:给号码为{},发送的内容为{}", phone, content);
- } else {
- log.info("短信发送失败:给号码为{},发送的内容为{}", phone, content);
- }
- return sendSmsResponse;
- }catch (Exception e) {
- log.error(e.getMessage());
- return null;
- }
- }
-
- /**
- * 发送SMTP邮件,含附件
- * @param toAddress
- * @param fromAlias
- * @param subject
- * @param bodyContent
- * @param attachPath
- * @return
- */
- public static boolean sendSMTPEmail(String toAddress,String fromAlias,String subject,String bodyContent,String attachPath){
- try {
- if (fromAlias == null || fromAlias.length() <= 0) {
- fromAlias = AlibabaConstants.ALI_EMAIL_CN_FROM_ALIAS;
- }
- Properties props = System.getProperties();
- props.setProperty("mail.smtp.host", AlibabaConstants.ALI_SMTP_HOST);
- props.setProperty("mail.smtp.socketFactory.fallback", "false");
- props.setProperty("mail.smtp.port", String.valueOf(AlibabaConstants.ALI_SMTP_PORT));
- props.setProperty("mail.smtp.socketFactory.port", String.valueOf(AlibabaConstants.ALI_SMTP_SSL_PORT));
- props.setProperty("mail.smtp.auth", "true");
- //建立邮件会话
- Session session = Session.getDefaultInstance(props, new Authenticator() {
- //身份认证
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(AlibabaConstants.ALI_EMAIL_ACCOUNT_NAME, AlibabaConstants.ALI_SMTP_PASSWORD);
- }
- });
- //建立邮件对象
- MimeMessage message = new MimeMessage(session);
- //设置邮件的发件人
- message.setFrom(new InternetAddress(AlibabaConstants.ALI_EMAIL_ACCOUNT_NAME,fromAlias));
- //收件人
- message.setRecipients(Message.RecipientType.TO, toAddress);
- //主题
- message.setSubject(subject);
- //内容
- Multipart multipart = new MimeMultipart();
- //body部分
- BodyPart contentPart = new MimeBodyPart();
- contentPart.setContent(bodyContent, "text/html;charset=utf-8");
- multipart.addBodyPart(contentPart);
- //附件部分
- BodyPart attachPart = new MimeBodyPart();
- FileDataSource fileDataSource = new FileDataSource(attachPath);
- attachPart.setDataHandler(new DataHandler(fileDataSource));
- attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
- multipart.addBodyPart(attachPart);
- message.setContent(multipart);
- //发送邮件
- Transport.send(message);
- return true;
- } catch (Exception e) {
- log.error(e.toString());
- return false;
- }
- }
- }
- /**
- * 阿里服务常量
- * @author lenny
- * @date 20210710
- */
- public class AlibabaConstants {
- private AlibabaConstants() {
- }
-
- /**
- * 阿里邮件发送账户
- */
- public static final String ALI_EMAIL_ACCOUNT_NAME = "xxxxx.xxxxxx.com";
- /**
- * 阿里邮件发送人名称
- */
- public static final String ALI_EMAIL_CN_FROM_ALIAS="xxx";
- /**
- * 阿里邮件发送人名称
- */
- public static final String ALI_EMAIL_EN_FROM_ALIAS = "xxx";
- /**
- *阿里邮件地区标识
- */
- public static final String ALI_EMAIL_REGION_ID = "cn-hangzhou";
- /**
- * 阿里邮件验证账户
- */
- public static final String ALI_EMAIL_ACCESS_KEY_ID = "xxx";
- /**
- *阿里邮件密钥
- */
- public static final String ALI_EMAIL_SECRET = "xxx";
- /**
- * 阿里邮件主题(中文)
- */
- public static final String ALI_EMAIL_CN_SUBJECT="【邮件提醒】";
- /**
- * 阿里邮件主题(英文)
- */
- public static final String ALI_EMAIL_EN_SUBJECT="[Email reminder]";
- /**
- * 阿里短信API产品名称
- */
- public static final String ALI_SMS_PRODUCT = "Dysmsapi";
- /**
- * 阿里短信API产品域名
- */
- public static final String ALI_SMS_DOMAIN = "dysmsapi.aliyuncs.com";
- /**
- * 阿里短信地区标识
- */
- public static final String ALI_SMS_REGION_ID = "cn-hangzhou";
- /**
- * 阿里短信验证账户
- */
- public static final String ALI_SMS_ACCESS_KEY_ID = "xxx";
- /**
- * 阿里短信密钥
- */
- public static final String ALI_SMS_SECRET = "xxxxxx";
- /**
- * 阿里短信签名(中文)
- */
- public static final String ALI_SMS_CN_SIGN_NAME="xxx";
- /**
- * 阿里短信签名(英文)
- */
- public static final String ALI_SMS_EN_SIGN_NAME="xxx";
- /**
- * 阿里短信模板(中文)
- */
- public static final String ALI_SMS_CN_MESSAGE_TEMPLATE="SMS_xxxxxx";
- /**
- * 阿里短信模板(英文)
- */
- public static final String ALI_SMS_EN_MESSAGE_TEMPLATE="SMS_xxxxxx";
- /**
- * 阿里短信模板(推送微信服务号订阅信息)
- */
- public static final String ALI_SMS_CN_SUBSCRIPTION_TEMPLATE="SMS_xxxxx";
- /**
- * 阿里SMTP邮件域名
- */
- public static final String ALI_SMTP_HOST = "smtpdm.aliyun.com";
- /**
- * 阿里SMTP邮件端口
- */
- public static final int ALI_SMTP_PORT = 80;
- /**
- * 阿里SMTP邮件SSL端口
- */
- public static final int ALI_SMTP_SSL_PORT = 465;
- /**
- * 阿里SMTP邮件用户名
- */
- public static final String ALI_SMTP_USER = "xxxx";
- /**
- * 阿里SMTP邮件密码
- */
- public static final String ALI_SMTP_PASSWORD = "xxxxxxx";
- /**
- * SMTP邮件内容(中文)
- */
- public static final String ALI_SMTP_CN_CONTENT="报告已送达,请查阅附件!";
- /**
- * SMTP邮件内容(英文)
- */
- public static final String ALI_SMTP_EN_CONTENT="The report has been delivered, please refer to the attachment!";
- }
在调用的时候直接使用
- /**
- * 发送邮件
- * @param toAddress 接收人邮件地址
- * @param fromAlias 发送邮箱名称
- * @param htmlBody 消息体内容(用html格式发送效果更好)
- * @param subject 主题名称
- */
- AlibabaUtil.sendEmail(toAddress,fromAlias,htmlBody,subject);
- /**
- * 发送短信
- * @param phone 接收人手机号
- * @param signName 短信签名
- * @param templateCode 短信模板
- * @param content 符合模板的短信内容参数
- */
- AlibabaUtil.sendSms(phone,signName,templateCode,content);
- /**
- * smtp发送带附件的邮件
- * @param toAddress 接收人邮件地址
- * @param fromAlias 发送邮箱名称
- * @param subject 主题名称
- * @param bodyContent 邮件内容
- * @param filePath 邮件附件地址
- */
- AlibabaUtil.sendSMTPEmail(toAddress, fromAlias, subject, bodyContent, filePath);
短信报警:
普通邮件报警:
SMTP附件邮件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。