2016-04-17 3 views
0

Ich sende E-Mails mit Spring-Java-Mail und Microsoft Exchange.Konnte keine Verbindung zu Microsoft Exchange herstellen nach ein paar sendet

Wenn ich eine einzelne E-Mail sende funktioniert alles richtig, aber wenn ich ein paar Mails eng (nur 4 oder 5) senden, gibt der Server eine "Connection Timeout". Bei einem erneuten Versuch und erneutem Versuch werden schließlich alle Mails gesendet. Ich habe mit Microsoft-Support gesprochen und sie haben gesagt, dass 30 Mails/Minute erlaubt sind, aber ich kann nicht mehr als 3 oder 4 senden. Irgendeine Idee?

Das ist meine config:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
    <property name="host" value="${mailHost}" /> 
    <property name="port" value="${mailPort}" /> 
    <property name="username" value="${mailUser}" /> 
    <property name="password" value="${mailPassword}" /> 

    <property name="javaMailProperties"> 
     <props> 
      <prop key="mail.smtp.auth">true</prop> 
      <prop key="mail.smtp.starttls.enable">true</prop> 
     </props> 
    </property> 
</bean> 

mailHost=smtp.office365.com 
mailPort=25 
mailUser=xxx 
mailPassword=xxx 

Der Code, der die E-Mail sendet:

private void sendMail(String subject, String body, boolean isHtml,int atttemp, Attachment attachment, String... to) { 
    log.info("Sending mail("+subject+" to:"+Arrays.toString(to)); 
    try{ 
     MimeMessage message = mailSender.createMimeMessage(); 
     MimeMessageHelper helper = new MimeMessageHelper(message, true); 
     helper.setTo(to); 
     helper.setFrom(from); 
     helper.setText(body, isHtml); 
     helper.setSubject(subject); 
     if (attachment != null){ 
      log.debug("Adding attachment:"+attachment.getName()); 
      helper.addAttachment(attachment.getName(), new ByteArrayResource(IOUtils.toByteArray(attachment.getAttachment()))); 
     } 
     mailSender.send(message); 
     log.info("Mail sent"); 
    }catch(MessagingException|MailSendException|MailAuthenticationException e){ 
     log.error("Message:"+e.getMessage()); 
     if (atttemp < 5){ 
      log.error("Timeout Exception?. Retrying mail to...: "+Arrays.toString(to)+"; attempt:"+atttemp); 
      sendMail(subject, body,isHtml,atttemp++, attachment, to); 
     }else{ 
      log.error("Mail not sent to:"+Arrays.toString(to)+" after "+atttemp+" attemps"); 
     } 

    }catch(Throwable t){ 
     log.error("Error sending message to:"+Arrays.toString(to)+". ",t); 
    } 
} 

Und das ist die Ausnahme, die ich bin immer:

2016-04-17 16:39:04,380 ERROR c.h.n.m.MailSender [Thread-2] Message:Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 25; 
nested exception is: 
java.net.ConnectException: Connection timed out. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 25; 
nested exception is: 
java.net.ConnectException: Connection timed out 
+0

Könnten Sie versuchen, den Port auf 587 zu ändern? – fateddy

Antwort

0

Okay. Also, ich habe deinen Code ausprobiert. Ich habe Nachrichten von einem Exchange-Server (1 zunächst, dann 10 gleichzeitig) sowohl an ein Google Mail- als auch an ein Yahoo-Konto gesendet. Alles läuft reibungslos. Es scheint eher etwas in Ihrer Umgebung - und nicht der Code - zu sein, das Probleme verursacht.

  1. Ich schlage vor, Sie verschiedene Ports versuchen: 25, 465, 587
  2. versuchen, von einem Google Mail-Konto zu senden mit smtp.gmail.com als Server (Siehe: https://support.google.com/a/answer/176600?hl=en)
  3. Verbindung zu einem anderen ISP, wenn Sie
  4. Reduzieren Sie die Größe Ihres Anhangs
  5. Ich habe einige Code unten können Sie anstelle Ihrer vorhandenen Bean verwenden.

Beispieleigenschaftsdatei heißt messagesender.properties:

[email protected] 
[email protected],[email protected] 
[email protected],[email protected] 
[email protected],[email protected] 
[email protected] 
report.email.server=smtp.gmail.com 
report.email.port= 
report.email.username= 
report.email.password= 
#SSL,TLS 
report.email.encryption.strategy=SSL 
report.email.enable.authentication=false 
report.email.enable.encryption=false 
report.email.subject=Test 
report.email.text=This better work! 
report.email.retry.interval=15 
report.email.fail.attempts=40 

Der Code Bean Implementierung unten zu ersetzen. Dies wird direkt lesen Eigenschaften von Ihren Eigenschaften in Ihrem Classpath platzierten Datei:

import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.Serializable; 
import java.util.Properties; 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.mail.BodyPart; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Part; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 
import javax.mail.util.ByteArrayDataSource; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Service; 

@Service("MessageSender") 
public class MessageSender2 implements Serializable { 

    private static final Logger logger = LoggerFactory.getLogger(MessageSender2.class); 
    Properties props; 
    boolean wait = false; 
    Integer retryInterval = 0; 
    Integer failAttempts = 0; 
    Integer failCount = 0; 

    public void composeAndSendMessage(EmailObject reportObjects) throws IOException, MessagingException { 
     props = new Properties(); 
     props.load(this.getClass().getResourceAsStream("/messagesender.properties")); 

     retryInterval = Integer.valueOf(props.getProperty("report.email.retry.interval")); 
     failAttempts = Integer.valueOf(props.getProperty("report.email.fail.attempts")); 

     MimeMultipart mimeMultipart = prepareAttachmentContent(reportObjects); 
     MimeMessage mimeMessage = prepareMessageContent(mimeMultipart); 
     sendMail(mimeMessage); 
    } 

    private MimeMultipart prepareAttachmentContent(EmailObject reportObject) throws IOException, MessagingException { 

     MimeMultipart mimeMultipart = new MimeMultipart("mixed"); 
     String fileName = reportObject.getFile(); 
     ByteArrayInputStream bais = new ByteArrayInputStream(reportObject.getContent()); 

     DataSource bads = null; 
     bads = new ByteArrayDataSource(bais, "application/pdf"); 

     MimeBodyPart bodyPart = new MimeBodyPart(); 
     bodyPart.setDataHandler(new DataHandler(bads)); 
     bodyPart.setFileName(fileName); 
     bodyPart.setDisposition(Part.ATTACHMENT); 

     mimeMultipart.addBodyPart(bodyPart); 
     return mimeMultipart; 
    } 

    private MimeMessage prepareMessageContent(MimeMultipart mimeMultipart) throws MessagingException { 

     String fromAdd = props.getProperty("report.email.from"); 
     String[] recipientCc = null; 
     String[] recipientBcc = null; 
     String[] recipient = props.getProperty("report.email.to").split(","); 
     try { 
      recipientCc = props.getProperty("report.email.cc").split(","); 
     } catch (Exception ex) { 
     } 
     try { 
      recipientBcc = props.getProperty("report.email.bcc").split(","); 
     } catch (Exception ex) { 
     } 
     String replyTo = props.getProperty("report.email.reply.to"); 
     String host = props.getProperty("report.email.server"); 
     String port = props.getProperty("report.email.port"); 
     String username = props.getProperty("report.email.username"); 
     String password = props.getProperty("report.email.password"); 
     String encryptionStrategy = props.getProperty("report.email.encryption.strategy"); 
     Boolean authenticate = Boolean.valueOf(props.getProperty("report.email.enable.authentication")); 
     Boolean encrypt = Boolean.valueOf(props.getProperty("report.email.enable.encryption")); 
     String subject = props.getProperty("report.email.subject"); 
     String text = props.getProperty("report.email.text"); 

     Properties properties = System.getProperties(); 
     properties.setProperty("mail.smtp.host", host); 
     properties.setProperty("mail.smtp.from", fromAdd); 
     if (port != null && !port.isEmpty()) { 
      properties.put("mail.smtp.port", port); 
     } 

     if (encrypt != null && encrypt) { 
      if (encryptionStrategy != null && encryptionStrategy.equalsIgnoreCase("TLS")) { 
       properties.put("mail.smtp.starttls.enable", "true"); 
      } 
      if (encryptionStrategy != null && encryptionStrategy.equalsIgnoreCase("SSL")) { 
       properties.put("mail.smtp.socketFactory.port", port); 
       properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
      } 
     } 

     Session session = null; 

     if (authenticate != null && authenticate) { 
      properties.put("mail.smtp.auth", "true"); 
      if (username != null && !username.isEmpty()) { 
       session = Session.getInstance(properties, 
         new javax.mail.Authenticator() { 
          @Override 
          protected PasswordAuthentication getPasswordAuthentication() { 
           return new PasswordAuthentication(username, password); 
          } 
         }); 
      } 
     } else { 
      session = Session.getDefaultInstance(properties); 
     } 

     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(fromAdd)); 

     InternetAddress sendTo[] = new InternetAddress[recipient.length]; 
     for (int h = 0; h < recipient.length; h++) { 
      sendTo[h] = new InternetAddress(recipient[h]); 
     } 
     message.setRecipients(Message.RecipientType.TO, sendTo); 

     InternetAddress sendToCc[] = null; 
     try { 
      sendToCc = new InternetAddress[recipientCc.length]; 
      for (int h = 0; h < recipientCc.length; h++) { 
       sendToCc[h] = new InternetAddress(recipientCc[h]); 
      } 
      message.setRecipients(Message.RecipientType.CC, sendToCc); 
     } catch (Exception ex) { 
     } 

     InternetAddress sendToBcc[] = null; 
     try { 
      sendToBcc = new InternetAddress[recipientBcc.length]; 
      for (int h = 0; h < recipientBcc.length; h++) { 
       sendToBcc[h] = new InternetAddress(recipientBcc[h]); 
      } 
      message.setRecipients(Message.RecipientType.BCC, sendToBcc); 
     } catch (Exception ex) { 
     } 

     InternetAddress[] reply = new InternetAddress[1]; 
     reply[0] = new InternetAddress(replyTo); 

     message.setReplyTo(reply); 
     message.setSubject(subject); 

     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText(text); 
     messageBodyPart.setContent(text, "text/html"); 

     mimeMultipart.addBodyPart(messageBodyPart); 
     message.setContent(mimeMultipart); 
     return message; 
    } 

    public void sendMail(MimeMessage message) throws MessagingException { 

     failCount++; 

     if (failCount > 1) { 
      logger.info("Retry email sending . Attempt number : " + (failCount - 1)); 
     } 

     if (failCount < (failAttempts + 1)) { 

      if (wait) { 
       try { 
        Thread.sleep(retryInterval * 60000); //Wait 1 minute 
        wait = false; 
        sendMail(message); 
       } catch (InterruptedException exception) { 
       } 
      } 

      if (!wait) { 
       try { 
        Transport.send(message); 
        failCount = 0; 
       } catch (Exception exception) { 
        logger.error("An exception was thrown during sending email: \n" + exception); 
        wait = true; 
        sendMail(message); 
       } 
      } 
     } 
    } 

    class EmailObject { 
     private byte[] content; 
     private String file; 

     public EmailObject(byte[] content, String file) { 
      this.content = content; 
      this.file = file; 
     } 
     public byte[] getContent() { 
      return content; 
     } 
     public void setContent(byte[] content) { 
      this.content = content; 
     } 
     public String getFile() { 
      return file; 
     } 
     public void setFile(String file) { 
      this.file = file; 
     } 
    } 
} 

Sie können die oben versuchen und sehen, ob es in irgendeiner Weise hilft. Prost!

0

Wie @ JonathanAmos vorgeschlagen, das Problem lag nicht im Code, war in der Umgebung. Amazon ec2 beschränkt die Verbindungen zu Mailports, um Spam zu vermeiden. Ich fordere diese Einschränkung Entfernung here und jetzt funktioniert gut.

Verwandte Themen