如何通过在java发送邮件来验证电子邮件地址真的存在 [英] How to verify an email address really exists by sending a mail in java

查看:190
本文介绍了如何通过在java发送邮件来验证电子邮件地址真的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Our web application sends email to every user who enters their email id. But how can i make sure that email id entered by user is valid one.Actually what we do when any user enters a email id we send link to his email id to activate the acount. I have a code for sending emails. But it doesnot give me any errors even if mail id does not exists. Will you please tell me how to solve the problem ? If email id does not exists really it should give some error.

我在这里附加我的代码

    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"  )");
}

}


推荐答案

您最高可以使用 regex 来检查地址是否在语法上有效和/或查找如果域名有效, MX记录,但这仍然不能保证电子邮件地址是合法的,属于有问题的注册人,实际上没有比发送验证邮件和等待短时间确认更可靠的方式。

You can at highest use regex to check if the address is syntactically valid and/or looking up the MX records if the domain is valid, but this still does not guarantee that the email address is legitimate and belongs to the registrant in question. There is really no more reliable way than sending a verficiation email and waiting for confirmation in a short timespan.

这篇关于如何通过在java发送邮件来验证电子邮件地址真的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆