SmtpClient is unable to connect to Gmail using SSL - c#

I see many posts on SMTPClient but I'm still stuck.
I am creating a C# Console application targeting the .NET 4.0 framework and running on a Windows 2008 R2 Datacenter (server).
I want send an email using SSL from a gmail account I just created.
If I use port 587, I get an exception:
The SMTP server requires a secure connection or the client was not authenticated
If I use port 465, I get another one:
Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall.
I realize this question has been asked several times in the past. All answers I see relate to the order in which UseDefaultCredentials is set. It must be set to false BEFORE the real credentials are set. I have followed this advice (as per the code below) and yet I still get a failure.
Other answers include switching ports from 465 to 587... which i have tried and just results in a different error (as stated above).
Here's my code:
NetworkCredential credentials = new NetworkCredential("something#gmail.com", "2-step-auth-Password");
MailAddress address = new MailAddress("something#gmail.com");
SmtpClient client = new SmtpClient("smtp.gmail.com", 465); // or 587
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = credentials;
client.Timeout = 10000; // 10 seconds.
client.Host = "smtp.gmail.com";
//client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
MailMessage message = new MailMessage("example#gmail.com", recipients);
message.Subject = "Hello";
message.Body = "Goodbye";
message.BodyEncoding = Encoding.UTF8;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString());
}
message.Dispose();
Update 2: I see another answer which may be my issue: gmail requires an application-specific password (which is apparently not available with my current gmail account. not sure why yet). but this may be my issue.
Update 3: I have followed all instructions. Generated 2-step authentication with Gmail, and still fails.

Below code works for me, you can try this:
SmtpClient smtp = new SmtpClient("smtp.office365.com");
//Your Port gets set to what you found in the "POP, IMAP, and SMTP access" section from Web Outlook
smtp.Port = 587;
//Set EnableSsl to true so you can connect via TLS
smtp.EnableSsl = true;
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("Username", "Password");
smtp.Credentials = cred;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("Sender Email Address", "Sender Name");
StringBuilder mailBody = new StringBuilder();
mail.To.Add("ToEmailAddress");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "Body Text";
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}

Related

Access denied when sending a mail from C# programm

I'm developing a third-party add-on to run in a program called M-Files.
The purpose of the add-on is to send a mail with the help of an SMTP server. I created a fake SMTP server in DevelMail.com just for testing.
Testing the SMTP server from a browser works but when i run the code it gives me the following error.
Transaction failed. The server response was: 5.7.1 Client host rejected: Access denied
Here are the SMTP information:
Host: smtp.develmail.com
SMTP Port: 25
TLS/SSL Port: 465
STARTTLS Port : 587
Auth types: LOGIN, CRAM-MD5
Here is the code:
MailAddress adressFrom = new MailAddress("notification#mfiles.no", "M-Files Notification Add-on");
MailAddress adressTo = new MailAddress("majdnakhleh#live.no");
MailMessage message = new MailMessage(adressFrom, adressTo);
message.Subject = "M-Files Add-on running";
string htmlString = #"<html>
<body>
<p> Dear customer</p>
<p> This is a notification sent to you by using a mailadress written in a metadata property!.</p>
<p> Sincerely,<br>- M-Files</br></p>
</body>
</html>
";
message.Body = htmlString;
SmtpClient client = new SmtpClient();
client.Host = "smtp.develmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword");
client.EnableSsl = true;
client.Send(message);
Reason for the Issue:
Usually, email sending option using SMTP encountered Access denied
because there should have a sender email which required to allow
remote access. When SMTP request sent from the sender email
it checks whether there is remote access allowed. If no, then you
always got Access denied message.
Solution:
For example let's say, you want to send email using Gmail SMTP in that case you do have to enable Allow less secure apps: ON
How To Set
You can simply browse this link Less secure app access and turn that to ON
See the screen shot
Code Snippet:
public static object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath)
{
try
{
MailMessage mail = new MailMessage();
//Must be change before using other than gmail smtp
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = mailSubject;
mail.Body = mailBody;
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);//Enter the credentails from you have configured earlier
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Note: Make sure, fromEmail and (senderName, senderPass) should be same email with the credential.
Hope that would help.

The SMTP server requires a secure connection or the client was not authenticated in C# ASP.NET

I am trying to send an email but i am getting authentication failure.
Do we need any specific type of authentication for Google mail.
[Error in Browser][1]
// Created Mail message and taken inputs
MailMessage mail = new MailMessage(from,to,subject,message);
// Used SMTP Client
SmtpClient client = new SmtpClient();
// Google Port
client.Port = 587;
client.Credentials = new NetworkCredential(From.Text, password.Text);
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(mail);
Label.Text = "Sent Successful";
I am getting error near Send(mail)
You're not setting the SMTP server address?
Assuming your credentials are correct then you should use SmtpClient client = new SmtpClient("smtp.gmail.com") and client.Port = 465. 587 is for TLS

C# "Additional information: The SMTP server requires a secure connection or the client was not authenticated."

So im trying to send an email on C# and always getting the same error: "Additional information: The SMTP server requires a secure connection or the client was not authenticated." i have checked out pretty much every single link i could find about this i have diabled all gmail protection and my credentials are correct i don't know why this error continues
MailMessage mail = new MailMessage();
mail.From = new MailAddress("xx#gmail.com");
mail.Sender = new MailAddress("xx#gmail.com");
mail.To.Add("external#emailaddress");
mail.IsBodyHtml = true;
mail.Subject = "Email Sent";
mail.Body = "Body content from";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("xx#gmail.com", "xx");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Timeout = 30000;
try
{
smtp.Send(mail);
}
catch (SmtpException ex)
{
MessageBox.Show(ex.Message.ToString());
}
This is the exception message from C#, what is the actual response from the server? You may need to enable "less secure apps" to connect to your gmail account first (see https://www.google.com/settings/security/lesssecureapps).
I have tried your code getting the error "5.5.1 Authentication required". Please give this link a try Sending email through Gmail SMTP server with C#

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

I'm trying out very simple tutorial on how to send e-mail via .NET and C#. However when I try to execute the code I got the following exception:
An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The operation has timed
out.
So what I have done is to find the server settings to which I want to send mails to. This is very popular in my country:
Incoming settings
Protocol -> POP
Email address -> username#abv.bg
Username -> username#abv.bg
Password -> password
POP server -> pop3.abv.bg
Security type -> SSL
Server port -> 995
Outgoing server settings
Username -> username#abv.bg
Password -> password
SMTP server -> smtp.abv.bg
Security type -> SSL
Server port -> 465
then I created a Console project and in my main method I have this code:
SmtpClient client = new SmtpClient("smtp.abv.bg");
client.Port = 465;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(
"username#abv.bg", "password");
MailMessage msg = new MailMessage();
msg.To.Add("username#abv.bg");
msg.From = new MailAddress("username#abv.bg");
msg.Subject = "Test subject";
msg.Body = "Test test test...";
client.Send(msg);
Since I have no experience with this I just try the code as you see it. No additional settings anywhere. The only thing that I think should not be problem but I think worth mentioning that here:
client.Credentials = new NetworkCredential(
"username#abv.bg", "password");
and here:
msg.To.Add("username#abv.bg");
I'm using the same e-mail. But I think this shouldn't be a problem. Any idea what am I doing wrong here?
Here is a working example (with Gmail) I wrote years ago for testing.
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("xxx#gmail.com");
mail.From = new MailAddress("yyy#gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yyy#gmail.com", "pw"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}
I would try this function with Gmail just to eliminate network related issues like firewall. When that will work, the rest is just to find the right settigs for the SMPT server

Relay access denied 5.7.1

I've got the error "Relay access denied", but I can connect my account with some email programs.
My code:
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("xx#xx.com", "xxx");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("xx#xx.com");
smtpClient.Host = "mail.xx.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
message.To.Add("aa#aa.com");
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
//Relay access denied...
}
Does anyone know the reason for this?
Since your smtp host port is 587, I think you should set smtpClient.EnableSsl to true before calling its Send method.
Although this question is almost a year old now, I just stumbled upon the solution to a similar problem today. Apparently, the System.Net.Mail.SmtpClient class does not support the AUTH PLAIN method. Unfortunately, the SMTP server I was trying to use only offered AUTH PLAIN. Since the SmtpClient class and my server couldn't agree on an authentication method, the SmtpClient tried to send mails without authentication which got rejected by the SMTP server...
You should make sure that the SMTP server you are using offers at least one of the authentication mechanisms that SmtpClient supports. I couldn't find an exhaustive list of supported mechanisms but my server is now offering AUTH LOGIN which works with SmtpClient.
Please try
SmtpClient client = new SmtpClient("mail.xx#xx.com");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("xx#xx.com", "xxx");
client.Port = 587;
client.EnableSsl = true;
MailMessage maili = new MailMessage();
maili.Body = body;
maili.Subject = subject;
maili.IsBodyHtml = true;
maili.From = new MailAddress("xx#xx.com");
maili.To.Add("aa#aa.com");
try
{
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
client.Send(maili);
}
catch (Exception ex)
{
throw ex;
}
maili.Dispose();
In my case, the smtp password had changed, and gave me this error...
Usually it's very simple: the "From" e-mail address must have the same domain name as the domain name of the account from which the e-mail is being sent. So, if you send e-mail through a Hotmail account with the e-mail address e.g. "john.doe#hotmail.com", the "From" address must be set to the same.

Categories