22

I am sending email through Java using com.sun.mail.smtp.SMTPTransport.

I am successful to send the email, but SMTPTransport not giving any error if I send the mail to invalid email address.

Is there a way to check that given mail address is exists or not?

I dont mean to check the mail address as client side, I need to check as server side.

I found many questions like this on many forums but I am not getting any proper solutions.

My Code is -

String email = "reciever@theirDomain.com";

Properties props = new Properties();
props.put("mail.smtps.host", "mail.myDomain.com");
props.put("mail.smtps.auth", "true");
Session session = Session.getInstance(props, null);

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("Mail Demo <my_email@myDomain.com>"));
msg.setRecipients(Message.RecipientType.TO, email);
msg.setSubject("Mail Example");
msg.setSentDate(new Date(System.currentTimeMillis()));

String txt = "Message Body";

msg.setText(txt);
SMTPTransport st = (SMTPTransport)session.getTransport("smtps");
st.connect("mail.myDomain.com","my_email@myDomain.com","password");
st.sendMessage(msg, msg.getAllRecipients());

System.out.println("ServerResponse : " + st.getLastServerResponse());

It gives output for both valid and invalid email_address :- 250 OK id=1TbWgN-0007oY-8r

Please help me to resolve the problem. Thanks in advance.

Deepu
  • 2,590
  • 19
  • 52
  • 74
  • What have you tried? I see nothing in your code to check validity/correctness of an email adress. I would do it with a regular expression. – jlordo Nov 22 '12 at 13:47
  • define valid: do you mean check if an email actually corresponds to a real mailbox, or that the email has a valid syntax ? – Peter Nov 22 '12 at 13:49
  • @jlordo thanks for reply. Regular expression only checks that email address is in valid format. I want to check that the email address exists or not – Deepu Nov 22 '12 at 13:50
  • well you can try amazon web services , there if your mail is sent to wrong mail add it throws exception , not precisely best option but you can give it a try – Hussain Akhtar Wahid 'Ghouri' Nov 22 '12 at 13:51
  • @HussainAkhtarWahid thanks, but where can I find Amazon web services? – Deepu Nov 22 '12 at 13:54

2 Answers2

26

Thank you all for your responses.

I am able to solve my problem through MX Record checking .

I used this Link to resolve the problem. May this also be useful for someone.

Hashtable env = new Hashtable();
env.put("java.naming.factory.initial",
             "com.sun.jndi.dns.DnsContextFactory");
DirContext ictx = new InitialDirContext( env );
Attributes attrs = ictx.getAttributes
                       ( hostName, new String[] { "MX" });
Attribute attr = attrs.get( "MX" );
if (( attr == null ) || ( attr.size() == 0 )) {
   attrs = ictx.getAttributes( hostName, new String[] { "A" });
   attr = attrs.get( "A" );
   if( attr == null )
         throw new NamingException
                  ( "No match for name '" + hostName + "'" );
}

Thank you.

Deepu
  • 2,590
  • 19
  • 52
  • 74
8

The only way to confirm an email address is to send an email to it and require the user to follow a (unique) link in the email back to your website.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 2
    +1, but it won't help to identify temporary junkmail addresses. – jlordo Nov 22 '12 at 13:51
  • @Quentin thanks for reply. I want to do it for registration, so I want it in Java. – Deepu Nov 22 '12 at 13:53
  • 1
    Deepu - I think what @Quentin is trying to tell is the approach and it have nothing to do with the Language (Java, C#, etc). For example, when you create a new account in, let's say, twitter, the site sends a email you mentioned on the registration page. Obviously, you open up your mail box and click the link to verify you are a real user. Same approach Quentin mentioned and have nothing to do with the Language. – Steven Nov 22 '12 at 14:37
  • @Steven ya I understand that Quentin is mentioned the good way, it is nothing to do with Language. But it is checking manually means receiver have to click on the Unique link, I want any notification Programmatically that mail delivered successfully or not. – Deepu Nov 23 '12 at 06:31
  • @Deepu — You aren't going to get one. That is why the answer starts with *The only way*. Once the email arrives at the destination server, the sender loses all connection to it. The server could deliver it to the user. It could consider it as spam and throw it away. It could deliver it to the user, but the user turns out to be the *wrong* user because you were given the wrong email address. etc. – Quentin Nov 23 '12 at 07:43