Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the email have sent successfully or not?

Tags:

c#

winforms

See title. Since SmtpClient.Send has no return value, I want to know how I can be sure that the E-Mail was successfully sent.

Here is the code I have until now and it works fine (it is from google):

private void sendMail(string strToAddress, string strFromAddress, string strSubject, string strBody)
{
    // new instance of MailMessage
    MailMessage mailMessage = new MailMessage();

    // Sender Address
    mailMessage.From = new MailAddress(strFromAddress);

    // Recepient Address
    mailMessage.To.Add(new MailAddress(strToAddress));

    // Subject 
    mailMessage.Subject = strSubject;

    // Body
    mailMessage.Body = strBody;

    // format of mail message
    mailMessage.IsBodyHtml = true;

    // new instance of Smtpclient
    SmtpClient mailSmtpClient = new SmtpClient("mail.lablabal.com");

    // mail sent
    mailSmtpClient.Send(mailMessage);
}
like image 824
user891757 Avatar asked Dec 01 '25 13:12

user891757


1 Answers

If there's an immediate error, SmtpClient::Send() will throw an exception. There's no way to "track" the email (unless there's some confirmation link to click or whatever). You won't keep a server connection till the mail is received, only till your smtp server passed it successfully (or failed doing so).

like image 178
Mario Avatar answered Dec 04 '25 01:12

Mario