如何通过在java中发送邮件来validation电子邮件地址确实存在

我们的Web应用程序向每个输入其电子邮件ID的用户发送电子邮件 但是,我如何确保用户输入的电子邮件ID是有效的。实际上,当任何用户输入电子邮件ID时,我们会发送链接到他的电子邮件ID以激活帐户。 我有一个发送电子邮件的代码。 但即使邮件ID不存在,它也不会给我任何错误。 你能告诉我如何解决这个问题吗? 如果电子邮件ID不存在,它应该给出一些错误。

我在这里附上我的代码

package csv; import javax.mail.PasswordAuthentication; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class email { public void send(String recipeintEmail, String subject, String messageText,String[] attachments) throws MessagingException, AddressException { /* It is a good practice to put this in a java.util.Properties file and encrypt password. Scroll down to comments below to see how to use java.util.Properties in JSF context. */ String senderEmail = "our email address"; String senderMailPassword = "password"; String gmail = "smtp.gmail.com"; Properties props = System.getProperties(); props.put("mail.smtp.user", senderEmail); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.debug", "true"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); // Required to avoid security exception. email.MyAuthenticator authentication = new email.MyAuthenticator(senderEmail,senderMailPassword); Session session = Session.getDefaultInstance(props,authentication); session.setDebug(true); MimeMessage message = new MimeMessage(session); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(messageText); // Add message text Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Attachments should reside in your server. // Example "c:\file.txt" or "/home/user/photo.jpg" for (int i=0; i < attachments.length; i++) { messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(attachments[i]); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(attachments [i]); multipart.addBodyPart(messageBodyPart) ; } message.setContent(multipart); message.setSubject(subject); message.setFrom(new InternetAddress(senderEmail)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipeintEmail)); Transport transport = session.getTransport("smtps"); transport.connect(gmail,465, senderEmail, senderMailPassword); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } private class MyAuthenticator extends javax.mail.Authenticator { String User; String Password; public MyAuthenticator (String user, String password) { User = user; Password = password; } @Override public PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(User, Password); } } public static void main(String args[]) throws MessagingException { // email e=new email(); // String at[]={"c:/COPYRIGHT.txt"}; // e.send("xyz@gmail.com", "hello","test" )"); } } 

您可以在最高级别使用正则表达式来检查地址是否在语法上有效和/或在域名有效时查找MX记录 ,但这仍然不能保证电子邮件地址是合法的并且属于相关注册人。 实际上没有比发送简历电子邮件并在短时间内等待确认更可靠的方法了。

没有万无一失的方法可以做到这一点。 您可以尝试在此博客文章中解释的步骤,但不保证可以使用各种邮件服务器/中继设置。

向用户发送激活密钥以及URL,如果用户想要登录/使用您提供的内容,则需要提供有效的电子邮件ID。

如果您要坚持发送电子邮件进行validation,我建议您明确说明您将在允许用户访问他们注册的任何内容之前发送电子邮件validation链接,以减少虚假电子邮件地址的数量。 我们已经完成了测试,注册表单标签中的一些小变化显着降低了我们的无效电子邮件速率。

一句警告。 如果您使用发送电子邮件来validation电子邮件是否有效以及您是否向无效地址发送大量电子邮件,特别是在面向消费者的域(例如Yahoo,GMail,AOL等)上,您可能会被标记为垃圾邮件发送者。硬弹跳(发送到无效地址)将影响您的声誉评分。 几个月前,我写了一篇关于validation电子邮件的不同方法的博客文章 ,以免费和商业方式validation您的注册人可能有用。

您可以使用java mail API来validation电子邮件。

 try { // // Create InternetAddress object and validated the supplied // address which is this case is an email address. InternetAddress internetAddress = new InternetAddress(email); internetAddress.validate(); isValid = true; } 

catch(AddressException e){System.out.println(“你在catch块中 – 发生exception:”+ email); }

您可以从http://www.oracle.com/technetwork/java/index-138643.html下载Java Mail API

解决这个问题的一种方法是跟踪退回邮件。 但由于标准不均衡,这种方法存在一些困难。

但是,做一些无所事事可能是值得的。 有时,了解用户是否收到了电子邮件并且没有响应或者电子邮件地址本身是不正确的,这一点很重要。

您可以查看以下链接: http : //www.oracle.com/technetwork/java/faq-135477.html#bounce

从链接中看到这段摘录:

问:当无法传递消息时,将返回失败消息。 如何检测这些“退回”消息?

答:虽然有一个用于报告此类错误的Internet标准(多部分/报告MIME类型,请参阅RFC1892),但尚未广泛实施。 RFC1211深入讨论了这个问题,包括许多例子。

在Internet电子邮件中,特定邮箱或用户名的存在只能由传递邮件的最终服务器确定。 在到达终端服务器之前,该消息可能会通过多个中继服务器(无法检测到错误)。

通常,当终端服务器检测到这样的错误时,它将向原始消息的发送者返回指示失败原因的消息。 有许多Internet标准涵盖此类传递状态通知,但大量服务器不支持这些新标准,而是使用ad hoc技术返回此类故障消息。

这使得很难将“退回”消息与导致问题的原始消息相关联。 (请注意,此问题完全独立于JavaMail。)JavaMail现在包括对解析传递状态通知的支持; 有关详细信息,请参阅JavaMail包中的NOTES.txt文件。

有许多技巧和启发式方法可以解决这个问题 – 它们都不是完美的。 一种技术是变量包络返回路径,在http://cr.yp.to/proto/verp.txt中描述。