How to check mail address is exists or not?

徘徊边缘 提交于 2019-11-29 02:35:25

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/13514005/how-to-check-mail-address-is-exists-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!