I am using the following code to try to send an email asynchronously, but no email is sent and I am not sure what is being done incorrectly. I have also added the 2nd segme
Joe's solution guided me a lot, thanks for that! Yet to contribute with it, you must include the following namespaces:
using System.Configuration;
using System.Net.Mail;
I´ve changed his solution a little, after a lot of trying i've reached some code that worked (it must still be refacored, but it doesn't change the idea), this is how my SendAsync method looks like:
public Task SendAsync(IdentityMessage message) {
//SmtpClient client = new SmtpClient();
//return client.SendMailAsync(ConfigurationManager.AppSettings["SupportEmailAddr"],
// message.Destination,
// message.Subject,
// message.Body);
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
//client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("mailName@gmail.com", "mailPassword");
return client.SendMailAsync("mailName@gmail.com", message.Destination, message.Subject, message.Body);
}
You can see Joe's solution commented in top, then, a lot of configurations are made to the SmtpClient (the timeout is commented due to some testing i've been making, you can uncomment it if it suits your needs).
After that, the mail is sent in an asyncronous way, notice that the sender (which Joe get's from an AppSettings variable) is the same specified in the credentials (you must create a gmail [or wathever mail service you want] account and use it's name and password to create the credentials).
That should do the trick! Bear in mind that gmail may complicate your life while trying to connect to your new mail account this way, to solve it, you must log in that account and go to your account configurations and activate "less secure applications acces" (or sth like that, i speak spanish so my traduction may not be that good...).
Edit 22/04/16: Seems this solution doesn't work properly while working behing a proxy, there should be a way to configure it. In my case i find it cheaper to disable the proxy and go on, but for those of you who can't afford that, expect to have this obstacle while implementing this.