.NET Framework Developer Center >
.NET Development Forums
>
Network Class Library (System.Net)
>
Send mail message with SSL + Authentication
Send mail message with SSL + Authentication
- Hello, I'm testing a small app that needs to send emails using an SMTP server which requires SSL + Username and Password Authentication on port 465.
I've used the following code:
string to = "myname@gmail.com"; string from = "info@xxx.it"; MailMessage message = new MailMessage(from, to); message.Subject = "Test"; message.Body = @"Test"; SmtpClient client = new SmtpClient("thesmtpserver"); client.UseDefaultCredentials = false; NetworkCredential cred = new NetworkCredential("myname@mycompany.it", "mypassword"); client.Credentials = cred; client.EnableSsl = true; client.Port = 465; client.Timeout = 20000; try { client.Send(message); } catch (Exception e) { Console.WriteLine(e); }
I get a "Timeout Error".
Can anyone help me figure out what is wrong? If I configure Outlook Express with the same configuration it works perfectly.
Best regards
Alessandro
All Replies
- Try getting a tracelog for your application (see instructions in my signature) and see if it can tell you why the timeout is occuring.
feroze
--
My blog
Instruction on how to create a tracelog with your System.Net application
I can't see how the code above utilizes SSL.
Javaman- The code enables SSL by setting EnableSSL=true.
feroze
--
My blog
Instruction on how to create a tracelog with your System.Net application
- Sorry for being late. I've enable tracelog and here's the result:
System.Net Information: 0 : [3440] Associating MailMessage#44419000 with Message#52697953 System.Net Verbose: 0 : [3440] SmtpClient::.ctor(host=thesmtpserver) System.Net Information: 0 : [3440] Associating SmtpClient#22597652 with SmtpTransport#10261382 System.Net Verbose: 0 : [3440] Exiting SmtpClient::.ctor() -> SmtpClient#22597652 System.Net Verbose: 0 : [3440] SmtpClient#22597652::Send(MailMessage#44419000) System.Net Information: 0 : [3440] SmtpClient#22597652::Send(DeliveryMethod=Network) System.Net Information: 0 : [3440] Associating SmtpClient#22597652 with MailMessage#44419000 System.Net Information: 0 : [3440] Associating SmtpTransport#10261382 with SmtpConnection#59109011 System.Net Information: 0 : [3440] Associating SmtpConnection#59109011 with ServicePoint#42659827 System.Net.Sockets Verbose: 0 : [3440] Socket#40644060::Socket(InterNetwork#2) System.Net.Sockets Verbose: 0 : [3440] Exiting Socket#40644060::Socket() System.Net.Sockets Verbose: 0 : [3440] Socket#40644060::Connect(218:465#-629107473) System.Net.Sockets Verbose: 0 : [3440] Exiting Socket#40644060::Connect() System.Net Information: 0 : [3440] Associating SmtpConnection#59109011 with SmtpPooledStream#17043416 System.Net.Sockets Verbose: 0 : [3440] Socket#40644060::Receive() System.Net.Sockets Verbose: 0 : [0932] Socket#40644060::Dispose() System.Net.Sockets Error: 0 : [3440] Exception in the Socket#40644060::Receive - Operazione di blocco interrotta da una chiamata a WSACancelBlockingCall System.Net.Sockets Verbose: 0 : [3440] Exiting Socket#40644060::Receive() -> 0#0 System.Net Error: 0 : [3440] Exception in the SmtpClient#22597652::Send - Impossibile leggere dati dalla connessione del trasporto: Operazione di blocco interrotta da una chiamata a WSACancelBlockingCall. System.Net Error: 0 : [3440] in System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) in System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) in System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) in System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) in System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) in System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) in System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) in System.Net.Mail.SmtpClient.GetConnection() in System.Net.Mail.SmtpClient.Send(MailMessage message) A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll System.Net Verbose: 0 : [3440] Exiting SmtpClient#22597652::Send() System.Net.Mail.SmtpException: Timeout dell'operazione. in System.Net.Mail.SmtpClient.Send(MailMessage message) in GuiTest.Program.Main() in C:\GuiTest\Program.cs:riga 49 - The trace indicates the three-way handshake failed. You sent a packet to nowhere.
Javaman - From the trace it looks like the client connected to the server, but the server did not respond.
Can you try the same code against a different SMTP server?
feroze
--
My blog
Instruction on how to create a tracelog with your System.Net application
- Agreed that's what happened the server didn't respond.
Javaman - I do not understand why it works perfectly if I use the following deprecated code:
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "thesmtpserver"; mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2; mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 465; mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true"; mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1; mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "myname@mycompany.it"; mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "mypassword"; mail.To = "myname@gmail.com"; mail.From = "info@xxx.it"; mail.Subject = "Test"; mail.Body = "Test"; mail.BodyFormat = System.Web.Mail.MailFormat.Html; System.Web.Mail.SmtpMail.SmtpServer = "thesmtpserver"; System.Web.Mail.SmtpMail.Send(mail);
Best regards
Alessandro - Can you get a tracelog of this session, just as you did before?
feroze
--
My blog
Instruction on how to create a tracelog with your System.Net application


