java-腾讯企业邮箱发送邮件

package com.jfj.platform.utils;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.MimeMessage;


public class MailUtil {

    private static  String  username="账号";   //"此处是邮箱地址"

    private static  String  password="密码";  //""

    private static  String  phone="";

    private static JavaMailSenderImpl mailSender = createMailSender();

    private static JavaMailSenderImpl createMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtp.exmail.qq.com");
        sender.setPort(25);
        sender.setUsername(username);
        sender.setPassword(password);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", "600000");
        p.setProperty("mail.smtp.auth", "false");
        sender.setJavaMailProperties(p);
        return sender;
    }



    /**发送html
     *
     * @param to
     * @param html
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    public static   void sendMail(String to, String html) throws MessagingException,UnsupportedEncodingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码,否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(username, "****平台");
        messageHelper.setTo(to);
        messageHelper.setSubject("注册回执");
        messageHelper.setText(html, true);
//      messageHelper.addAttachment("", new File(""));//附件
        mailSender.send(mimeMessage);
    }



    public static boolean emailSend(String name,String companyName,String companyPhone,String password,String companyKey,String email){
        String content="<!DOCTYPE html>\n" +
                "<html lang='en'>\n" +
                "<head>\n" +
                "    <meta charset='UTF-8'>\n" +
                "    <meta name='viewport' content='width=device-width, initial-scale=1.0'>\n" +
                "    <meta http-equiv='X-UA-Compatible' content='ie=edge'>\n" +
                "    <title>Document</title>\n" +
                "    <style> \n" +
                "        \n" +
                "    </style>\n" +
                "</head>\n" +
                "<body >\n" +
                "    <div >\n" +
                "        <h1 style='text-align: center;height: 100px;line-height: 100px;'>注册回执<img src="+"图片地址"+" alt='' style='width: 100px;float: right;'></h1>\n" +
                "        <div style='width: 100%;border-bottom:1px solid gray '></div>     \n" +
                "        <p>尊敬的:"+name+"</p>\n" +
                "        <p style='text-indent: 2em;'>很荣幸通知您,您所在的企业:“"+companyName+"装饰公司”,已经通过****平台审核,希望在接下来的服务过程中,能让您满意,如果在使用过程中有任何问题,请拨打:"+phone+",将有专属的客服与您联系,并耐心解答您的疑问。</p>\n" +
                "        <p>请妥善保管好您企业的登录地址和账号,不要外传或泄露。</p>\n" +
                "        <p>账号:"+companyPhone+"<br> \n" +
                "            密码:"+password+"<br>\n" +
                "            网址:http://"+companyKey+".company.deliverhome.cn</p>\n" +
                "        <div style='width: 100%;border-bottom:1px solid gray '></div>     \n" +
                "        <div>\n" +
                "            <p style=\"height: 100px\">\n" +
                "                <img src="+""+" alt=''style='width: 100px;'>\n" +
                "                <span style='font-size: 12px;display: inline-block;position: relative;bottom: 10px;'>关注*****公众号,了解更多产品咨询及优惠活动</span>\n" +
                "            </p>    \n" +
                "        </div>\n" +
                "    </div>\n" +
                "\n" +
                "</body>\n" +
                "</html>";
        try {
            sendMail(email,content);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return true;
    }

    public static void main(String[] args) {
        emailSend("小孟","花儿","18866668888","123456789","wahaha","12344440@qq.com");
    }
}

 

因为是带图片的,所有被屏蔽到垃圾邮箱了。

 

部署到阿里服务器才发现有问题(服务调用超时)阿里端口25是关闭的。只有大公司才能申请开启。换另外一种方法

package com.jfj.platform.utils;


import com.sun.mail.util.MailSSLSocketFactory;
import com.yxpf.common.core.utils.YxpfUtils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;


public class EmailSendUtil {

    private static  String  phone="010-0000-0000";
    private static String account = "";// 登录账户
    private static String password = "";// 登录密码
    private static String host = "smtp.exmail.qq.com";// 服务器地址
    private static String port = "465";// 端口
    private static String protocol = "smtp";// 协议
    //初始化参数
    public static Session initProperties() {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", protocol);
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.port", port);
        // 使用smtp身份验证
        properties.put("mail.smtp.auth", "true");
        // 使用SSL,企业邮箱必需 start
        // 开启安全协议
        MailSSLSocketFactory mailSSLSocketFactory = null;
        try {
            mailSSLSocketFactory = new MailSSLSocketFactory();
            mailSSLSocketFactory.setTrustAllHosts(true);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        properties.put("mail.smtp.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.socketFactory.fallback", "false");
        properties.put("mail.smtp.socketFactory.port", port);
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(account, password);
            }
        });
        // 使用SSL,企业邮箱必需 end
        // TODO 显示debug信息 正式环境注释掉
        session.setDebug(true);
        return session;
    }
    // @param sender 发件人别名
    // @param subject 邮件主题
    //@param content 邮件内容
    //@param receiverList 接收者列表,多个接收者之间用","隔开
    //@param fileSrc 附件地址
    public static void send(String sender, String subject, String content, String receiverList, String fileSrc) {
        try {
            Session session = initProperties();
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress(account, sender));// 发件人,可以设置发件人的别名
            // 收件人,多人接收
            InternetAddress[] internetAddressTo = new InternetAddress().parse(receiverList);
            mimeMessage.setRecipients(Message.RecipientType.TO, internetAddressTo);
            // 主题
            mimeMessage.setSubject(subject);
            // 时间
            mimeMessage.setSentDate(new Date());
            // 容器类 附件
            MimeMultipart mimeMultipart = new MimeMultipart();
            // 可以包装文本,图片,附件
            MimeBodyPart bodyPart = new MimeBodyPart();
            // 设置内容
            bodyPart.setContent(content, "text/html; charset=UTF-8");
            mimeMultipart.addBodyPart(bodyPart);
            if(YxpfUtils.isNotEmpty(fileSrc)){
               // 添加图片&附件
                bodyPart = new MimeBodyPart();
                bodyPart.attachFile(fileSrc);
                mimeMultipart.addBodyPart(bodyPart);
            }
            mimeMessage.setContent(mimeMultipart);
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static boolean emailSend(String name,String companyName,String companyPhone,String password,String companyKey,String email){
        String content="<!DOCTYPE html>\n" +
                "<html lang='en'>\n" +
                "<head>\n" +
                "    <meta charset='UTF-8'>\n" +
                "    <meta name='viewport' content='width=device-width, initial-scale=1.0'>\n" +
                "    <meta http-equiv='X-UA-Compatible' content='ie=edge'>\n" +
                "    <title>Document</title>\n" +
                "    <style> \n" +
                "        \n" +
                "    </style>\n" +
                "</head>\n" +
                "<body >\n" +
                "    <div >\n" +
                "        <h1 style='text-align: center;height: 100px;line-height: 100px;'>注册回执<img src="+"https://*-beijing.aliyuncs.com/logo.jpeg"+" alt='' style='width: 100px;float: right;'></h1>\n" +
                "        <div style='width: 100%;border-bottom:1px solid gray '></div>     \n" +
                "        <p>尊敬的:"+name+"</p>\n" +
                "        <p style='text-indent: 2em;'>很荣幸通知您,您所在的企业:“"+companyName+"装饰公司”,已经通过****平台审核,希望在接下来的服务过程中,能让您满意,如果在使用过程中有任何问题,请拨打:"+phone+",将有专属的客服与您联系,并耐心解答您的疑问。</p>\n" +
                "        <p>请妥善保管好您企业的登录地址和账号,不要外传或泄露。</p>\n" +
                "        <p>账号:"+companyPhone+"<br> \n" +
                "            密码:"+password+"<br>\n" +
                "            网址:http://"+companyKey+".*********.cn</p>\n" +
                "        <div style='width: 100%;border-bottom:1px solid gray '></div>     \n" +
                "        <div>\n" +
                "            <p style=\"height: 100px\">\n" +
                "                <img src="+"https://*-cn-beijing.aliyuncs.com/******.jpg"+" alt=''style='width: 100px;'>\n" +
                "                <span style='font-size: 12px;display: inline-block;position: relative;bottom: 10px;'>关注****公众号,了解更多产品咨询及优惠活动</span>\n" +
                "            </p>    \n" +
                "        </div>\n" +
                "    </div>\n" +
                "\n" +
                "</body>\n" +
                "</html>";
            send("***平台","注册回执",content,email,"");
        return true;
    }

}

发一版阿里就OK了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值