2012-09-06 15 views
6

Sto provando a scrivere una semplice classe di mittente della posta che riceverà un sacco di argomenti e l'utilizzo di quelli invierà un'e-mail utilizzando il nostro server Exchange 2010. Mentre l'autenticazione ecc sembra funzionare bene, sto ricevendo la seguente eccezione quando il codice sta effettivamente cercando di inviare l'email (credo). Mi sono assicurato che l'autenticazione funzioni e ottengo un ritorno dalla sessione, ma ancora non funziona. Qualcuno potrebbe versare qualcosa di simile a quello che sto facendo male o che mi manca? Grazie.JavaMail - javax.mail.MessagingException

Eccezione:

javax.mail.MessagingException: [EOF] 
     at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481) 
     at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1512) 
     at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1054) 
     at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:634) 
     at javax.mail.Transport.send0(Transport.java:189) 
     at javax.mail.Transport.send(Transport.java:140) 
     at com.ri.common.mail.util.MailSender.sendHTMLEmail(MailSender.java:75) 
     at com.ri.common.mail.util.MailSender.main(MailSender.java:106) 

codice rilevante:

import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 


public class MailSender 
{ 
    public static void sendHTMLEmail(String fromEmailId, String toEmailId, String host, String hostUserName, 
      String hostPassword, String mailSubject, String mailBody) 
    { 
     // Get system properties. 
     Properties props = System.getProperties(); 
     // Setup mail server 
     props.put("mail.transport.protocol", "smtp"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.auth", "true"); 

     final String hostUName = hostUserName; 
     final String hPassword = hostPassword; 

     Authenticator authenticator = new Authenticator() 
     { 
      protected PasswordAuthentication getPasswordAuthentication() 
      { 
       return new PasswordAuthentication(hostUName, hPassword); 
      } 
     }; 

     // Get the default Session object. 
     Session session = Session.getDefaultInstance(props, authenticator); 

     try 
     { 
      // Create a default MimeMessage object. 
      MimeMessage message = new MimeMessage(session); 

      // Set From: header field of the header. 
      message.setFrom(new InternetAddress(fromEmailId)); 

      // Set To: header field of the header. 
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmailId)); 

      // Set Subject: header field 
      message.setSubject(mailSubject); 

      // Send the actual HTML message, as big as you like 
      message.setContent(mailBody, "text/html"); 

      // Send message 
      Transport.send(message, message.getAllRecipients()); 
      System.out.println("Sent message successfully...."); 
     } 
     catch(Exception mex) 
     { 
      mex.printStackTrace(); 
     } 

    } 

    public static void main(String[] args) 
    { 

     String to = "[email protected]"; 
     String from = "[email protected]"; 
     String host = "correctHostForExch2010"; 
     String user = "correctUser"; 
     String password = "CorrectPassword"; 
     String subject = "Test Email"; 
     String body = "Hi there. This is a test email!"; 

     MailSender.sendHTMLEmail(from, to, host, user, password, subject, body); 
    } 
} 

EDIT: ho acceso il debug e si dice

MAIL FROM:<[email protected]> 530 5.7.1 Client was not authenticated 
DEBUG SMTP: got response code 530, with response: 530 5.7.1 Client was not authenticated. 

Perchè dovrebbe essere quando la sessione autenticazione succeded?

+2

è possibile attivare la modalità di debug , in modo che tu possa vedere cosa sta dicendo la tua app al server SMPT, alla fine del discorso dovresti vedere qualcosa che indica l'errore. – Alex

+0

Ciao Alex, grazie! Ho attivato il debug e viene visualizzato MAIL FROM: <[email protected]> 530 5.7.1 Il client non è stato autenticato DEBUG SMTP: ha ricevuto il codice di risposta 530, con risposta: 530 5.7.1 Il client non è stato autenticato. Perché dovrebbe essere quando l'autenticazione di sessione è riuscita? – legendofawesomeness

+0

Ho appena visto che per alcune versioni di Exchange l'utente dovrebbe avere uno dei seguenti formati 'utente @ dominio' o' dominio \\ utente', se può essere di aiuto nel frattempo. – Alex

risposta

7

Grazie Alex e Bill. Ho funzionato abilitando la seguente proprietà.

props.put("mail.smtp.starttls.enable", "true"); 
1

due possibilità:

  1. In realtà non è che cercano di autenticazione a causa del suo uso dei Session.getDefaultInstance.
  2. Non si sta autenticando perché non sta trovando alcun meccanismo di autenticazione supportato sia dal client che dal server, probabilmente perché il server vuole che tu usi SSL prima di inviare qualsiasi informazione di autenticazione. Prova a impostare "mail.smtp.starttls.enable" su "true".

Se potessimo vedere più del tuo output di debug, potremmo dirti quale di questi è il caso.

0

ho avuto anche questo errore rintracciare il mio correzione era:

Per OS centos7: andare a vi/etc/hosts e verificare che l'indirizzo IP è corretto

Problemi correlati