Java腾讯企业邮箱发送邮件

##Java通过腾讯企业邮箱发送邮件(多人发送)

  1. 企业邮箱需要使用ssl
    
     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 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);
             // 添加图片&附件
             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();
         }
     }
     
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值