天天看點

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();
         }
     }
     
               

繼續閱讀