Author Topic: Windows 8 breaks send mail code?  (Read 2336 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Windows 8 breaks send mail code?
« on: March 24, 2013, 11:17:09 AM »
I am at wits end here. The following code has worked in XP, Vista, and Win7 with minor tweaks for the changes in .NET versions. However, I cannot get it to work in Win8. I get a Timeout error every time, no matter what minor edits I've tested. I've searched extensively with Google and Bing and have not seen any other similar reports, much less solutions to the problems.  I understand that Win8 is fairly new, but it's been out long enough now for at least some to have been moving their code forward. Any hints from the gurus here, besides "forget Win8", would be greatly appreciated.

Code - C#: [Select]
  1.             SmtpClient client = new SmtpClient();
  2.             client.Host = my email server;
  3.             client.Port = Convert.ToInt32(25);
  4.             client.Timeout = 4000;
  5.             client.EnableSsl = false;
  6.             client.DeliveryMethod = SmtpDeliveryMethod.Network;
  7.             client.UseDefaultCredentials = true;
  8.             NetworkCredential credentials = new NetworkCredential(email account, password);
  9.             client.Credentials = credentials;
  10.  
  11.             string msgBody = "some text from a form";
  12.             MailAddress clientAddress = new MailAddress(ueser_entered_email);
  13.  
  14.  
  15.             MailAddress myAddress = new MailAddress("myemail@isp.net");
  16.  
  17.             MailMessage msg = new MailMessage(clientAddress, myAddress);
  18.             msg.Subject = "Test email";
  19.  
  20.             msg.ReplyToList.Add(clientAddress);
  21.             msg.IsBodyHtml = false;
  22.             msg.Body = msgBody;
  23.  
  24.             try { client.Send(msg); }
  25.             catch(SmtpException err)
  26.             {
  27.                 string emsg = err.Message;
  28.              }
  29.  

In regards to this code in previous windows versions, it does sometimes fail without an exception, meaning the user never knows that it has failed. I think this is due to their network configuration but have not been able to track down  the culprit. Since no error is thrown I'm not quite sure how to address this issue.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Windows 8 breaks send mail code?
« Reply #1 on: March 24, 2013, 11:28:23 AM »
Well, for one you could start by trapping all errors for the entire bit of code, instead of only trapping SmtpException when you try to send the mail. Perhaps the Win8 implementation of SmtpClient requires different/more properties be used.

I could see something like this happening due to increased security measures

or

Have you checked to see that the Win8 firewall isn't blocking SMTP access from an unknown program?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Windows 8 breaks send mail code?
« Reply #2 on: March 24, 2013, 11:53:16 AM »
Thanks, Keith,
I actually had the Catch set to trap everything, and this is the only exception type thrown so I changed it.

I tested with the firewall turned off and got the same result. Not sure what other security measures I'd need to check for.

Gasty

  • Newt
  • Posts: 90
Re: Windows 8 breaks send mail code?
« Reply #3 on: March 24, 2013, 12:55:44 PM »
Hi,

I'm not using w8 for development, but I usually don't set UseDefaultCredentials, just set the credentials to be used, also I set the port to be used (usually default to 25) . If you have access to the smtpserver, you can take a look at  the logfile in order to check some error logged in.

Gaston Nunez


Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Windows 8 breaks send mail code?
« Reply #4 on: March 24, 2013, 01:02:21 PM »
Thanks, Gaston. I originally had not used the UseDefaultCredentials, I only added it trying to make the code work. I do set the port to 25 in the third line of code. Not sure how to look for errors on the server...it is a GoDaddy smtp server.

n.yuan

  • Bull Frog
  • Posts: 348
Re: Windows 8 breaks send mail code?
« Reply #5 on: March 24, 2013, 01:29:25 PM »
Thanks, Gaston. I originally had not used the UseDefaultCredentials, I only added it trying to make the code work. I do set the port to 25 in the third line of code. Not sure how to look for errors on the server...it is a GoDaddy smtp server.

Where do you run the code? Since you mention Win 8 and this is Acad discussion forum, I assume the code runs in your computer (maybe from your Acad addin app).

Well, GoDaddy is public accessible service hosted outside the network of your computer, you definitely DO NOT use client.UseDefaultCredential=true. It should be false and you need to create a network credential with valid user name/password. and you may also set client.EnableSsl=true (very likely).

To be honest, I am not sure why your code worked previously with WinXP/7, if you used UseDefaultCredential=true, it sounds strange, at least.

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Windows 8 breaks send mail code?
« Reply #6 on: March 24, 2013, 02:13:13 PM »
Norman, as I noted to Gaston, I did not previously use the  client.UseDefaultCredential=true, I only added that in an attempt to get it to work. You can also see that the very next line of code sets the valid credentials which should be overriding the default setting anyway (regardless, I get the same error with or without the UseDefaultCredentials line).

Yes, this is used inside of Autocad in my addin as a way for users to request am activation code. We have used it for the last 5 years, with only a few users that end up not having the email sent, and no error is generated for those users.

Gasty

  • Newt
  • Posts: 90
Re: Windows 8 breaks send mail code?
« Reply #7 on: March 24, 2013, 03:19:49 PM »
Hi,

Are You sure about the port number?, usually ISP change the default port. Any way 2 ideas, use some third party (free) component like this: www.dimac.net, or use a different approach using a socket like the code in this page:http://www.eggheadcafe.com/articles/20030316.asp.

Gaston Nunez
 

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Windows 8 breaks send mail code?
« Reply #8 on: March 24, 2013, 03:21:45 PM »
I'm attaching a small sample WinForm project (VS2010) which exhibits the issue. This works fine in Win7 but fails to send, with a Timeout error, in Win8.

Edit: Removed file due to it containing information about my mail server
« Last Edit: March 24, 2013, 06:44:18 PM by Jeff_M »

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Windows 8 breaks send mail code?
« Reply #9 on: March 24, 2013, 05:03:47 PM »
OK, more testing. Trying to Telnet from a cmd window into the mail server fails in Win8 with "Could not open connection with the host on port 25: connect failed". Performing the same thing in Win7 connects immediately. So this must be a Win8 security setting, but I sure can't find anything. And even worse, I can't expect my customers to worry about this. Looks like I will have to stop using the auto-email and have the user manually send the data.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Windows 8 breaks send mail code?
« Reply #10 on: March 24, 2013, 06:07:58 PM »
Try using port 587 and authenticating the user ... better yet, create a configuration so the user can enter their own account info to send SMTP mail
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Windows 8 breaks send mail code?
« Reply #11 on: March 24, 2013, 06:42:49 PM »
Well now, more research has given me 2 more ports to try with the secureserver.net (GoDaddy's email server) address. Changing the port to 80 has now allowed the email to be correctly sent. And still works with the Firewall turned on with no adjustments.