Why would SmtpClient behave differently on XP/Vista/Win7 - c#

I'm using the following code which appears to work perfectly every time on Vista/Win7.
private void SendEmail(string subject, string body, string attach)
{
using (MailMessage message = new MailMessage("username#gmail.com", "username#gmail.com", subject, body))
{
message.IsBodyHtml = true;
if (!string.IsNullOrEmpty(attach))
{
Attachment attached = new Attachment(attach);
message.Attachments.Add(attached);
}
SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("username#gmail.com", "password"),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network
};
client.Send(message);
}
}
However on Windows XP I'm getting:
No connection could be made because the target machine actively refuses it
I've checked and Windows firewall is completely disabled...

Try from the Windows machine the following:
windows key + r
Type cmd
Type telnet smtp.gmail.com 587
If it says connection refused or similar then it's a firewall or network problem, unrelated with the code.

Hard to say if this is it, but we had that problem at one point, and it was an antivirus utility that was the culprit.

Are you using the same version of System.Net.Mail on all three systems ?
Also, could be related to the Windows Firewall blocking connections (or some other firewall).

I doubt this has anything to do with the OS, that type of exception is usually bubbled up from inner ones. Trap the exception and have a look into the inner exceptions and see what the real problem is.
However, this sort of problem is usually a firewall blockage, the remote smtp server is blocking incoming requests or your machine is blocking outgoing requests on port 25.

Related

Sending mail using gmail and .net

Getting the exception:
Failure sending mail.
While using System.Net.Mail.Smtp
in C#.NET on the line smtp.Send(message);
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(sendto);
message.Subject = "CP1 Lab Password";
message.From = new System.Net.Mail.MailAddress("abc#gmail.com");
message.Body = mail_message;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
smtp.Host = "smtp.gmail.com";
smtp.Port = 465; //(465 for SSL)587
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "mypassword");
smtp.Send(message);
Edit 1:
Here is the detail of the error:
A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond ...
The same error when using port 25.
Few months before this code used to work but today it is not working
As from your exception message (incomplete) and the code, it is very hard to say what went wrong in your case. Failure sending mail is thrown when the server is not found, port cannot be connected. It means that connection was not established, until now.
The exception message that gets raised if the connection was established but authentication was not performed correctly is, "The SMTP server requires an authenticated connection...". I would suggest that you check the PORT, SMTP server host (do not add http://) and then re-try. An SMTP connection (in most general cases) requires
SMTP Server host: smtp.gmail.com is enough!
PORT to connect at: I have always used default TCP port for SMTP, 25. It works.
EnableSsl: most require it. I suggest you always use it. client.EnableSsl = true;
Credentials are required by all: No server would allow bots sending emails. In this concern, if you create a new account. You may face trouble sending emails programmatically, I faced the problem with a new account not sending the emails whereas an old account (default account of mine) was sending the email without any trouble.
The following code template (if filled with accurate parameters) will definitely send the email, because I have tested and verified it bazillion times. :)
// You should use a using statement
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
// Configure the client
client.EnableSsl = true;
client.Credentials = new NetworkCredential("<username>", "<password>");
// client.UseDefaultCredentials = true;
// A client has been created, now you need to create a MailMessage object
MailMessage message = new MailMessage(
"from#example.com", // From field
"to#example.com", // Recipient field
"Hello", // Subject of the email message
"World!" // Email message body
);
// Send the message
client.Send(message);
/*
* Since I was using Console app, that is why I am able to use the Console
* object, your framework would have different ones.
* There is actually no need for these following lines, you can ignore them
* if you want to. SMTP protocol would still send the email of yours. */
// Print a notification message
Console.WriteLine("Email has been sent.");
// Just for the sake of pausing the application
Console.Read();
}
Sending an email may be a headache sometimes, because it requires some basic understanding of networking also. I have written an article that covers sending emails in .NET framework and a few problems that beginners usually stumble upon such as this one. You may be interested in reading that article also.
http://www.codeproject.com/Articles/873250/Sending-emails-over-NET-framework-and-general-prob

Sending out an automated email

I have an service that needs to send out automated emails on failures. I feel like i have it set up but i keep on receiving the following error:
Service not available, closing transmission channel. The server response
was: 4.3.2 Service not available
I cant quite figure out where i went wrong, but here is my code:
public static void AutoEmail()
{
try
{
SmtpClient newClient = new SmtpClient();
newClient.Host = "host name";
newClient.Port = Port number;
newClient.Credentials = new System.Net.NetworkCredential(
"username", "password");
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress("something#email.com"));
mail.Body = "This is a test message.";
mail.Subject = "Test - " + DateTime.Now;
mail.From = new MailAddress("something2#email.com");
newClient.Send(mail);
}
catch (Exception ex)
{
Log.WriteException("Error in Email", ex);
}
}
Any help would be much appreciated. Thanks!
Are you sure SMTP server you are using permits applications to send emails? I had met with similar issue and root cause was Exchange server was rejecting the send request due to insufficient permissions. And I have the same steps in my code as in yours. Check for permissions.
Have you tried
newClient .UseDefaultCredentials = true;
Does it help ?
The SMTP server name only works on a computer within the network that contains that SMTP server.
You need to make sure SMTP server's host name and port number in your program is correct. All the other code in your program seem fine.
I experienced the same error before. At the end, I changed to the correct host name and port number and everything works.
For example, Outlook.com Or Hotmail email accounts
host="smtp-mail.outlook.com" port="25" enableSsl="true"
https://www.outlook-apps.com/outlook-com-pop-settings/

Email to yahoo from C# application error

I am able to ping smtp.mail.yahoo.com from my system but when i send email from following code using yahoo address it gives error transport failed to connect to server.
The same code successfully sends the email from gmail account.
I am using port 465 for yahoo.
MailMessage oMsg = new MailMessage();
oMsg.From = from.Text;
oMsg.To = to.Text;
oMsg.Subject = "Hi";
oMsg.BodyFormat = MailFormat.Html;
oMsg.Body = msg.Text;
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", host);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
WebProxy proxy = WebProxy.GetDefaultProxy();
if (proxy.Address != null)
{
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/urlproxyserver", proxy.Address.Host);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/proxyserverport", proxy.Address.Port);
}
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",from.Text);
oMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pass.Text);
// ADD AN ATTACHMENT.
/* MailAttachment oAttch = new MailAttachment(path+ "\\Image.bmp", MailEncoding.Base64);
oMsg.Attachments.Add(oAttch);*/
SmtpMail.SmtpServer.Insert(0,host);
if (proxy.Address != null)
MessageBox.Show("Sending via proxy settings: " + proxy.Address.ToString());
try
{
SmtpMail.Send(oMsg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
oMsg = null;
Any ideas why this error occurs?
Being able (or not) to ping a host does not say anything about whether you will be able to connect to a particular service on it. For that, you need to try to actually establish a connection. (And of course, the fact that you can establish a connection does not necessarily imply that the service in question is working properly.)
Usually, it's a good idea to use telnet to try connecting to the remote host on the port in question. The syntax on the command line is simply telnet host.fqdn.example.com portnumber. This will tell you if there is anything at all at the other end of the pipe responding to connection attempts, which is a first step in determining where the problem is.
Second, it's usually a good idea to trim the code to the minimal version that exhibits the problematic behavior, and include the full code to show the problematic behavior. You are using a number of variables in your code which we really know nothing about.
Some ISPs block outgoing connections to the SMTP ports on hosts other than their own mail servers, to reduce the amount of outgoing spam. Maybe there is a typo in the value in host? Maybe you are inadvertantly using some unexpected MailMessage implementation? And so on.
That said, I would definitely first try to connect to the mail server in question manually, through a proxy if you are using one to connect using that code. If that doesn't work either, then your problem at least has nothing to do with the code in the question, and you can look elsewhere (in which case one possible candidate would be ISP filters; maybe they have a list of allowed external SMTP hosts and Yahoo's isn't on it?).

Sending an Email through gmail SMTP Server (SSL)

I know there's a lot of example on how to send an email using C# but I am really running into some issues and I can't make it to work.
I always get "Failure sending mail" error, Unable to connect to the remote server - No connection could be made because the active machine actively refused it (IP address here).
What does this error mean? And how do I fix this?
Any help will be much appreciated
Here's the code that I've been using: (although I already did try a lot of things)
string SendersAddress = "test#gmail.com";
string ReceiversAddress = "test1#gmail.com";
const string SendersPassword = "test-pass-here";
const string subject = "Testing";
const string body = "Hi This Is my Mail From Gmail";
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 3000
};
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
smtp.Send(message);
}
catch (Exception ex)
{
}
Thanks!
It sounds like you're running into issues connecting to the SMTP server.
Make sure the firewall is open on port 25, and that you actually have an SMTP server running wherever you're trying to establish your connection.
You're trying to establish an SSL connection to gmail on port 587. That port must be used with TLS, not SSL.
Use port 465 instead.
Also note that the Timeout property is expressed in milliseconds, so 3000 can be a little short, depending on your network. Try using a more permissive value, like 30000.
Also worth checking that your anti-virus is not blocking port 25, I have been caught by that before!
Same for me in past few days,
Easy solution:
First check if everything is OK with Telnet, then try to do the same in C#.
Here is a good intro : http://www.wikihow.com/Send-Email-Using-Telnet.
Just beware that some servers use the EHLO command instead of HELO
EDIT:
take a look at the way that you can connect to SMTP server of Google here

how to test connection is established in smtp in c# windows

I want to test connection is established without sending mail.My code is below.I have put wrong email and password but i m not getting any exception.I don't want to test with email send.
try {
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential credentials = new NetworkCredential(textboxprimarymail.Text, textBoxpassprimary.Text);
smtpClient.Credentials = credentials;
connectionestablish.Text = "Connetion Established";
connectionestablish.ForeColor = System.Drawing.Color.ForestGreen;
}
catch {
connectionestablish.Text = "Connetion Error";
connectionestablish.ForeColor = System.Drawing.Color.Red;
}
This question is a duplicate. Basically, there is no way. As stated in another answer here:
There is no way.
SmtpClient can not validate
the usernamepassword without
contacting the serve it connects to.
What you could do is do it outside
SmtpClient, by opening a TCP
connection to the server... but
authentication CAN be complicated
depending how the server is
configured.
May I ask WHY you need to know that
before sending? normal behavior IMHO
would be to wrap sending in
appropriate error handling to catch
exceptions here.
How to validate smtp credentials before sending mail?

Categories

Resources