Search code examples
c#emailsmtpsspi

smtp email send request fails sometimes


I am using below code in c#,

string Subject = "test12";     
    MailMessage mail = new MailMessage();
    mail.To.Add(item.ToString());
    mail.From = new MailAddress(EmailUserName);
    mail.Subject = Subject;
    mail.Body = PopulateBody();       
    mail.IsBodyHtml = true;
    SmtpClient client = new SmtpClient(EmailHost, EmailPort);        
    client.EnableSsl = true;
    NetworkCredential credentials = new NetworkCredential(EmailUserName, EmailPassword);

    client.UseDefaultCredentials = false;
    client.Credentials = credentials;
    client.Send(mail);

I am getting error in Client.send(mail) method

What I have tried:

System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The function requested is not supported --- End of inner exception stack trace --- at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception) at


Solution

  • I would try without authentication first to check if that gives a different error and also try without ssl.

        protected bool NotifyByMail(string server, string strFrom, string strTo, string strSubject, string strBodyText, bool isBodyTextHtml = false)
        {
            if (string.IsNullOrEmpty (server)
                || string.IsNullOrEmpty (strFrom)
                || string.IsNullOrEmpty (strTo)
                || string.IsNullOrEmpty (strSubject)
                || string.IsNullOrEmpty (strBodyText))
                return false;
    
            try {
                MailAddress from = new MailAddress (strFrom);
                MailAddress to = new MailAddress (strTo);
                MailMessage message = new MailMessage (from, to);
    
                message.Subject = strSubject;
                message.Body = strBodyText;
                message.IsBodyHtml = isBodyTextHtml;
    
                SmtpClient client = new SmtpClient (server);
    
            // Include credentials if the server requires them.
            //client.Credentials = new System.Net.NetworkCredential ("********", "*******");// System.Net.CredentialCache.DefaultNetworkCredentials;
    
                client.Send (message);
                return true;
            }
            catch (Exception exception) {
                        // TODO ErrorHandling
            }
    
            return false;
        }