java信息和邮件发送工具类

maven依赖

        <!-- 阿里信息服务-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.0.3</version>
        </dependency>
        <!-- Java邮件 -->
        <dependency>
		    <groupId>javax.mail</groupId>
		    <artifactId>mail</artifactId>
		    <version>1.4.5</version>
		</dependency>

代码

public class SmsUtil {
    public static void smsServerException(String phoneNumbers, String code) {
        String accessKeyId = "";
        String secret = "";
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", "签名名称");//签名名称
        request.putQueryParameter("TemplateCode", "SMS_123456");//模板名称如: SMS_123456
        request.putQueryParameter("TemplateParam", "{\"name\":\"" + code + "\"}");//code值 验证码模板就把name换成code 通用模板就用name
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        }

    }

    /**
     * mail
     *
     * @throws MessagingException
     * @throws AddressException
     */
    public static void sendrMail(String addresser, String secret, String recipients, String subject, String content)
            throws AddressException, MessagingException {
        Properties props = new Properties();
        //属性mail.smtp.auth设置发送时是否校验用户名和密码
        //属性mail.transport.protocol设置要使用的邮件协议
        //属性mail.host表示发送服务器的邮件服务器地址
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.qq.com");//qq邮箱发送
        props.setProperty("mail.smtp.port", "587");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        //第二个参数为验证信息返回器
        //即Authenticator对象会返回验证信息(用户名,密码等信息)
        //这里的Authenticator是一个匿名内部类
        Session session = Session.getInstance(props,
                new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        //返回发件人账号与密码信息
                        return new PasswordAuthentication(addresser, secret);
                    }
                }
        );

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(addresser));
        msg.setSubject(subject);
        //Message的setRecipients方法可以指定一个或多个收件人
        //这里我们指定多个收件人(即收件人参数数组)
        /*ArrayList<InternetAddress> arrayList = new ArrayList<>();
        for (String strMail : recipients) {
			arrayList.add(new InternetAddress(strMail));
		}
        Address[] address = new Address[recipients.size()];
        address = arrayList.toArray(address);
        System.out.println("收件人们:" + address);*/

        InternetAddress[] address = InternetAddress.parse(recipients);

        msg.setRecipients(MimeMessage.RecipientType.TO,
                address);
                //内容自己编辑
        msg.setContent("<font color=\"red\">" + content + "</font>", "text/html;charset=gbk");
        //上面邮件数组也可以使用下面InternetAddress的parse方法,
        //将带逗号分隔的邮件字符串转化为一个Address数组:
        //InternetAddress[] address = InternetAddress.parse("emailtest3842@126.com,emailtest3087@sina.com");

        /*
          send静态方法。该方法做了以下操作:
          1.连接服务器。
          2.发送邮件。
          3.关闭连接。
         * */
        System.out.println("邮件开始发送!");
        Transport.send(msg);
        System.out.println("邮件发送成功!");
    }

    public static void main(String[] args) {
        smsServerException("13636363366", "你好!世界!");
         try {
            sendrMail("jie@jie.com", "密钥", "jie@163.com,jie@126.com", "主题",   "你好:世界");//jie@jie.com发件人邮箱
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值