9

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 anyone you know where has any ideas about this I would greatly appreciate some help.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
David Honess
  • 153
  • 1
  • 1
  • 5
  • Yahoo probably has (different) anti-spam policies in place. You'll have to consult them what the rules are. – H H Feb 23 '11 at 14:40
  • Are the NetworkCredential parameters correct? user name being the full email address – Tony Feb 23 '11 at 14:43
  • Thanks for the input chaps. Yes I have checked their documentation and they specify to use the username without the @yahoo.com. As I said it does work though Outlook Express with the extact same settings. – David Honess Feb 23 '11 at 14:51
  • Are you absolutely sure you can send email in Outlook? It was my understanding that you had to upgrade to Yahoo Mail Plus to use a third-party client. – Phil Feb 23 '11 at 14:53
  • @David: check and compare the protocol and port settings in Outlook Ex. But Yahoo could still look at other features. – H H Feb 23 '11 at 15:08
  • Thanks Henk. Phil yes I am sure I sent an email via OE, I recieved the emails via my Gmail account so I know it does work. Statto's post below may be the answer though! – David Honess Feb 23 '11 at 15:14
  • Is it a Yahoo Plus account or just the free one? I know that POP3 is only available to the Yahoo Plus accounts and wondering whether the same applies for the SMTP side too? – statto Feb 23 '11 at 14:52
  • It's a free one. I know that I can send SMTP email though my account as it does work through Outlook Express. – David Honess Feb 23 '11 at 15:06

5 Answers5

6
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);
    }
}
jacobsgriffith
  • 1,448
  • 13
  • 18
3

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

Community
  • 1
  • 1
statto
  • 492
  • 2
  • 6
  • 16
  • The old System.Web.Mail/CDO method will work, but the whole namespace has been deprecated since 2.0. It'd probably be fine for this project (being a "Personal Project"), but I wouldn't use it on anything that's going to production. – The Moof Feb 23 '11 at 15:08
2

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

The Moof
  • 794
  • 4
  • 10
0

I had the same problem until I set the port to 587 and disabled SSL.

man910
  • 159
  • 1
  • 9
-1

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");
Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86