尝试使用C#发送电子邮件时的常见算法问题 [英] Common algorithm issue while trying to send an email using C#

查看:287
本文介绍了尝试使用C#发送电子邮件时的常见算法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过C#应用程序发送电子邮件,但是每次查看时,都会出现错误,提示"对sspi的调用失败".内部实例显示为"客户端和服务器无法通信,因为它们不具有通用算法"

I'm trying to send an email via my C# application, but every time i try to send the email, an error occurs, saying "A call to sspi failed", when i look at the inner exeption it says something like "The client and server cannot communicate, because they do not possess a common algorithm"

我的代码是这样的:

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上,我有类似的内容:

And on my App.config i have something like:

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

推荐答案

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

At the end i just give up with using SmtpClient and MailMessage, i used 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();

这篇关于尝试使用C#发送电子邮件时的常见算法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆