C# SMTP email sending code fails for Yahoo Mail but works fine for other servers, can anyone help?
I am using this code to send an SMTP email via the yahoo SMTP server, it is for a personal project I am writing.
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);
It's all pretty standard code for sending SMTP email, but... the server seems to throw an error. It forcibly terminates the connection. This does not happen if I use other SMTP servers like Gmail, Windows Live or various other ISP Smtp servers.
This is the exception and stack trace:
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
I know the problem is not environmental though as I can send to the same server with these exact settings using Outlook Express. I am wondering if I need to send a certificate or something?
If you, or any开发者_Go百科one you know where has any ideas about this I would greatly appreciate some help.
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);
}
}
It's not supported through 465, but the following post details a workaround
How can I send emails through SSL SMTP with the .NET Framework?
UPDATE: This link details why it might work through Outlook Express, but not through the System.Net.Mail
http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx
Port 465 isn't supported by System.Net.Mail.SmtpClient.
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx
From the Remarks Section:
This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.
Edit: You could try using port 587 instead (if Yahoo supports it).
I had the same problem until I set the port to 587 and disabled SSL.
I think you should revert to using System.Web.Mail
which lets you control fields that are not accessible through the newer System.Net.
Try to play with those. For instance you could try this:
(use is documented here, fields are documented here)
MailMessage msg = new MailMessage();
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.mail.yahoo.com");
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
// try "2", I have not tested for yahoo mail
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "1");
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // 0= anonymous - 1=basic - 2=NTLM
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "yahoousername");
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "yahoopwd");
精彩评论