I am sending emails to our clients from Java. And there is no any authentication for our SMTP. So I use the following code in Java to send it without authentication:
Properties props = new Properties();
Session session;
props.put("mail.smtp.auth", "false");
session = Session.getInstance(props, null);
This code works fine for sending emails from Java. But I want to send emails using ASP.NET and C#. But I am unable to send it. For sending it using C# I am using the following code:
SmtpClient smtp = new SmtpClient();
smtp.Host = "<My smtp.Host>";
smtp.EnableSsl = false;
smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Send(message);
But it gives me the following error:
The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.7.1 Relaying not allowed: <Here email address of To>
How to send it without authentication?
From Msdn. If the UseDefaultCredentials property is set to false and the Credentials property has not been set, then mail is sent to the server anonymously.
Related
I was new to SMTP client in C#. So I decided to test it with my credentials. I built an ASP.NET Web forms application that has a Contact Us page on which I am trying to send an email to whoever person fills the form.
I tried sample code after going through this article - https://www.c-sharpcorner.com/UploadFile/87b416/sending-a-simple-email-using-smtpclient-in-C-Sharp/
I have one account in Yahoo so I used its SMTP domain "smtp.mail.yahoo.com" with port number: 465, then my app always threw Timeout exception. So I decided to try with Google's server "smtp.gmail.com" with "587" port and now it raised different exception with message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
I don't understand what are the prerequisites for working with SMTP on secure servers like Google and Yahoo. Please someone explain.
Also note that I didn't have 2 step verification enabled for my Google account, just to make it clear since some questions on SO have mentioned that this might be the problem.
I also read this question but I am testing directly on my machine - Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
In case if it helps, here is the sample code:
try
{
MailMessage m = new MailMessage();
m.From = new MailAddress("dummy123#gmail.com");
m.To.Add(new MailAddress("dummyreceiver123#gmail.com"));
m.Subject = TBSub.Text;
m.Body = TBBody.Text;
m.IsBodyHtml = true;
NetworkCredential nc = new NetworkCredential();
nc.UserName = "dummy123"
nc.Password = "dummy#123";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = nc;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(m);
}
catch (Exception ex)
{
//Log this error
}
I just tested out your code it works fine you just need to modify this section:
nc.UserName = "test"
nc.Password = "password";
This has to be a valid gmail or google app email along with the password for the smtp connection to work properly. I would recommend that you put in your own for testing purposes, and then modify this to have your email as well:
m.From = new MailAddress("yourEmail#gmail.com");
m.To.Add(new MailAddress("yourEmail#gmail.com"));
Just so that you can validate that your message is being passed from your function.
I have read other answers on the stackoverflow. but none of the solutions work for me.
I'm trying to send email through live.com, but unable to it.
The error message:
mailbox unavailable. The server response was: 5.7.3 requested action aborted;
user not authenticated
The code:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("email#live.com");
mail.To.Add("someone#someone.com");
mail.Subject = "hello";
mail.Body = "awefkljj kawefl";
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.live.com", 587);
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("email#live.com", "password");
smtp.Send(mail);
Are you able to send the email by using above code?
It works before, last year, but it is no more working now.
What had been changed?
I ran into an issue where I was unable to send emails using the smtp.live.com SMTP server from certain hosts -- particulary Azure hosts. In my case, the SMTP attempt was from a host that I had never used to sign-in previously, so the attempt was blocked with the 5.7.3 error:
Mailbox unavailable. The server response was: 5.7.3 requested action aborted; user not authenticated
The solution was to browse to the account settings, locate the SMTP request in its recent activity, and select "This was me":
In my case I was using a gmail account but was telling the service otherwise.
I changed:
smtp.Host = "smtp.live.com";
To:
smtp.Host = "smtp.gmail.com";
Which resolved my issue.
This Solved My Problem:
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("myaccount#live.com","mypassword");
UseDefaultCredentialsproperty should set to false before setting Credentials
If you have this problem on 2017, and you think you are probaly using an office 365 account, make sure to change the host to "smtp.office365.com". It worked for me.
I'm working on an ASP.NET Web Forms app and I'm trying to programmatically send an email to myself. I'm using Gmail's SMTP client, and all is well except that when I send my message, I get this error:
"System.Net.Mail.SmtpException: 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"
If I go into my gmail account settings and enable an option that allows me to allow access for "less secure apps", everything works fine. I'm wondering how I can send my email with having this option enabled.
protected void sendEmail(object sender, EventArgs e)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new System.Net.NetworkCredential("myusername#gmail.com", "mypassword"),
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true
};
MailAddress from = new MailAddress("myusername#gmail.com", "Torchedmuffinz");
MailAddress to = new MailAddress("myusername#gmail.com", "Torchedmuffinz");
MailMessage message = new MailMessage(from, to);
message.Subject = "test";
message.Body = "test";
Attachment attachFile = new Attachment(#"pathtofile");
message.Attachments.Add(attachFile);
try { client.Send(message); }
catch (Exception email_exception)
{
System.Diagnostics.Debug.WriteLine(email_exception);
}
}
Gmail port 587 does not support SSL.
I think the following code should work for you.
MailMessage msg = new MailMessage();
msg.From=new MailAddress("yourmail#gmail.com");
msg.To.Add("receiver#receiverdomain.com");
msg.Subject="Your Subject";
msg.Body="Message content is going to be here";
msg.IsBodyHtml=false; //if you are going to send an html content, you have to make this true
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port=587;
NetworkCredential credential=new NetworkCredential("yourmail#gmail.com","your gmail password");
client.UseDefaultCredentials=false;
client.Credentials=credential;
client.Send(msg);
There is a possibility to use Google SMTP servers without 'Allow less secure apps' option, but you cannot use your standard google username and password. See my instructions on other post:
Is there a way to use ASP.NET to send email through a Google Apps acccount without selecting the 'Allow less secure apps' option?
I have read other answers on the stackoverflow. but none of the solutions work for me.
I'm trying to send email through live.com, but unable to it.
The error message:
mailbox unavailable. The server response was: 5.7.3 requested action aborted;
user not authenticated
or error message:
System.Net.Mail.SmtpException: Service not available,
closing transmission channel.
The server response was: Cannot connect to SMTP server 65.55.176.126
(65.55.176.126:587), NB connect error 1460
The code:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("email#live.com");
mail.To.Add("someone#someone.com");
mail.Subject = "hello";
mail.Body = "awefkljj kawefl";
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.live.com", 587);
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("email#live.com", "password");
smtp.Send(mail);
Are you able to send the email by using above code?
It works before, last year, but it is no more working now.
I'm not sure what has been changed to live.com email server.
What new settings or parameters should apply?
I ran into an issue where I was unable to send emails using the smtp.live.com SMTP server from certain hosts -- particulary Azure hosts. In my case, the SMTP attempt was from a host that I had never used to sign-in previously, so the attempt was blocked with the 5.7.3 error:
Mailbox unavailable. The server response was: 5.7.3 requested action aborted; user not authenticated
The solution was to browse to the account settings, locate the SMTP request in its recent activity, and select "This was me":
Tested and it works (different host address and a few other property set):
using (var client = new SmtpClient("smtp-mail.outlook.com")
{
Port = 587,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
EnableSsl = true,
Credentials = new NetworkCredential(_sender, _password)
})
{
using (var mail = new MailMessage(_sender, _recipient)
{
Subject = _subject,
Body = _message
})
{
client.Send(mail);
}
}
Also, if the account has two-step verification turned on, you'll have to generate an app password and use that instead.
Your code works for me without any changes with a live.com address. I am able to generate the same exception by simply putting an incorrect password.
I would suggest following checks:
Did the user change password recently? Are you able to login with the credentials provided over the web interface?
if yes, does your program uses the exact same credentials? please note that white space can be your enemy.
I'm getting the error "Server does not support secure connections" with my code below.
SmtpClient client = new SmtpClient(exchangeServer);
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(user, password);
MailAddress from = new MailAddress(fromAddress);
MailAddress to = new MailAddress(to);
MailMessage mail = new MailMessage(from, to);
// ...
client.Send(mail);
How can I fix this issue?
Your server does not support SSL on the default port; Most won't.
When you set SSL off, you get the message, "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated"
That tells you that you are not authenticated. Further, you said in a comment, "Because if I set UseDefaultCredentials = true and use my own user address in the "from" address, I am able to send an email successfully."
This is apparently an issue with how the SMTP server is configured. You will need to get appropriate credentials, or have the SMTP server set to allow mail to be sent from the web server.
What port are you using? You may find that you need to specify the port in your SmtpClient object.
write
client.EnableSsl = false;