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

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?

Related

Why can't I send e-mail from a C# app when I can send e-mail from a mail client just fine?

I have a task to troubleshoot why a C# app is failing to send automated e-mail messages. I carefully checked the source code, and could find absolutely nothing wrong.
Therefore, I tried to send e-mail from Thunderbird, the e-mail client I normally use. I specified the same SMTP relay, the same UID and password, and everything worked fine.
Trying to isolate the problem, I tried writing a very short C# console app to see what might be going wrong. I wrote the following:
using System.Net;
using System.Net.Mail;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
var client = new SmtpClient("my.server.with.ssl", 465);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("uid", "pwd");
var message = new MailMessage("me#example.com", "myemail#mydomain", "Test Message Subject", "Test Message Body");
client.Send(message);
}
}
}
I entered the same credentials that I used in Thunderbird. When I run the above, I get the following error:
Unhandled Exception: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
How can it be that e-mail is sent just fine from Thunderbird, but the simple programming above fails to work when I try to send an e-mail from a basic C# app?
Edit I tried changing the port number to 587 as suggested in the Question that #stuartd linked to, and in that case, I get an error that says The operation timed out.
Edit I've tried using other e-mail servers and adjusting the settings, but nothing works so far. I've tried connecting to the same SMTP server that I use for my personal e-mail and it shows an error that the connection times out.
Edit I can't say why, but everything seems to be working now in spite of the fact that I didn't change any code. It seems as if something odd happened with my connection.
try using the default constructor and specify the host only.
Also some servers require that the client be authenticated before the server sends e-mail on its behalf. Try changing the value of UseDefaultCredentials
var mail = new MailMessage();
mail.From = new MailAddress("xxx#gmx.net");
mail.To.Add("yyy#gmail.com");
mail.IsBodyHtml = true;
mail.Subject = "subject";
mail.Body = "content";
var client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Host = "mail.gmx.net";
client.EnableSsl = true;
client.Credentials = new NetworkCredential("user", "pass");
client.Send(mail);
if that doesn't help than tell us the server name if its the public one
Try using:
SmtpClient smtpClient = new SmtpClient("mail.mymailhost.com");
smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

Cannot send email with SMTP in Windows Forms

So, I am working on windows Forms and trying so send email with smtp.
Here is a Code:
MailMessage mail = new MailMessage(from, to, subject, text);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 465;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(from, password);
try
{
client.Send(mail);
MessageBox.Show("Mesage has benn sant");
}
catch (Exception ex)
{
MessageBox.Show("Failure while sending message");
MessageBox.Show(ex.Message);
}
When I run this Code I am getting Following Error:
"Failure sending email".
When I changed port to 587 I have got following:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication REquired".
So why I cannot send email ? Can someone explain me that ?
I have changed port to 25 and have sent mail from hotmail( I have replaced "smtp.gmail.com" with "smtp.live.com"), not from gmail. And it works. Seems it's something wrong with gmail.
I was able to make your code work by adding the following three lines and making change in the gmail account setting:
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential (from, password);
In addition to the above changes, it turns out, you also need to update your google account settings and allow less secure apps.
I learned about this on this thread: Sending email in .NET through Gmail
Hope this helps.
Firstly change the port to 25.
Also, the arguments for client.Credentials are supposed to be your username and password to login with. For example...
client.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "mypassword");
I had the same problem. Was able to fix it by disabling this security setting:
going to this link and turning on "access for less secure apps"
https://www.google.com/settings/security/lesssecureapps
enter image description here

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

SMTP email client is throwing errors "Timeout" or "requires secure connection" even with correct credential

I was new to SMTP client in C#. So I decided to test it with my credentials. I built an ASP.NET Web forms application that has a Contact Us page on which I am trying to send an email to whoever person fills the form.
I tried sample code after going through this article - https://www.c-sharpcorner.com/UploadFile/87b416/sending-a-simple-email-using-smtpclient-in-C-Sharp/
I have one account in Yahoo so I used its SMTP domain "smtp.mail.yahoo.com" with port number: 465, then my app always threw Timeout exception. So I decided to try with Google's server "smtp.gmail.com" with "587" port and now it raised different exception with message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
I don't understand what are the prerequisites for working with SMTP on secure servers like Google and Yahoo. Please someone explain.
Also note that I didn't have 2 step verification enabled for my Google account, just to make it clear since some questions on SO have mentioned that this might be the problem.
I also read this question but I am testing directly on my machine - Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
In case if it helps, here is the sample code:
try
{
MailMessage m = new MailMessage();
m.From = new MailAddress("dummy123#gmail.com");
m.To.Add(new MailAddress("dummyreceiver123#gmail.com"));
m.Subject = TBSub.Text;
m.Body = TBBody.Text;
m.IsBodyHtml = true;
NetworkCredential nc = new NetworkCredential();
nc.UserName = "dummy123"
nc.Password = "dummy#123";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = nc;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(m);
}
catch (Exception ex)
{
//Log this error
}
I just tested out your code it works fine you just need to modify this section:
nc.UserName = "test"
nc.Password = "password";
This has to be a valid gmail or google app email along with the password for the smtp connection to work properly. I would recommend that you put in your own for testing purposes, and then modify this to have your email as well:
m.From = new MailAddress("yourEmail#gmail.com");
m.To.Add(new MailAddress("yourEmail#gmail.com"));
Just so that you can validate that your message is being passed from your function.

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/

Categories

Resources