C# SMTP email sending code fails for Yahoo Mail but works fine for other servers, can anyone help?
Asked Answered
C

5

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("[email protected]", 
                                         "[email protected]");

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.

Commodus answered 23/2, 2011 at 14:31 Comment(8)
Yahoo probably has (different) anti-spam policies in place. You'll have to consult them what the rules are.Titus
Are the NetworkCredential parameters correct? user name being the full email addressPrimo
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.Commodus
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?Enteritis
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.Ecdysis
It's a free one. I know that I can send SMTP email though my account as it does work through Outlook Express.Commodus
@David: check and compare the protocol and port settings in Outlook Ex. But Yahoo could still look at other features.Titus
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!Commodus
E
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

Enteritis answered 23/2, 2011 at 15:2 Comment(1)
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.Conterminous
C
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("[email protected]"), new MailAddress("[email protected]"));
        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);
    }
}
Cairngorm answered 26/9, 2011 at 17:59 Comment(2)
does it takes time to forward mail?Discard
I am getting: Mailbox unavailable. The server response was: Requested mail action not taken: mailbox unavailableLilllie
E
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

Enteritis answered 23/2, 2011 at 15:2 Comment(1)
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.Conterminous
C
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).

Conterminous answered 23/2, 2011 at 14:58 Comment(0)
E
0

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

Everywhere answered 24/5, 2012 at 0:3 Comment(0)
D
-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");
Disjunct answered 23/2, 2011 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.