Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

How to check whether any "Email" Exists or not.
Using java

Thanks in Advance............,


Regards
Sindhu
Posted
Comments
Richard MacCutchan 24-Sep-14 3:07am    
Try sending a message to the address and see what response you get.
Member 11105351 24-Sep-14 3:20am    
When Am sending a message using Java Mail API its not throwing any exception for an invalid email.

Thanks&Regards,
Sindhu
Richard MacCutchan 24-Sep-14 3:26am    
Why would it? If it looks like a valid email then it will be sent. It is only checked for validity when it reaches its destination server. For example if you send a message to invalidname@gmail.com it is gmail that will validate it, and send a reply to you if there is no such address.
Member 11105351 24-Sep-14 3:33am    
The below mentioned is my code which doesn't have a functionality of checking recipient email is valid or not.Can u please suggest me how to add that functionality.

String to = "abc@gmail.com";

// Sender's email ID needs to be mentioned
String from = "validemail@gmail.com ";
final String username = "validemail@gmail.com";//change accordingly
final String password = "********";//change accordingly

// Assuming you are sending email through relay.jangosmtp.net
String host = "smtp.gmail.com";

Properties props = new Properties();

props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.setProperty("mail.smtp.protocol", "smtps");
props.setProperty("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
// props.setProperty("mail.smtp.user", username);

Session session = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

// Create a default MimeMessage object.
Message message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));

// Set Subject: header field
message.setSubject("Testing Subject");

// This mail has 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart("related");

// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();

String htmlText = null;
if("userRegistered".equals(request))
{
System.out.println("if loop");
htmlText = getHtmlDataforReg_User(emailId,userName,userPassword);
}
else
{
System.out.println("else loop");
htmlText = getHtmlData(userName,userPassword);
}
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);

// put everything together
message.setContent(multipart);
// Send message
Transport.send(message);

System.out.println("Sent message successfully....");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
Richard MacCutchan 24-Sep-14 3:46am    
Firstly if you have additional information then please add it to your question, properly formatted, so everyone can see it.

Secondly, there is no functionality to check whether it is valid. The only way you can find out is when you receive a message back from the server to tell you that there is no such address. And that message may come within a few seconds, or within a few days, or never.

1 solution

It is not possible. It's easy to prove by sending some message using correctly-looking but not existing address in the "To:" header. The only thing you can validate is the validity of the domain name, by using DNS and/or even pinging it. But it wold not solve this problem.

This is not how this mail system works.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900