SmtpClient SendMail fails -- why?

Jun 8, 2011 at 10:18 PM

Hi All,

The code insert tool isn't working for me for some reason (GG codeplex) hopefully it's readable in pre tags, but any thoughts as to why the below code gives me back a result with SendSuccessful == false, an empty string Data, and empty InvalidMailAddressList? Is there any way to see the actual server response for the attempt?

            SmtpClient client = new SmtpClient();
            client.ServerName = SmtpServerName;
            client.UserName = SmtpUserName;
            client.Password = SmtpPassword;
            client.Ssl = SmtpSslRequired;
            client.Port = 25;
            Logger.LogDebug("Connecting to {0}", SmtpServerName);

            if (!client.Authenticate())
                Logger.LogError("Could not authenticate SMTP user {0} at server {1}. {2}", client.UserName, client.ServerName, client.Ssl ? "Ssl connection attempted." : string.Empty);

               var message = new SmtpMessage();
                message.Subject = SummarySubject;
                message.BodyText = GetBodyText(summary);
                message.From = EmailsFrom;
                message.To.Add(new MailAddress(SummaryTo));

                Logger.LogDebug("Sending summary mail");
                //also tried:
               //var respOnce = client.SendMail(message);
                var respOnce = client.SendMail(EmailsFrom, SummaryTo, string.Empty, string.Empty, message.GetDataText());
                if (respOnce.SendSuccessful == false)
                    Logger.LogError("Failed sending summary mail. Server response: {0}", respOnce.Data);
                    Logger.LogDebug("Summary mail sent.");
Jun 13, 2011 at 4:11 PM

I cannot reproduct your bug.
You can see actual server response by debug source code of C#Mail (It's open source!!)

I recomment below two method that you may find something information.

Check SmtpResponseResult object in GetResponse method of SmtpClient class.
This object represent about the response string from server.
You also check SendMail method of SmtpClient class.
This method communicate with server and execute send mail process by SMTP protocol.