1
public bool SendEmailToActivate(string email)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("mymailid@gmail.com");
                mail.To.Add("sendingmailid@gmail.com");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("mymailid@gmail.com", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

how to know the mail is send success to the user or not in c#. ie. if the mail is not send to user then i want the count of the failure mails.. please help me.

DEVASISH BEHARA
  • 115
  • 3
  • 11
  • 2
    Possible duplicate of [SmtpClient get result from server on send](http://stackoverflow.com/questions/7273949/smtpclient-get-result-from-server-on-send) –  May 10 '17 at 11:57

1 Answers1

1

A good start to your solution would be to actually utilize what catch can do, and what the various Exception classes provide in feedback.

Your current implementation does nothing for handling. IntelliSense probably has flagged ex as not being used

catch (Exception ex) {
    return false;
}

In the case of an SMTP Exception, you can retrieve the actual status code and look up what the status means. This would go in above your existing generic catch and handler.

catch (SmtpException sx) { // general SMTP errors
    var smtpStatus = sx.StatusCode;
    // See for values: https://msdn.microsoft.com/en-us/library/system.net.mail.smtpstatuscode(v=vs.110).aspx

    LogSmtpException(sx); // Log your exception review (you need to create)

    return false;
}
catch (Exception ex) { // general error NOT previously caught
    LogException(ex); // Log your exception review (you need to create)
    return false;
}

But there is an even more specific SMTP Exception you could get, usually for a bad address, so we could throw that on top of the 2 other catches

catch (SmtpFailedRecipientException rx) { // Bad recipient
    var smtpStatus = rx.StatusCode; // inherited from SmtpException

    // SEE: https://msdn.microsoft.com/en-us/library/system.net.mail.smtpfailedrecipientexception(v=vs.110).aspx

    LogSmtpException(sx); // Log your exception review (same as SmtpException)

    return false;
}


catch (SmtpException sx) { // general SMTP errors
    var smtpStatus = sx.StatusCode;
    LogSmtpException(sx);
    return false;
}

catch (Exception ex) { // general error NOT previously caught
    LogException(ex);
    return false;
}

What I have implemented

My "Send Mail" routine is not a bool method; it actually returns a KeyValuePair<int, string>, which is interpreted by the caller.

KeyValuePair<int, string> MessageStatus = new SendMail(my values);
if ((int)MessageStatus.Key == 250) {
    // all ok, proceed as normal 
} 
else {
    ViewData["StatusCode"] = MessageStatus.Key;
    ViewData["StatusMessage"] = MessageStatus.Value;
}

And my SendMail is structured like this. I do not lookup the exact reason at the time of the event, but I log the exceptions for later review.

public KeyValuePair<int, string> SendMail (my values) {

    int StatusCode;
    string StatusMessage;

    // ===== standard message building code =====
    try {
        // ===== smtp client setup code =====

        smtp.Send(mail);
        StatusCode = 250;                      // Standard SMTP OK
        StatusMessage = "Message Sent";
    }
    catch (SmtpFailedRecipientException rx) {
        LogExceptionSMTP(rx, message.To);
        StatusCode = (int)rx.StatusCode;
        StatusMessage = rx.Message;
    }
    catch (SmtpException sx) {
        LogExceptionSMTP(sx, message.To);
        StatusCode = (int)sx.StatusCode;
        StatusMessage = sx.Message;
    }
    catch (Exception ex) {
        ex.Data.Add( "SendMail_Recipient", message.To );
        LogException(ex);
        StatusCode = 500;                      // Standard App Error Code
        StatusMessage = ex.Message;
    }
Mad Myche
  • 1,075
  • 1
  • 7
  • 15
  • if some one give the wrong mailid then how can know the mail is send or not..? – DEVASISH BEHARA May 12 '17 at 09:45
  • @DEVASISHBEHARA You can only tell if the email was sent or not; and if not you know which address is incorrect because of the exception handler logging the address. If someone gives you an incorrect address that does exist, the mail you send will go the wrong person. That's why many sites will have the users enter the email twice or send a confirmation mail. – Mad Myche May 12 '17 at 10:52
  • #Mad Myche :- if the mail address is not available then the mail wasn't went to any other person, then it sent to my mail id with the message Address not found at that time how can i know..? the mail is deliverer or not..? – DEVASISH BEHARA Jun 01 '17 at 05:09
  • @DEVASISHBEHARA That should raise an `SmtpFailedRecipientException` error, which can be caught then you could send a new message to yourself saying that. – Mad Myche Jun 01 '17 at 11:02