我正在使用此代码通过雅虎 SMTP 服务器发送 SMTP 电子邮件,它用于我正在编写的个人项目。
using System.Net.Mail;
using System.Net;
SmtpClient theClient = new SmtpClient("smtp.mail.yahoo.com", 465);
theClient.UseDefaultCredentials = false;
theClient.Credentials = new NetworkCredential("username", "password");
theClient.EnableSsl = true;
MailMessage theMessage = new MailMessage("username@yahoo.com",
"to.someone@gmail.com");
theMessage.Subject = "Dave test from C# subject";
theMessage.Body = "Dave test from C# body";
theClient.Send(theMessage);
这都是发送 SMTP 电子邮件的非常标准的代码,但是......服务器似乎抛出错误。它强行终止连接。如果我使用其他 SMTP 服务器(如 Gmail、Windows Live 或各种其他 ISP Smtp 服务器),则不会发生这种情况。
这是异常和堆栈跟踪:
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at ConsoleApplication1.Program.Main(String[] args) in E:\dev\ARCSoftware.FTPProcessor\ConsoleApplication1\Program.cs:line 28
我知道问题不是环境问题,因为我可以使用 Outlook Express 将这些确切设置发送到同一台服务器。请问需要发证书什么的吗?
如果您或您认识的任何人对此有任何想法,我将非常感谢您的帮助。
最佳答案
using System.Net.Mail;
using System.Net;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btn_Send_Click(object sender, RoutedEventArgs e)
{
MailMessage oMail = new MailMessage(new MailAddress("username@yahoo.com"), new MailAddress("username@yahoo.com"));
SmtpClient oSmtp = new SmtpClient();
oSmtp.Host = "smtp.mail.yahoo.com";
oSmtp.Credentials = new NetworkCredential("username", "password");
oSmtp.EnableSsl = false;
oSmtp.Port = 587;
oSmtp.Send(oMail);
}
}
关于C# SMTP 电子邮件发送代码对于 Yahoo Mail 失败但对于其他服务器工作正常,任何人都可以帮忙吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5092235/