Search code examples
emailxamarinerror-handlingsmtpsmtpclient

Xamarin, get error sending mail using SmtpClient


I am using System.Net.Mail.SmtpClient to send mail in my Xamarin Form app. It's set to using my gmail address, and working great.

I would like to get the error from the smtp server (if there is one) to inform the user.

So, I am using the event _SendCompleted

Here my code

(sending email)

 MailMessage mail = new MailMessage();
                        SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
                        
                        mail.From = new MailAddress(outils.emailEmetteur);
                        mail.To.Add("[email protected]");//Tiers.contactEmail);
                        mail.Subject = outils.emailObjetDefaut;
                        mail.Body = outils.emailMessageDefaut;

                        bool fileExist = File.Exists(filePath);
                        if (fileExist)
                        {
                            Attachment pJ = new Attachment(filePath);
                            mail.Attachments.Add(pJ);

                            smtpServer.Port = 587;
                            smtpServer.Host = "smtp.gmail.com";
                            smtpServer.EnableSsl = true;
                            smtpServer.UseDefaultCredentials = false;
                            smtpServer.Credentials = new NetworkCredential(outils.emailEmetteur, outils.emailEmetteurMDP);

                            try
                            {
                                smtpServer.SendAsync(mail, null);
                                smtpServer.SendCompleted += smtpServer_SendCompleted;

                                return true;
                            }
                            catch (Exception ex)
                            {
                               
                            }
                        }

(send completed event)

 private static void smtpServer_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            string token = (string)e.UserState;
            string info = "";

            if (e.Cancelled)
            {
                info = "Send canceled.";
            }
            if (e.Error != null)
            {
                info = e.Error.ToString();
            }
            else
            {
                info = "Message sent.";
            }
        }

I am trying to send an email to an incorrect address ([email protected])

My event is correctly triggered, but e.Cancelled and e.Error are NULL, and, in my Gmail inbox, I receive an error from smtp server telling me that the email address was incorrect, and that what I want to get in my Xamarin app.

Do you have any idea ? Thanks :)


Solution

  • Your client relays it to Gmail's outgoing mail server; the transaction to submit the message to SMTP is successful. The error only happens later, when Gmail tries to connect to the recipient's mail server, and can't resolve it; at that point, your client is no longer necessarily connected, and so the mail server generates a bounce message and delivers it to the sender's inbox.

    In the general case, you either have to write your own mail server (but no, don't go there) or examine the inbox for bounce messages.