Exception message while sending an email - c#

Exception:
The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.7.1 Client was not
authenticated
I am not using Defaultcredentials, so I set client.UseDefaultCredentials = false;
Also, I provided valid email id and password, host name and port number; however, I am
still getting the same Exception. How can I resolve this issue? Thanks in advance for any help.
public void SendCustomerForgotPassword(string mailTo, string forgotUserId, string forgotPassword)
{
string mailSubject = "", mailBody = "";
mailSubject = "";
StringBuilder sbMailBody = new StringBuilder();
sbMailBody.AppendLine("Welcome to *****,");
sbMailBody.AppendLine("\r");
mailBody = sbMailBody.ToString();
SetSmtpClient(mailTo, mailSubject, mailBody);
}
private void SetSmtpClient(string recipients, string subject, string body)
{
string EmailFrom = "here set an email id";
string EmailFromPassword = "set password";
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(EmailFrom, EmailFromPassword);
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "pod51018.outlook.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = credentials;
client.Send(EmailFrom, recipients, subject, body);
}

Just like the message states, you're not authenticated.
In your code:
string EmailFrom = "";
string EmailFromPassword = "";
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(EmailFrom, EmailFromPassword);
If this is your exact code - you're passing an empty username and password..

You will need to check the following details are correct (as in the details shown on your Office 365 panel)
Host Name
E-Mail Address
Password
The code appears good (assuming your actually passing an address/password since it's a blank string in your code) so it's likely an issue in your settings.

Related

C# SMTP MailMessage receiving error "5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM"

The code I currently have is:
public void SendEmail(string to, string cc, string bcc, string subject, string body, string attachmentPath = "", System.Net.Mail.MailPriority emailPriority = MailPriority.Normal, BodyType bodyType = BodyType.HTML)
{
try
{
var client = new System.Net.Mail.SmtpClient();
{
client.Host = "smtp-mail.outlook.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("[my company email]", "[my password]");
client.Timeout = 600000;
}
MailMessage mail = new MailMessage("[insert my email here]", to);
mail.Subject = subject;
mail.Body = body;
client.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The email address I'm trying to send to is hosted on Office 365's Outlook. We might have to change the specific address later, but they'd likely be configured the same.
However, whenever I try to run the client.Send(mail); command, I receive the same error. The full text of the error is:
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
I've tried a few different things, like switching the port between 25 and 587, changing the host to Office365's, or toggling UseDefaultCredentials and EnableSssl to true or false. But I always see the same error. Is there something else I'm missing?
I found an example code block elsewhere on this site and replacing everything I had with it made the difference.
The function name and parameters were the same, but here's what I replaced the body of it with.
var _mailServer = new SmtpClient();
_mailServer.UseDefaultCredentials = false;
_mailServer.Credentials = new NetworkCredential("my email", "my password");
_mailServer.Host = "smtp.office365.com";
_mailServer.TargetName = "STARTTLS/smtp.office365.com";
_mailServer.Port = 587;
_mailServer.EnableSsl = true;
var eml = new MailMessage();
eml.Sender = new MailAddress("my email");
eml.From = eml.Sender;
eml.To.Add(new MailAddress(to));
eml.Subject = subject;
eml.IsBodyHtml = (bodyType == BodyType.HTML);
eml.Body = body;
_mailServer.Send(eml);
I don't know for certain but I think that replacing the Host value with the smtp Office 365 link rather than an outlook one, as well as remembering to add a Target Name which I did not have before, both did the trick and solved the authorization issue (I had previously confirmed it wasn't a credentials issue with our tech support).

Error when sending an email c#

I try to send an email (with gmail) in C#. I check the solutions I can find on google but it never works for me, I always have the error :
SMTP server requires a secure connection or the client was not
authenticated. The response from the server Was: 5.5.1 Authentication
Required.
This is an exampe of code :
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "*****#gmail.com";
string password = "*****";
string emailTo = "******#gmail.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
The MSDN says that : Some SMTP servers require that the client be
authenticated before the server sends e-mail on its behalf. Set this
property to true when this SmtpClient object should, if requested by
the server, authenticate using the default credentials of the
currently logged on user. For client applications, this is the desired
behavior in most scenarios.
So you need to set smtp.UseDefaultCredentials = false; before assigning Credentials, So your code will looks like the following:
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
I find the solution which works for me. The solution proposed by un-lucky
was good but I need to configure gmail account.
So the code is :
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "****#gmail.com";
string password = "*****";
string emailTo = "****#gmail.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Moreover in your gmail account, you have to make this changes.
gmail account changes

Sending email using gmail smtp server

I have been trying for a long time to send a mail from a Gmail account to a gmail account using the below code.
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
if (fuAttachment.HasFile)
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
After the execution reaches "smtp.Send(mm)" the browser says waiting and after 2 minutes I get the exception saying "Failure Sending Email"
And the following Error message
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 ...:587" (some IP)
I have searched a lot for this but haven't found a solution. Please help me solve this issue.
Thank you.
Try This One.
public static string SendMail(string stHtmlBody, string stSubject, string stToEmailAddresses)
{
string stReturnText = string.Empty;
try
{
if (!string.IsNullOrEmpty(stToEmailAddresses))
{
//Set SmtpClient to send Email
string stFromUserName = "fromusername";
string stFromPassword ="frompassword";
int inPort = Convert.ToInt32(587);
string stHost = "smtp.gmail.com";
bool btIsSSL =true;
MailAddress to = new MailAddress(stToEmailAddresses);
MailAddress from = new MailAddress("\"" + "Title" + "\" " + stFromUserName);
MailMessage objEmail = new MailMessage(from, to);
objEmail.Subject = stSubject;
objEmail.Body = stHtmlBody.ToString();
objEmail.IsBodyHtml = true;
objEmail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
System.Net.NetworkCredential auth = new System.Net.NetworkCredential(stFromUserName, stFromPassword);
client.Host = stHost;
client.Port = inPort;
client.UseDefaultCredentials = false;
client.Credentials = auth;
client.EnableSsl = btIsSSL;
client.Send(objEmail);
return stReturnText;
}
}
catch (Exception ex)
{
}
return stReturnText;
}
First of all, I think you should use
UseDefaultCredentials = false;
And
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
You also need to allow less secure apps to access your account
I had a similar attempt but using Java. I was not successful doing that after a lot of search. I then used the Yahoo! SMTP, which worked very easily. Maybe you can try that.

Confused about sending email in ASP.NET MVC

Hello all can anyone please explain what would the UserName and Password be? I believe that those are asking for my gmail's username and password right? (I'm the one who will always receive emails) I am trying to implement the email service for my contact form. Sorry I'm pretty new to asp.net mvc.
public class MailService : IMailService
{
public string SendMail(string from, string to, string subject, string body)
{
var errorMessage = "";
try
{
var msg = new MailMessage(from, to, subject, body);
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "myemail#yahoo.com",
Password = "password"
};
smtp.Credentials = credential;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(msg);
}
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
return errorMessage;
}
}
Yes. The Username and Password are the ones required by your host. In this case the credentials you need to login at smtp.gmail.com
Yes, you need to authenticate using the login information just as if you were setting up an email client to send emails.
SmtpClient.Credentials Property
Gets or sets the credentials used to authenticate the sender.
....
Some SMTP servers require that the client be authenticated before the server will send e-mail on its behalf.
Yes, it's the gmail username and password.
Remember to create a application specific password and use that instead of the actual gmail password
Found the following code in one of my projects:
static Task sendMail(IdentityMessage message)
{
string text = string.Format("Please confirm your account by pasting this link on your browser\'s address bar:\n\n{0}", message.Body);
string html = "Please confirm your account by clicking this link<br/>";
MailMessage msg = new MailMessage();
msg.From = new MailAddress("id#gmail.com");
msg.To.Add(new MailAddress(message.Destination));
msg.Subject = message.Subject;
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("id#gmail.com", "gmailpassword");
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
return smtpClient.SendMailAsync(msg);
}

Not able to send mail from localhost through yahoo,gmail smtp server

What is wrong in below mentioned asp.net code while sending test mail from my localhost web application?
Error: The SMTP server requires a secure connection or the client was
not authenticated. The server response was: 5.7.1 Authentication
required
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "abcdefg#gmail.com";
string password = "12345";
string emailTo = "zyxw#gmail.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
The server requires an authentication. Yahoo doesn't just send emails for anybody. I don't think you can send an email through their gateway using a Google account.

Categories

Resources