Sending email using smtp on gmail [duplicate] - c#

This question already has answers here:
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
(21 answers)
Closed 7 years ago.
I am trying to send confirmation email to user using smtp provided by google and test in my local machine. I have written the code and supply the settings.
SmtpClient client = SiteUtilites.GetMailClient();
MailAddress sender = new MailAddress(coreEmail, coreDisplayName);
MailAddress receiver = new MailAddress(model.EmailAddress, model.Firstname + " " +model.Lastname);
MailAddressCollection collection = new MailAddressCollection();
MailMessage mailMessage = new MailMessage(sender, receiver);
mailMessage.IsBodyHtml = true;
mailMessage.Body = "Hello";
client.Send(mailMessage);
This is the setting I have below
String smtpServer = ConfigurationManager.AppSettings["smtpServer"];
String smtpUsername = ConfigurationManager.AppSettings["smtpUsername"];
String smtpPassword = ConfigurationManager.AppSettings["smtpPassword"];
String smtpPort = ConfigurationManager.AppSettings["smtpPort"];
SmtpClient sc = new SmtpClient(smtpServer);
NetworkCredential nc = new NetworkCredential(smtpUsername, smtpPassword);
sc.UseDefaultCredentials = false;
sc.Credentials = nc;
sc.EnableSsl = true;
sc.Port = 587;
Do I need my site to run in https ? I just want to test my script in my local machine.
This is the error it gives me
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

It's possible that you are using weak password or using the default credentials.
see following links for more details:
[SOLVED] 5.5.1 Authentication Required with smtp.gmail.com
Sending email through Gmail SMTP server with C#
The SMTP server requires a secure connection or...
Gmail Error :The SMTP server requires a sec...

Related

how to send email in Asp.net mvc 5 [duplicate]

This question already has answers here:
Sending email in .NET through Gmail
(26 answers)
Closed 3 years ago.
What code should I use to send email in C#?
I tried to find a specific code so that I could send an email from my website.
And then I get an error:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
public void sendEmail(string toEmail, string subject, string emailBody)
{
string senderEmail = "My_Email";
string senderPassword = "My_Email_Password";
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Timeout = 500000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(senderEmail, senderPassword);
MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = UTF8Encoding.UTF8;
client.Send(mailMessage);
}
Do I need to use Google API??
Its a security issue, Gmail by default prevents access for your e-mail account from custom applications. You can set it up to accept the login from your application.
You need to go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps. So that you will be able to login from all apps.

Office 365 SMTP sending mails failed for some account

I tried 2 Office 365 accounts for SMTP mail sending. One is ok, the other one failed. I am pretty sure the account name and passwords are right.
Error message:
Message = "The SMTP server requires a secure connection or the client
was not authenticated. The server response was: 5.7.57 SMTP; Client
was not authenticated to send anonymous mail during MAIL FROM
[DM3PR11CA0008.namprd11.prod.outlook.com]"
C# code:
int port = 587;
int.TryParse(configuration["smtp:port"], out port);
string host = configuration["smtp:server"] ?? "smtp.office365.com";
SmtpClient client = new SmtpClient(host, port);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(configuration["smtp:username"], configuration["smtp:password"]);
await client.SendMailAsync(mailMessage);

How to send mail using code from Office365 when TLS is enabled?

My client has an account in Office365 and he recently added this account in the website he is using. Now he says that he cannot send mails using this account, whilst other accounts are OK. He says that TLS is enabled in the account. Anyhow, I am able to login to the mail account. I checked the settings and found that the SMTP settings are as follows:
Server name: smtp.office365.com
Port: 587
Encryption method: STARTTLS
This is a sample code that I am using to check.
String userName = "365useraccount";
String password = "password";
MailMessage msg = new MailMessage();
msg.To.Add(new System.Net.Mail.MailAddress("test#test.com"));
msg.From = new System.Net.Mail.MailAddress(userName);
msg.Subject = "Test Office 365 Account";
msg.Body = "Testing email using Office 365 account.";
msg.IsBodyHtml = true;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.office365.com";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(userName, password);
client.Port = 587;
client.EnableSsl = true;
try
{
client.Send(msg);
}
catch
{
}
I am getting an exception with message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [BMXPR01CA0043.INDPRD01.PROD.OUTLOOK.COM]
However, I am able to send mails using other Office365 accounts.
Any help of idea would be greatly appreciated

.NET SMTP mail - error = helo command rejected need fully-qualified hostname

I am trying to send email in a .NET console application. I have an SMTP server with i.P. address X.X.X.X (sanitized to protect the innocent).
The SMTP server has been set up (the relay configured) to allow email from the server that is hosting the .NET console app, and send that email to the outside world.
I have tested this with telnet from the server hosting the console app. I successfully sent an email with telnet console from the server hosting the console app using the SMTP server's I.P. address. There is no authentication required when using telnet. I have not been given any connection credentials.
But when I try to do it in the .NET app, I get the following error:
Syntaz error, command unrecognized. The server response was: : Helo command rejected: need fully-qualified hostname
Here is my code:
string mailMessagetest = "test";
string subjecttest = "test";
List<string> recipienttest = new List<string>();
recipienttest.Add("me#mydomain.com");
utility.SendMail(recipienttest, subjecttest, mailMessagetest);
Here is the function SendMail:
public static void SendMail(List<string> recipient, string subject, string message)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("myfriend#mydomain.com");
foreach (string to in recipient)
{
mailMessage.To.Add(to);
}
mailMessage.Subject = subject;
mailMessage.Body = message;
SmtpClient client = new SmtpClient();
client.Host = "X.X.X.X";
client.Port = 25;
client.Send(mailMessage);
}
Change the hostname from an IP address to its name, e.g. "smtp.provider.com" or if its internal "mailserver.domain".
UPDATE:
Try read this system.net.mail.smtpclient fqdn required and this the FQDN is not sent when you send a HELO or EHLO.
Create a user with password on your SMTP server then apply the following settings:
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("user", "pass");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Port = 25;
smtp.EnableSsl = true; // or false depending your server

SMTP Service not available

I am trying to create a web application which upon entering your email address and message , sends an email with this information from the email address.
I used this:
try
{
NetworkCredential login = new NetworkCredential("your_____#gmail.com", "password");
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.To.Add(new MailAddress("my____email#gmail.com"));
email.From = new MailAddress("your_____#gmail.com");
email.Subject = "Question";
email.Body = question;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = login;
client.Send(email);
}
catch
{
}
But its giving me an SMTP error.
"Service not available, closing
transmission channel. The server
response was: Cannot connect to SMTP
server 209.85.129.111
(209.85.129.111:25), connect error
10051" System.Exception
{System.Net.Mail.SmtpException}
To send through your gmail account, you need to connect to port 587:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
You do not need to specify port 587 - the code works without it. I have successfully sent and received e-mail using:
SmtpClient client = new SmtpClient("smtp.gmail.com");
If you look at the error closely, it says "Cannot connect to SMTP server" and error 10051 means the network is unreachable. Do you have a firewall blocking port 587?
Gmail uses port 465 and the erros show port 25
try using 465 port
http://mail.google.com/support/bin/answer.py?answer=76147

Categories