Reputation:
I developed an ASP.net application that can send an email to any domain. I'm using a simple .Net smtp client:
var fromAddress = new MailAddress("[email protected]");
var fromPassword = "xxxxxx";
var toAddress = new MailAddress("[email protected]");
var smtpClient = new System.Net.Mail.SmtpClient
{
Host = "smtp.gmail.com",// or any others
Port = 587, // correspond to host
EnableSsl = true,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
smtpClient.Send(message)
. But I cannot authenticate to any modern mail providers(including gmail, yahoo, yandex) by using .Net.Mail because I have got the following exception The server response was: 5.5.1 Authentication Required
All of the smtp configuration fill-out correctly.
How can I use DotNetOpenAuth for authentication and .Net.Smtp for sending emails? Please give me an example.
Upvotes: 4
Views: 219
Reputation: 4928
OK, I know I am adding a lot of answers, but here are my findings after looking at this for several hours:
The error is the client is not authenticated (5.5.1)
I got the same error, then I noticed an email in my inbox (the gmail account is seldom used) saying Someone tried to sign in to your Google Account [email protected] from an app that doesn't meet modern securty standards.
Is you app blocked by google: http://security.google.com/settings/security/activity might show your app, but are you getting an email?
Also, if the account uses 2 step authentication, the email will not be sent without setting an application specific password: https://support.google.com/accounts/answer/185833
I think this is account configuration instead of a problem with your code.
Upvotes: 0
Reputation: 4928
From the comments, I can see the image, showing what you are trying to do:
However, you cannot know what the configurations are going to be and users will select configurations that are not correct. What will you do if the user selects TLS, but there is no TLS support from the mail server?
I think you need to add a Send Test Email button, to send a test email to check the configuration. You can then show any errors that are encountered.
Upvotes: 0
Reputation: 4928
This code works for me, once the email and credentials are updated:
try
{
var message = new MailMessage();
message.Subject = "Test email";
message.Body = "This is a test message";
message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress("[email protected]"));
var client = new SmtpClient
{
Host = "smtp.sendgrid.com",
Port = 587,
Credentials = new NetworkCredential("User_Name", "My_Password")
};
client.Send(message);
}
catch (Exception ex)
{
var msg = ex.Message;
}
Make sure your SendGrid credentials are correct.
Upvotes: 0
Reputation: 4928
I'd consider your SMTP server. Is it allowing you to send email? Is your request making it to the server and being validated? Have you looked at the traffic using Fiddler?
You can sign up for a free account with SendGrid, who will provide you with a SMTP server to send the email for you. You really are better of using a company like SendGrid instead of the gmail SMTP server.
Other than that, Mail4Net might help you in your code.
To use GMail as a SMTP Server, you have to set some values:
Outgoing Mail (SMTP) Server: smtp.gmail.com
Use Authentication: Yes
Use Secure Connection: Yes (this can be TLS or SSL depending on your mail client)
Username: your GMail account, i.e. [email protected]
Password: your GMail password
Port: 465 or 587
You can also move your connection to the web or app config file:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="network" from="[email protected]">
<network
host="smtp.gmail.com"
port="465"
enableSsl="true"
userName="[email protected]"
password="password"
/>
</smtp>
</mailSettings>
</system.net>
</configuration>
Upvotes: 4