3

I am developing an android application..in that application there is a registration module.for that i have to implement email verification functionality.

by using the following code I am able to send email for particular email..

  public void onClick(View v) {
            // TODO Auto-generated method stub

            try {   
                GMailSender sender = new GMailSender("username@gmail.com", "*******");
                sender.sendMail("This is Subject",   
                        "This is Body",   
                        "rose.jasmine87@gmail.com",
                        "naresh_bammidi@yahoo.co.in"
                        );   
            } catch (Exception e) {   
                Log.e("SendMail", e.getMessage(), e);   
            } 

        }

but how to know the status, whether it has been sent or not?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vijaya
  • 574
  • 4
  • 10
  • 25

2 Answers2

2

I'm assuming that you're using GMailSender as defined in this post.

Internally GMailSender calls Transport.send(message) which will throw an exception if the send to the GMail server is unsuccessful, but this is being caught and suppressed, so your calling code has no way of knowing whether sending was successful. Firstly you'll need to change the GMailSender code to do something a bit more meaningful in the case of a send error.

What you must remember is that email is not delivered directly to the final recipient by your app or even the GMail server. Just because you managed to send correctly to the GMail server, does not mean that it will actually reach its intended recipient, as it could fail at any mail relay on its route. To properly detect and report on whether mail actually reaches its destination you'll need something a little more sophisticated than this.

RFC 1891 is an extension to the SMTP protocol which supports delivery status notifications, but you may need to re-architect your app to be able to use this. Essentially it works by setting flags in your outgoing message to instruct mail relays to inform you of the message status. In order for you to receive this notification, you must essentially have your own mail server which is capable of receiving emails. You will receive an email containing, for example, a delivery report once a mail relay has successfully delivered it to the recipient's mailbox.

So, to properly implement this, you'll need a mail account for your app which will receive delivery status notifications. You'll need to create an SMTPMessage object, and add a header including a "Return-Receipt-To" header, whose value is set to this mail account. You'll also need to setNotifyOptions() on your message, and then send it to the GMail server. Your app will need to check its account periodically for delivery notifications.

This is a purely email-centric approach. Without knowing your precise requirements, there are alternate mechanisms that you can use. For example, if your requirement is purely to verify that an email address exists, then you can send an email which contains a URI to a server that you control. The URI contains parameters which uniquely identify both the user, and the installation of your app. The user must click on the link, and your server component verifies the mail account. It can then use something like C2DM to inform your app that the mail account is real and valid.

Sorry if this answer is a little long, and does not offer you a simple solution, but if you want to be able to properly determine whether mail is reaching its recipient, then there is no simple answer, I'm afraid.

Community
  • 1
  • 1
Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • thanks for your response..do i need to have own email server?there is no alternative solution ?why because when ever mail has been sent to that email id,,,i want to change status to 1 in my db registration table,,,so then only user can log in to application.. – Vijaya Jun 07 '11 at 06:47
  • You don't actually need your own mail server, just access to an account on a mail server. For performance reasons it would be best to have a separate account for each unique installation of your app. If you only want to check that an email has been sent, then see the first part of my answer. If you want confirmation of when an email has been delivered to the recipient, then there are alternatives which I've added to the original answer. – Mark Allison Jun 07 '11 at 06:56
  • you are saying that i need to change gmaisender code part.what should i change ...why becz i am new to android.please guide me – Vijaya Jun 07 '11 at 07:16
  • Look at the sendMail method in GMailSender. It has a try/catch block which catches everything, and will not alert you to any problems. Also have a look at the javadocs for Transport.send (http://javamail.kenai.com/nonav/javadocs/javax/mail/Transport.html#send(javax.mail.Message)) which shows you what exceptions will be thrown. Catching these will enable you to determine if the message sending failed. This isn't Android specific, it's basic Java. You've copied a piece of code, and you need to study it to understand its limitations, and where you need to extend it to meet your requirements. – Mark Allison Jun 07 '11 at 07:27
0

check below method, which will validate email from client side, simply pass mail string it will return a boolean, whether entered email is correct or not.

public boolean isEmail(String email)
{
    boolean matchFound1;
    boolean returnResult=true;
    email=email.trim();
    if(email.equalsIgnoreCase(""))
        returnResult=false;
    else if(!Character.isLetter(email.charAt(0)))
        returnResult=false;
    else 
    {
        Pattern p1 = Pattern.compile("^\\.|^\\@ |^_");
        Matcher m1 = p1.matcher(email.toString());
        matchFound1=m1.matches();

        Pattern p = Pattern.compile("^[a-zA-z0-9._-]+[@]{1}+[a-zA-Z0-9]+[.]{1}+[a-zA-Z]{2,4}$");
        // Match the given string with the pattern
        Matcher m = p.matcher(email.toString());

        // check whether match is found
        boolean matchFound = m.matches();

        StringTokenizer st = new StringTokenizer(email, ".");
        String lastToken = null;
        while (st.hasMoreTokens()) 
        {
            lastToken = st.nextToken();
        }
        if (matchFound && lastToken.length() >= 2
        && email.length() - 1 != lastToken.length() && matchFound1==false) 
        {

           returnResult= true;
        }
        else returnResult= false;
    }
    return returnResult;
 }
RobinHood
  • 10,897
  • 4
  • 48
  • 97