如何检查邮件地址是否存在?

Dee*_*epu 21 java email smtp mime-message

我使用com.sun.mail.smtp.SMTPTransport通过Java发送电子邮件.

我成功发送了电子邮件,但如果我将邮件发送到无效的电子邮件地址,则SMTPTransport不会出现任何错误.

有没有办法检查给定的邮件地址是否存在?

我不是要将邮件地址检查为客户端,我需要检查为服务器端.

我在很多论坛上发现了很多这样的问题,但我没有得到任何合适的解决方案.

我的代码是 -

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());
Run Code Online (Sandbox Code Playgroud)

它为有效和无效的email_address提供输出: - 250 OK id = 1TbWgN-0007oY-8r

请帮我解决这个问题.提前致谢.

Dee*_*epu 22

谢谢大家的回复.

我可以通过MX Record检查来解决我的问题.

我使用此链接 来解决问题.愿这对某人也有用.

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 + "'" );
}
Run Code Online (Sandbox Code Playgroud)

谢谢.


Que*_*tin 7

确认电子邮件地址的唯一方法是向其发送电子邮件,并要求用户按照电子邮件中的(唯一)链接返回您的网站.

  • +1,但它无法识别临时垃圾邮件地址. (2认同)