views:

1029

answers:

4

Hi,

I'm trying to send an email using my godaddy hosting.

I've also given away the username and password (as you can see in the code) for the testing email account so if anyone has the time, can they try and send an email using these settings? I'm totally at a brick wall.

I've got Thunderbird setup and it connects and sends emails fine using the settings below, but when I run this peice of code, it just times out. If Thunderbird can send email to this server then It must be my code. Its really frustrating.

SmtpClient ss = new SmtpClient();

                try {
                    ss.Host = "smtpout.secureserver.net";
                    ss.Port = 465;
                    ss.Timeout = 10000;
                    ss.DeliveryMethod = SmtpDeliveryMethod.Network;
                    ss.UseDefaultCredentials = false;
                    ss.Credentials = new NetworkCredential("[email protected]", "abc123", "comicidolonline.com");
                    ss.EnableSsl = true;

                    MailMessage mailMsg = new MailMessage("[email protected]", "[email protected]", "subject here", "my body");
                    mailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                    ss.Send(mailMsg);

                    Console.WriteLine("Sent email");
                    Console.ReadKey();
+1  A: 

When you say it worked with Thunderbird, I'm assuming you tried from Thunderbird on the webserver? (You RDP'd in?)

Also, if this is a web app (assuming it is, since you are referring to godaddy), why do you have Console statements in a web app?!? There isn't any console involved.

Also, you say the code isn't working. How do you know? What is the exception?

Cheers! Dave

dave wanta
Well I took the code and put it into a console app so I dont have to run the web application each time.I'm trying to send email using c# on my own machine. The settings I've mentioned above work in thunderbird which proves that the host, password and email work. The bit of code I've posted does not work. I get a timeout exception. Feel free to try, the username and password I've supplied are real :-)
Sir Psycho
ahh...I think I see the problem (just did a quick telnet). Here is what I think is happening.Your mail server wants the SSL session initiated before the SMTP session ever starts. This is how Thunderbird connects with SMTP over SSL.However, some mail servers (per the RFCs), switch the SMTP session to SSL *after* the 220 welcome response is sent back. This is how System.Net.Mail works. It wants to see the first 220, and then switch to SSL.Since the 220 response is never sent (because the SSL session wasn't started-- a catch 22), System.Net.Mail timesout.
dave wanta
Thank you Dave. At least you've shed some light on the subject. So I guess its not possible to fix.
Sir Psycho
@Dave, please consider adding that very useful comment to the answer itself?
Arjan
+3  A: 

Hi, Since you are asking how to send email, and it can't be sent with System.Net.Mail (because of the SSL issue), one option is to use aspNetEmail (something I wrote -- so this is partially a shameless plug). I'm not aware of any open source email component that can do this, with the SSL connection required.

Here is some code (just tested, and it works).

EmailMessage m = new EmailMessage();
m.LogPath = "c:\\email.log";
m.Logging = true;

m.Server = "smtpout.secureserver.net";                    
m.Port = 465;                    
m.TimeOut = 10000;                    
m.Username = "[email protected]";
m.Password = "abc123";            

m.From ="[email protected]";
m.To = "[email protected]";
m.Subject = "subject here";
m.Body = "my body";                    

AdvancedIntellect.Ssl.SslSocket ssl = new AdvancedIntellect.Ssl.SslSocket();
//load the SSL socket, but tell it to connect *before* the 220 Welcome response
m.LoadSslSocket( ssl, true );

m.Send();                    
Console.WriteLine("Sent email");

If you want to try aspNetEmail, feel free to download the latest build update from link text

You will also need a free eval license, which you can get from: link text

If you have any other questions, feel free to ping me offline.

Cheers!

Dave

(more info added during later edit) I couldn't find the orignal doc on MSDN that I was thinking of (it's been a few years). But here is a MSDN blog post from Dan Bagley. http://blogs.msdn.com/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx

System.Net.Mail with SSL to authenticate against port 465 Sending mail using System.Net.Mail with SSL will fail:

System.Net.NetworkCredential aCred = new System.Net.NetworkCredential("myacct", "mypassword");

SmtpClient smtp = new SmtpClient("smtp.mail.myserver.com", 465);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = aCred;

System.Net.Mail only supports “Explicit SSL”.

Explicit SSL

System.Net.Mail only supports “Explicit SSL”. Explicit SSL starts as unencrypted on port 25, then issues a STARTDLS and switches to an Encrypted connection. See RFC 2228.

Explicit SLL would go something like: Connect on 25 -> StartTLS (starts to encrypt) -> authenticate -> send data

If the SMTP server expects SSL/TLS connection right from the start then this will not work.

Implicit SSL

There is no way to use Implicit SSL (SMTPS) with System.Net.Mail. Implicit SSL would have the entire connection is wrapped in an SSL layer. A specific port would be used (port 465 is common). There is no formal RFC covering Implicit SSL.

Implicit SLL would go something like: Start SSL (start encryption) -> Connect -> Authenticate -> send data

This is not considered a bug, it’s a feature request. There are two types of SSL authentication for SMTP, and we only support one (by design) – Explicit SSL.

dave wanta
Hi Dave, thanks for putting in time for the response, but I was hoping there was a .NET workaround or way around this issue without me buying anything. Thanks again for your time.
Sir Psycho
Nope, can't do this with System.Net.Mail. This is a known issue, and is documented on MSDN. IIRC, this was actually submitted as a bug when .NET 2.0 was released in beta. However, since System.Net.Mail is tecnically RFC compliant in this scenario, it wasn't really considered a bug, and MS has no intention of providing a work-around.
dave wanta
I don't recall reading anything about this in the past. Dave, can you post a link to the MSDN documentation for this or something like that? Just because this scenario isn't 100% defined in the RFC doesn't mean that MS won't support it if there's enough customer demand. Also, even in the updated RFC's (5321 and 4409) it's not mentioned anywhere I could find. Please provide some background so that I can bring this up with our PM as something to address for a future .Net release. Thanks
Jeff Tucker
I couldn't find the original doc I was thinking of...but some quick googling turned up:http://blogs.msdn.com/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspxfrom the link--- System.Net.Mail only supports “Explicit SSL”. ... "This is not considered a bug, it’s a feature request. There are two types of SSL authentication for SMTP, and we only support one (by design) – Explicit SSL. "
dave wanta
just added an edit to the above post, with more complete information.
dave wanta
+1  A: 

I found this post about GoDaddy's SMTP settings

THe code below worked for me. I had to change the port to the default SMTP port, and remove the domain specification from the NetworkCredentials.

 SmtpClient ss = new SmtpClient();

        try
        {
            ss.Host = "smtpout.secureserver.net";
            ss.Port = 25;
            ss.Timeout = 10000;
            ss.DeliveryMethod = SmtpDeliveryMethod.Network;
            ss.UseDefaultCredentials = false;
            ss.Credentials = new NetworkCredential("[email protected]", "abc123");

            MailMessage mailMsg = new MailMessage("[email protected]", "[email protected]", "subject here", "my body");
            mailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            ss.Send(mailMsg);

            Console.WriteLine("Sent email");
            Console.ReadKey();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadKey();
        }
scottm
you did not use ssl flag. That seems to OP's issue.
krishna
I didn't see anywhere in the post where "I need SSL socket" was specified, so I figured just getting it to work was enough.
scottm
See my answer for how the EnableSsl flag is used internally in SmtpClient but you are correct, that's the issue.
Jeff Tucker
A: 

Remove the EnableSsl = true and see if that fixes it. The way SmtpClient works is that if credentials are provided and the server advertises the AUTH keyword in the EHLO response headers, and when we try to do something it asks for auth, we attempt to auth with the provided credentials. However, with EnableSsl, if the server does NOT advertise STARTTLS as a response to the EHLO then we throw immediately since we will not be able to use it. Therefore, unless you are 100% CERTAIN that the smtp server you are using supports SSL (TLS actually) you should not set EnableSsl and leave it at its default value of false. If you post up the exception you're getting I can tell you why it's being thrown.

Jeff Tucker