admin管理员组

文章数量:1559055

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

  1. 企业邮箱需要使用ssl
    
     private static string account = "企业邮箱账户";// 登录账户
     private static string password = "企业邮箱密码";// 登录密码
     private static string host = "smtp.exmail.qq";// 服务器地址
     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.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();
         }
     }
     

本文标签: 腾讯发送邮件企业邮箱