zoukankan      html  css  js  c++  java
  • 通过腾讯邮件服务器发送HTML邮件

    邮件发送工具:

    private static String host = "smtp.exmail.qq.com";// 服务器地址
    private static String port = "465";// 端口
    private static String protocol = "smtp";// 协议

    public static Session initProperties(String account, String password) {
    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");
    // 开启安全协议
    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);
    }
    });
    session.setDebug(true);
    return session;
    }
    /**
    * @Description: 发送邮件
    * @Title: send
    * @date 2019-10-08 17:12
    * @param @param sender发件人别名
    * @param @param subject邮件主题
    * @param @param content接收者列表,多个接收者之间用","隔开
    * @param @param receiverList
    * @param @param fileSrc 附件地址
    * @return void 返回类型
    * @throws @return void
    * @param sender
    * @param subject
    * @param content
    * @param receiverList
    * @param fileSrc
    */
    public void send(String sender, String subject, String content, String receiverList, String fileSrc, String account,
    String password) {
    try {
    Session session = initProperties(account, password);
    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(fileSrc!=null){

    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 void main(String[] args) throws Exception {
    String account = "xxxxxxxx@xxxxxx.cn";// 登录账户
    String password = "xxxxxxxxxxxxxxx";// 登录密码

    SendEmailUtil tenMailUtil = new SendEmailUtil();
    tenMailUtil.send("发件人别名", "邮件主题", "<h1 style='color:red'>HhhhHHHHHHH</h1>",
    "xxxxxx@foxmail.com,xxxxx@xxxxx.cn", "C:/Users/Administrator/Desktop/index.html   as  附件", account,
    password);
    }

    学着把生活的苦酒当成饮料一样慢慢品尝, 不论生命经过多少委屈和艰辛, 我们总是以一个朝气蓬勃的面孔, 醒来在每一个早上。
  • 相关阅读:
    零是奇数还是偶数?
    解决TortoiseSVN中out of date问题的一个方法
    squid透明代理+iptables防火墙,多网卡详细配置
    基于协同过滤的推荐方法
    IP分片和TCP分片 MTU和MSS(转)
    Google Protocol Buffer 的使用和原理(转)
    到底多少线程算是线程数太多?
    开源软件53个相关概念
    GPL,LGPL和BSD等协议注意事项
    IEEE 802
  • 原文地址:https://www.cnblogs.com/yhm9/p/11642775.html
Copyright © 2011-2022 走看看