繁体   English   中英

尝试使用C#发送电子邮件时的常见算法问题

[英]Common algorithm issue while trying to send an email using C#

我正在尝试通过C#应用程序发送电子邮件,但是每次尝试发送电子邮件时,都会发生错误,说“ 对sspi的调用失败 ”,当我查看内部组件时,它显示类似“ 客户端和服务器无法通信,因为它们不具有通用算法

我的代码是这样的:

try
{
    var fromAddress = new MailAddress("sender@domain.com", "Sender");
    var toAddress = new MailAddress("receiver@domain.com", "Receiver");
    const string fromPassword = "Pass123";
    const string subject = "Prueba";
    const string body = "Prueba body";

    var smtp = new SmtpClient
    {
        Host = "smpt.domain.com",
        Port = 25,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new System.Net.NetworkCredential(fromAddress.Address, fromPassword)
    };
    using (var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = subject,
        Body = body,
        DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure,
        BodyEncoding = UTF8Encoding.UTF8
    })
    {
        smtp.Send(message);
    }
}
catch (Exception exc) 
{
    MessageBox.Show(string.Format("Error: {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

在我的App.config上,我有类似以下内容:

<system.net>
    <mailSettings>
      <smtp from="sender@domain.com">
        <network host="smtp.domain.com" port="25" userName="sender@domain.com" password="Pass123" defaultCredentials="true" />        
      </smtp>
    </mailSettings>
</system.net>

检查此https://www3.trustwave.com/support/kb/article.aspx?id=11849

  • 导航到控制面板。
  • 管理工具,然后是本地安全策略。
  • 本地策略和安全选项。
  • 系统加密:使用符合FIPS的算法进行加密,哈希和签名。
  • 禁用设置,然后单击“应用”。
  • 重新启动IIS

最后,我只是放弃使用SmtpClient和MailMessage,而是使用Outlook MailItem

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

oMsg.To = "correo@gmail.com";
oMsg.Subject = "Prueba";
oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;

oMsg.HTMLBody = "<html><body>";
oMsg.HTMLBody += string.Format("<p>El dato {0}", var1);
oMsg.HTMLBody += string.Format("se guardo en la  <a href='{0}'>dirección</a>.</p> </br> <p>Saludos.</p> </body></html>", path);

oMsg.Display(false); 
((Microsoft.Office.Interop.Outlook._MailItem)oMsg).Send();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM