How can I send emails from my SMTP server? - c#

Hello I have a hosted Windows Server 2008 R2 Enterprise and a purchased domain name. I have pointed everything on my domain to my server's dns and uploaded my ASP.Net site to the IIS. I have also set up an SMTP Feature so now I'm trying to send emails from my SMTP but I can't because the application says that the user is not authenticated.
Here is the source code of my page:
MailMessage activationMail = new MailMessage();
activationMail.From = new MailAddress("no-reply#mydomain.com", "MyGame");
activationMail.To.Add(RegistrationEmail.Text);
StreamReader sRead = new StreamReader(Server.MapPath("~/Mails/ActivationMail.html"));
string readFile = sRead.ReadToEnd();
string Strcontent = "";
Strcontent = readFile;
Strcontent = Strcontent.Replace("[Name]", RegistrationRealName.Text);
Strcontent = Strcontent.Replace("[Username]", RegistrationUsername.Text);
Strcontent = Strcontent.Replace("[Password]", RegistrationPasswordCreate.Text);
Strcontent = Strcontent.Replace("[useractivation]", useractivation);
Strcontent = Strcontent.Replace("[Site]", site);
Strcontent = Strcontent.Replace("[Facebook]", "facebook");
activationMail.Subject = "My Game - Registration";
activationMail.Body = Strcontent.ToString();
sRead.Close();
activationMail.IsBodyHtml = true;
activationMail.BodyEncoding = UTF8Encoding.UTF8;
SmtpClient client = new SmtpClient();
client.Host = "mydomain";
NetworkCredential cr = new NetworkCredential();
cr.UserName = "User from AD DS";
cr.Password = "Password of User";
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = 25;
client.Timeout = 10000;
client.Send(activationMail);
Note: My domain name is not the same with my forest's domain. I have also tried to set my forest's domain as the client.Host but same result. I have these settings on my Web.config
<mailSettings>
<smtp from="no-reply#mydomain">
<network defaultCredentials="false" host="localhost" />
</smtp>
</mailSettings>
My SMTP server's settings are these:
IP Address = My server's IP
Authentication = Standar & Windows authentication are checked, TSL is checked, Default domain is set to my domain (not my MX record mail.domain). Anonymous authentication is not checked.
Connection = Only my server's IP has access
Relay = Only my purchased domain (not MX record) has access
Outbound security = Standar authentication (User from AD DS - same credentials with the ones at my cs file above), TLS enabled
Advanced delivery options = I set my domain name there (not MX record - gives me an error)
Does anybody know what am I doing wrong?

Add the following code as you haven't provided any credentials to the smtp:
client.Credentials = cr;
Like:
NetworkCredential cr = new NetworkCredential();
cr.UserName = "User from AD DS";
cr.Password = "Password of User";
client.Credentials = cr;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = 25;
client.Timeout = 10000;
client.Send(activationMail);

try adding these two line:
client.EnableSsl = false;
client.Credentials = cr;

Related

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. C#

I've tried with Gmail (tried activating less secure apps, 2 steps verification and apps password, zone time, etc..).
Also, I tried sending emails with outlook and I'm having the same error.
In the console, I get the followed error: POST 500
Notes:
I don't have access to the Server.
This is working on my Local.
The website uses Plesk.
The Gmail code was working before, but after changing the password is not working (we activated less security apps).
In Gmail code, I've tried different ports.
Code:
MailMessage message = new MailMessage();
message.From = new MailAddress(model.EmailAddress);
message.To.Add("email#gmail.com");
message.Subject = string.Format("Mensaje de {0} ({1}) - {2}", model.FirstName, model.EmailAddress, model.Subject);
message.Body = model.Message;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("email#gmail.com", "password");
client.Send(message);
/*
MailMessage mc = new MailMessage(System.Configuration.ConfigurationManager.AppSettings["Email"].ToString(), "email#hotmail.com");
mc.Subject = string.Format("Mensaje de {0} ({1}) - {2}", model.FirstName, model.EmailAddress, model.Subject);
mc.Body = model.Message;
mc.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.office365.com", 587);
smtp.Timeout = 1000000;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential nc = new NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["Email"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Password"].ToString());
smtp.UseDefaultCredentials = false;
smtp.Credentials = nc;
smtp.Send(mc);
*/

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).

The server response was: 5.7.3 Requested action aborted; user not authenticated

I want to send emails using C# for an MVC project and hotmail, I have tried port 25 and 587, using my own credentials, but every time I get '5.7.3 user not authenticated', if I change SSL to false, then I get '5.7.0, Must issue a STARTTLS command first'.
There are 2 other questions with the same problem but one of the answers is old (points me to a setting that doesn't exist in outlook.com) and the other one didn't solve it (changing ssl to false).
This is my code (of course I removed my email and password), question is, what else can I do to get ride of the 5.7.3, am I missing something?
MailMessage mail = new MailMessage(<email>, <email>);
SmtpClient client = new SmtpClient("smtp-mail.outlook.com");
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(<email>, <pass>);
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.EnableSsl = true;
client.Send(mail);
This code Works normally on my computer!
There are two issues that you should look at:
If you enabled the use of external applications in hotmail
Check the https://account.microsoft.com/account link if your computer
is locked.
My code:
MailMessage mail = new MailMessage("from", "to");
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Host = "smtp-mail.outlook.com";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("email", "password");
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
mail.Subject = "test";
mail.Body = "test";
client.Send(mail);

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

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