2015-01-05 25 views
5

Sto tentando di impostare JavaMail per inviare e ricevere email utilizzando Postfix, che è installato nella casella di sviluppo CentOS7. Ho confermato che postfix è in grado di visualizzare le e-mail ricevute digitando MAIL=/home/root/Maildir nel terminale, seguito da return e quindi mail, che elenca tutte le e-mail ricevute per l'account utente. Ma ancora quando accedo come root e controllo per vedere le e-mail ricevute nel CentOS 7 terminal, non ci sono nuove e-mail dopo aver eseguito il mio codice Javamail come descritto di seguito. Come posso ottenere Javamail per inviare e-mail smtp?JavaMail non invia posta SMTP via postfix

Qui è la mia classe:

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

//Send a simple, single part, text/plain e-mail 
public class TestEmail { 

    public void send(){ 
    // SUBSTITUTE YOUR EMAIL ADDRESSES HERE! 
    String to = "[email protected]"; 
    String from = "[email protected]"; 
    // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE! 
    String host = "localhost"; 

    // Create properties, get Session 
    Properties props = new Properties(); 

    //http://docs.oracle.com/javaee/6/api/javax/mail/Session.html 
    // If using static Transport.send(), 
    // need to specify which host to send it to 
    props.put("mail.smtp.host", host); 
    // To see what is going on behind the scene 
    props.put("mail.debug", "true"); 
    Session session = Session.getInstance(props); 

    try { 
     // Instantiate a message 
     Message msg = new MimeMessage(session); 

     //Set message attributes 
     msg.setFrom(new InternetAddress(from)); 
     InternetAddress[] address = {new InternetAddress(to)}; 
     msg.setRecipients(Message.RecipientType.TO, address); 
     msg.setSubject("A new record was just added."); 
     msg.setSentDate(new Date()); 

     // Set message content 
     msg.setText("This is a test of sending a " + 
        "plain text e-mail through Java.\n" + 
        "Here is line 2."); 

     //Send the message 
     Transport.send(msg); 
    } 
    catch (MessagingException mex) { 
     // Prints all nested (chained) exceptions as well 
     mex.printStackTrace(); 
    } 
} 
}//End of class 

io chiamo la classe come segue:

TestEmail em = new TestEmail(); 
em.send(); 

La console eclisse produce i seguenti registri quando il codice di cui sopra viene eseguito:

DEBUG: JavaMail version 1.5.0-b01 
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers 
DEBUG: Tables of loaded providers 
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]} 
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]} 
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] 
DEBUG SMTP: useEhlo true, useAuth false 
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false 
220 localhost.localdomain ESMTP Postfix 
DEBUG SMTP: connected to host "localhost", port: 25 

EHLO localhost.localdomain 
250-localhost.localdomain 
250-PIPELINING 
250-SIZE 10240000 
250-VRFY 
250-ETRN 
250-ENHANCEDSTATUSCODES 
250-8BITMIME 
250 DSN 
DEBUG SMTP: Found extension "PIPELINING", arg "" 
DEBUG SMTP: Found extension "SIZE", arg "10240000" 
DEBUG SMTP: Found extension "VRFY", arg "" 
DEBUG SMTP: Found extension "ETRN", arg "" 
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "DSN", arg "" 
DEBUG SMTP: use8bit false 
MAIL FROM:<[email protected]> 
250 2.1.0 Ok 
RCPT TO:<[email protected]> 
250 2.1.5 Ok 
DEBUG SMTP: Verified Addresses 
DEBUG SMTP: [email protected] 
DATA 
354 End data with <CR><LF>.<CR><LF> 
Date: Mon, 5 Jan 2015 13:12:02 -0800 (PST) 
From: [email protected] 
To: [email protected] 
Message-ID: <[email protected]in> 
Subject: A new record was just added. 
MIME-Version: 1.0 
Content-Type: text/plain; charset=us-ascii 
Content-Transfer-Encoding: 7bit 

This is a test of sending a plain text e-mail through Java. 
Here is line 2. 
. 
250 2.0.0 Ok: queued as DB1249A618 
QUIT 
221 2.0.0 Bye 
sessionID is: 0816C244BDBAAD890D82138DC3801962 
+1

Si noti che si sta utilizzando una versione piuttosto vecchia di JavaMail e si potrebbe voler aggiornare alla [versione corrente] (https://java.net/projects/javamail/pages/Home), anche se sono sicuro che non ha niente a che fare con il tuo problema –

+1

@BillShannon Grazie! Il mio 'pom.xml' afferma che sto usando la versione 1.5.0, ma a quanto pare dovrei esaminare gli esempi di codice più recenti. – CodeMed

risposta

1

dal log Ok: queued as DB1249A618 la posta viene ricevuta correttamente dal server SMTP. quindi il problema potrebbe essere un server errato (forse un filtro antispam) o un indirizzo di posta errato.

Guarda qui https://serverfault.com/questions/485505/get-postfix-to-forward-roots-mail per verificare che la configurazione del tuo server sia corretta.

+1

questo è un aswer, come probabilmente relay di problemi su Postfix, è necessario chiedere la sezione giusta. Aggiornato con un link alla soluzione probabile (per Ubuntu, ma non dovrebbe differire troppo) – Lesto

+1

Tecnicamente, è una risposta! :) – alexander