1

I have following code

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

public class SendEmail
{
   public static void main(String [] args)
   {

      // Recipient's email ID needs to be mentioned.
      String to = "*****@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "test1@localhost";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

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

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

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

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

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

Mercury Server Log

T 20140406 114001 534133f8 Connection from 127.0.0.1
T 20140406 114001 534133f8 EHLO RAVI-PC
T 20140406 114001 534133f8 MAIL FROM:<test1@localhost>
T 20140406 114001 534133f8 RCPT TO:<mygmailaccount@gmail.com>
T 20140406 114001 534133f8 DATA
T 20140406 114001 534133f8 DATA - 9 lines, 274 bytes.
T 20140406 114001 534133f8 QUIT
T 20140406 114001 534133f8 Connection closed with 127.0.0.1, 0 sec. elapsed.

Server Debug Output

DEBUG: JavaMail version 1.5.0
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 ESMTP server ready.
DEBUG SMTP: connected to host "localhost", port: 25

EHLO RAVI-PC
250-localhost Hello RAVI-PC; ESMTPs are:
250-TIME
250-SIZE 0
250 HELP
DEBUG SMTP: Found extension "TIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "0"
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<test1@localhost>
250 Sender OK - send RCPTs.
RCPT TO:<mygmailaccount@gmail.com>
250 Recipient OK - send RCPT or DATA.
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   mygmailaccount@gmail.com
DATA
354 OK, send data, end with CRLF.CRLF
From: test1@localhost
To: mygamilaccount@gmail.com
Message-ID: <22999979.0.1396766922465.JavaMail.RAVI@RAVI-PC>
Subject: This is the Subject Line!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is actual message
.
250 Data received OK.
QUIT
221 localhost Service closing channel.
Sent message successfully....

I have setup Mercury Mail Server on my local machine, Now I'm trying to send mail from local machine mail server to gmail user. I got Sent message successfully.... output. when I executed above code. But, I didn't received any mail in gmail account.

Evan
  • 2,400
  • 1
  • 18
  • 34
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Have you looked in your spam box? – Diego Alves Apr 06 '14 at 06:32
  • One minute, I will analyze your code. – Diego Alves Apr 06 '14 at 06:37
  • @Downvoter please explain what's wrong with my question ? – Ravi Apr 06 '14 at 06:37
  • Verify that your mail server is receiving the mail from your application. You might need to enable logging on your server to do this. If the mail is not being received you need to look at the way your application is set up to deliver to it. If it is being received the problem is with your mail server config or routing and is off-topic for [so] –  Apr 06 '14 at 06:38
  • change this code: "addRecipient" to "setRecipients" and test. – Diego Alves Apr 06 '14 at 06:40
  • Change all line code for: message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); – Diego Alves Apr 06 '14 at 06:41
  • @MikeW Thanks for your reply. I have just added `Server Log`. Please verify, everything is fine ? – Ravi Apr 06 '14 at 06:41
  • Looking at your log file it seems that your code is working and delivering mail to your mail server. The problem appears to be related to some routing issue in your mail server or spam filters at GMail, both of which are off-topic for [so]. –  Apr 06 '14 at 07:26

1 Answers1

0

jWeaver, I went through a similar problem and solved in a way that does not know very well explain why, but work.

Change your properties mode to according with this:

    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.debug", "true");
Diego Alves
  • 291
  • 1
  • 2
  • 11
  • I have included your code,It seems fine I didn't get any error. But, still mail not received. :( – Ravi Apr 06 '14 at 06:51
  • Man, I recommend that you install another mail server on your machine, do the test. There may be a problem. Check the doors, if they are open. Configure it in your code: "props.put("mail.smtp.port", "465");" I use the 465 port, but you use the port that you want. Check your firewall. – Diego Alves Apr 06 '14 at 07:32