![](/img/trans.png)
[英]I got an InvalidCastException error while trying to send a Email in 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
最后,我只是放弃使用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.