How can I make SMTP authenticated in C#
using System.Net;
using System.Net.Mail;
using(SmtpClient smtpClient = new SmtpClient())
{
var basicCredential = new NetworkCredential("username", "password");
using(MailMessage message = new MailMessage())
{
MailAddress fromAddress = new MailAddress("from@yourdomain.com");
smtpClient.Host = "mail.mydomain.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
// Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>your message body</h1>";
message.To.Add("to@anydomain.com");
try
{
smtpClient.Send(message);
}
catch(Exception ex)
{
//Error, could not send the message
Response.Write(ex.Message);
}
}
}
You may use the above code.
C# SMTP authentication failed but credentials are correct
This link saved my life: link
here the problem I experienced is well described: even though the user name is passed with the AUTH LOGIN the server responds with the AUTH_LOGIN_Username_Challenge again.
This problem happens only using System.Net.Mail. The link suggests tre possible solutions:
1)Use CDOSYS (Does not send the User Name with the AUTH Login)
2)Use System.Web.Mail (Does not send the User Name with the AUTH Login)
3)Contact the SMTP Server owner and have them fix the server.
Unfortunately I couldn't the SMTP server owner, so I had to use System.Web.Mail. I know it is deprecated but unfortunately in cases like this there is no other choice IMO.
That's my working code:
System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
msg.Body = message.Body;
string smtpServer = "mail.business.it";
string userName = "username";
string password = "password";
int cdoBasic = 1;
int cdoSendUsingPort = 2;
if (userName.Length > 0)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
}
msg.To = message.Destination;
msg.From = "me@domain.it";
msg.Subject = message.Subject;
msg.BodyFormat = MailFormat.Html;//System.Text.Encoding.UTF8;
SmtpMail.SmtpServer = smtpServer;
SmtpMail.Send(msg);
This answer helped me in using System.Web.Mail.
Smtp authentification required
You need to tell the SMTP client that you will not be using your windows credentials to access the SMTP, so add
smtpClient.UseDefaultCredentials = false;
above this line of code
smtpClient.Credentials = new NetworkCredential("myemail@gmail.com", "password");
Also, gmail doesn't allow impersonation, so
mailMessage.From = new MailAddress("support@team.com");
will have no effect - the emails will still appear to be sent from the email account you are accessing.
Also, make sure that the setting "allow less-secure applications" is set on your gmail account, and that 2-factor authentication is not enabled.
How to set username and password for SmtpClient object in .NET?
The SmtpClient can be used by code:
SmtpClient mailer = new SmtpClient();
mailer.Host = "mail.youroutgoingsmtpserver.com";
mailer.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
FluentEmail smtp with authentication - how to configure?
FluentEmail is just facade of integrated System.Net.Mail library. Function AddSmtpServer() has overloading including the possibility to pass standard SmtpClient. You can simply construct original SmtpClient and than pass it to the FluentEmail.
var client = new SmtpClient();
client.Credentials = new NetworkCredential("my_user", "my_password");
client.Host = "my_server";
client.Port = 25;
services
.AddFluentEmail("defaultsender@test.test")
.AddSmtpSender(client);
The SmtpClient class is marked as obsolete in .Net Core 2.2 and Microsoft recommends another solution ... See below if this may be more fitting.
"SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead"
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required?
First check for gmail's security related issues. You may have enabled double authentication in gmail. Also check your gmail inbox if you are getting any security alerts. In such cases check other answer of @mjb as below
Below is the very general thing that i always check first for such issues
client.UseDefaultCredentials = true;
set it to false.
Note @Joe King's answer - you must set client.UseDefaultCredentials before you set client.Credentials
Sending Authenticated SMTP Using C#
According to the following article: "Hacking" System.Net.Mail.SmtpClient, the exception could be thrown when the SmtpClient chooses to authenticate using NTLM authentication.
The article provides a fix, although as the article name suggests it is a hack.
However in the comments a poster also notes that an incorrect password can cause the same exception.
Related Topics
Programmatically Mouse Click in Another Window
ASP.NET Core 2.0-2.2 Kestrel Not Serving Static Content
Operators as Method Parameters in C#
Class with Single Method -- Best Approach
How to Access Winform Textbox Control from Another Class
5.7.57 Smtp - Client Was Not Authenticated to Send Anonymous Mail During Mail from Error
Escape Curly Brace '{' in String.Format
How to Read an Excel File in C# Without Using Microsoft.Office.Interop.Excel Libraries
How to Determine If a Decimal/Double Is an Integer
Applying CSS on ASP.NET Fileupload Control's Browse Button Only
How to Use Java-Style Throws Keyword in C#
Datacontract Xml Serialization and Xml Attributes
Simple Injector Unable to Inject Dependencies in Web API Controllers
Webclient.Downloadstring() Returns String with Peculiar Characters