2016-09-15 53 views
0

我想測試兩種方法通過SMTP發送郵件:SMTP重複發送消息 - 需要驗證

第1種方法:

public void sendEmail(String emailRecip, String subject, String texte, List<String> listAttachedFile){ 


     Session session = Session.getInstance(props, 
       new javax.mail.Authenticator() { 
        @Override 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(username, password); 
        } 
       }); 

     try { 
      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(username)); 
      String address = emailRecip; 
      InternetAddress[] iAdressArray = InternetAddress.parse(address); 
      message.setRecipients(Message.RecipientType.TO, iAdressArray); 
      message.setSubject(subject); 
      MimeBodyPart mbp1 = new MimeBodyPart(); 
      mbp1.setText(texte); 
      Multipart mp = new MimeMultipart(); 
      mp.addBodyPart(mbp1); 
      for (String attachedFile : listAttachedFile) { 
       if (attachedFile != null) { 
        addAttachment(mp, attachedFile); 
       } 
      } 
      message.setContent(mp); 
      Transport.send(message); 

     } catch (MessagingException e) { 
      isMsgSent = "0"; 
     } catch (IOException ex) { 
      Logger.getLogger(SMTPRepositoryImpl.class.getName()).log(Level.SEVERE, null, ex); 
     } 

} 

第二個方法:重複發送消息:

public void sendMailrepeatdly(String emailRecip, String subject, String texte, List<String> listAttachedFile){ 
Session session = Session.getInstance(props,null); 

     /*Session session = Session.getInstance(props, 
     new javax.mail.Authenticator() { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(username, password); 
     } 
     });*/ 

     try { 
      MimeMessage message = new MimeMessage(session); 
      String address = emailRecip; 
      InternetAddress[] iAdressArray = InternetAddress.parse(address); 

      message.setFrom(new InternetAddress(username)); 
      message.setRecipients(Message.RecipientType.TO, iAdressArray); 
      message.setSubject(subject); 
      BodyPart messageBodyPart = new MimeBodyPart(); 
      messageBodyPart.setText(texte); 
      Multipart multipart = new MimeMultipart(); 
      multipart.addBodyPart(messageBodyPart); 
      messageBodyPart = new MimeBodyPart(); 

      for (String attachedFile : listAttachedFile) { 
       if (attachedFile != null) { 
        addAttachment(multipart, attachedFile); 
       } 
      } 

      multipart.addBodyPart(messageBodyPart); 
      message.setContent(multipart); 
      message.saveChanges(); 
      try { 
       long startTime = System.currentTimeMillis(); 
       Transport tr = session.getTransport("smtps"); 
       tr.connect(host, username, password); 
       // tr.sendMessage(message,message.getAllRecipients()); 
       for (Address recipient : message.getAllRecipients()) { 
        tr.sendMessage(message, new Address[]{recipient}); 
       } 
       tr.close(); 

      } catch (SendFailedException sfe) { 
       System.out.println(sfe); 
      } catch (IOException ex) { 
       Logger.getLogger(SMTPRepositoryImpl.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } catch (MessagingException e) { 

     } 

} 

第一種方法效果很好,但用第二種方法我總是得到錯誤:

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required.

調試運行時,調用時 tr.sendMessage(message, new Address[]{recipient});

我失去了我的代碼的東西在第二種方法中,有什麼建議例外SendFailedException被拋出?在SMPT服務器的

的屬性是:

mail.smtp.auth = true

mail.smtp.starttls.enable = true

mail.smtp.host = smtp.gmail.com

mail.smtp.port = 587

+0

爲什麼所有的評論都被刪除!!!!! ,無論如何,解決方案是聲明Transport tr = session.getTransport(「smtp」);而不是像第一種方法那樣改變消息的結構。 – Selma

回答

0

您使用的是「SMTPS」協議,但您爲「SMTP」協議的屬性。更改您的代碼以使用「smtp」協議。