smtp email send request fails sometimes

末鹿安然 提交于 2020-12-16 05:53:54

问题


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


回答1:


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;
    }


来源:https://stackoverflow.com/questions/38527662/smtp-email-send-request-fails-sometimes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!