In the first 8 minutes of this tutorial, the developer tells you to uncomment the code in the forgotpassword() in the account controller, the email confirmation in the if, and finally the link to get the forgot password page on the login screen. He then tells you to write in the following code in your Identity config.
using System.Net.Mail;
public Task SendAsync(IdentityMessage message)
{
//Emails will be sent from this address
var from = "someusername#gmail.com";
var pass = "somepassword";
//setting up SMTP client
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(from, pass);
client.EnableSsl = false;
//Create Email
var mail = new MailMessage(from, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
mail.IsBodyHtml = true;
//Send email
return client.SendMailAsync(mail);
// Plug in your email service here to send an email.
return Task.FromResult(0);
}
My project builds as it does in the tutorial however, he is able to receive an email in the tutorial but I do not. I double and triple checked my username and password. What did I miss?
Related
I am trying to use the code snippet below. The goal is to just send an email, however I am getting an error "The SMTP server requires a secure connection or the client was not authenticated.".
My question is. What is the best way to send emails via code if an SMPT server requires an authenticated account? Is it bad practice to have a developer type account created for this purpose?
string server = "";
string to = "";
string from = "";
MailMessage message = new MailMessage(from, to);
message. Subject = "Subject";
message. Body = "Body";
SmtpClient client = new SmtpClient(server);
client.UseDefaultCredentials = true;
client. Port = 000;
client.EnableSsl = true;
This is how I send email using System.Net.Mail through office365 smtp.
Yes, you need to have an account and password with office365 to be able to send email through their smtp server. Use the code below, it is the same one I use in my web app to send email to users.
using (var message = new MailMessage())
{
message.To.Add(new MailAddress("recepient email", "receipient name"));
message.From = new MailAddress("your email", "your name");
message.Subject = "My subject";
message.Body = "My message";
message.IsBodyHtml = false; // change to true if body msg is in html
using (var client = new SmtpClient("smtp.office365.com"))
{
client.UseDefaultCredentials = false;
client.Port = 587;
client.Credentials = new NetworkCredential("your email", "your password");
client.EnableSsl = true;
try
{
await client.SendMailAsync(message); // Email sent
}
catch (Exception e)
{
// Email not sent, log exception
}
}
}
I am trying to send email using office365 e-mail account that has enabled the two-factor authentication. It gives an authentication failed error. For email accounts that have not enabled the two-factor authentication works fine. How to resolve this issue?
using (SmtpClient client = new SmtpClient())
{
client.Port = Convert.ToInt32(appSettings["Port"]);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "smtp.office365.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(SenderMailAddress, SenderMailPassword);
email.Subject = String.Format("{0}", txtMailSubject.Text);
//
email.Body = String.Format("{0}", text);
email.IsBodyHtml = true;
client.Send(email);
}
Error message is
System.Net.Mail.SmtpException: The SMTP server requires a secure
connection or the client was not authenticated. The server response
was: 5.7.1 Client was not authenticated at
System.Net.Mail.MailCommand.CheckResponse
You have to create an application password, then use the new application password in your code, check this link
MailAddress from = new MailAddress("fromid");
MailAddress to = new MailAddress("toID");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = #"The sensor get offline ...";
SmtpClient client = new SmtpClient(server);
client.Host = "hostID";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential
{
UserName = "**********",
Password = "*************,
};
Console.WriteLine("Sending an email message to {0} using the SMTPhost {1}.");
I use this code to sentmailasync:
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
//// Plug in your email service here to send an email.
//return Task.FromResult(0);
//Login password of sender
var from = ConfigurationManager.AppSettings["email"];
var pass = ConfigurationManager.AppSettings["e_password"];
//set address and port smtp-server from which main is sent
SmtpClient client = new SmtpClient("smtp-mail.outlook.com");
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(from, pass);
client.EnableSsl = true;
//creating massage and destination address
var mail = new MailMessage(from, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
mail.IsBodyHtml = true;
return client.SendMailAsync(mail);
}
}
I published code in two productions in one machine it works fine.
In other machine I get this error:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 40.100.172.242:587
Any idea why I get error above and how to fix it?
Im working on an web application i got stuck between this email sending code.I am working on contact page where anyone can send you email as you guys know that.This code in working fine but i can't pass through this error
as i said im working with Asp.Net Mvc so here is my POST controller im using gmail account so that it won't conflict between any mail services.
public ActionResult sendemail()
{
return View();
}
[HttpPost]
public ActionResult sendemail(string to, string from, string subject, string body, string pwd)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("faseehyasin12#gmail.com", pwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = subject;
mail.Body = body;
try
{
client.Send(mail);
Response.Write("ok");
return View();
}
catch(Exception e)
{
throw e;
}
}
and here is my view and i want to ask do i really need a password to send email to someone in this code ?
and my GET controller is empty with only written code is return view() so i won't bother to take ss for that. i have also allowed "less secure app" but it still gives me this error. Need Help
First go to https://myaccount.google.com/lesssecureapps and change status as open.
And go to https://accounts.google.com/b/0/displayunlockcaptcha and click Continue button.
Please try with following code.
string host = "smtp.gmail.com";
int port = 587;
bool ssl = true;
string fromAddress = "faseehyasin12#gmail.com";
string fromPassword = "your password here";
using (var mail = new MailMessage())
{
string subject = "Test";
string body = "Mail test";
mail.From = new MailAddress(fromAddress);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
mail.To.Add("mail#domain.com");
using (var smtpServer = new SmtpClient(host,port))
{
smtpServer.UseDefaultCredentials = false;
smtpServer.Credentials = new System.Net.NetworkCredential(fromAddress, fromPassword);
smtpServer.EnableSsl = ssl;
smtpServer.Send(mail);
}
}
I have tried sending mail from gmail and other share hosting mail server and its works as expected
But Now my requirement is sending mail from windows mail server.whenever i try to send mail it shows gives me error failure sending mail.
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "us2.webmail.mailhostbox.com";
string fromaddress = "info#csi.com";
client.Port = 25;
//client.Host = "mail.csisite.com";
// setup Smtp authentication
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("info#csisite.com", "password");
//client.UseDefaultCredentials = true;
client.Credentials = credentials;
client.EnableSsl = false;
try
{
//mail For Customer
MailMessage msgcustomer = new MailMessage();
msgcustomer.From = new MailAddress(fromaddress);
msgcustomer.To.Add(new MailAddress(EmailID));
msgcustomer.Subject = "Thank you for writing to us" ;
msgcustomer.IsBodyHtml = true;
msgcustomer.Body = "Test Mail";
client.Send(msgcustomer)
Send Mail:
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
using System.Net.Mail
MailMessage msgMail = new MailMessage();
MailMessage myMessage = new MailMessage();
myMessage.From = new MailAddress("sender's email","sender`s name and surname");
myMessage.To.Add("recipient's email");
myMessage.Subject = "Subject";
myMessage.IsBodyHtml = true;
myMessage.Body = "Message Body";
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("email", "password");
mySmtpClient.Host = "your smtp host address";
mySmtpClient.UseDefaultCredentials = false;
mySmtpClient.Credentials = myCredential;
mySmtpClient.ServicePoint.MaxIdleTime = 1;
mySmtpClient.Send(myMessage);
myMessage.Dispose();
updated user is getting the following error
{"A socket operation was attempted to an unreachable network 208.91.199.230:25"} and Error Code is 10051
The Error 10051 is a socket error that appears when the target network or host server can not be reached. The error could have occurred due to an erroneous configuration of the router, Internet disconnection, or a blocking action by a firewall. This error has occurred mostly in attempts to generate Internet Control Message Protocol (ICMP) and Simple Mail Transfer Protocol (SMTP) connections with wrong configurations. This error also indicates that the Windows software employed is configured to communicate to a router, since the 10065 error appears instead if Windows is not set to link to a router.
you should make ensure that the network path is correct, that the computer is not running two software firewalls and that the targeted computer is not turned off.
Use This Code for sending mail this is tested code.
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mMailMessage.To.Add(new MailAddress(to));
// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
} // Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
} // Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
mSmtpClient.Credentials = new System.Net.NetworkCredential
("YourEmail#gmail.com", "Password");
//Or your Smtp Email ID and Password
mSmtpClient.EnableSsl = true;
mSmtpClient.Send(mMailMessage);
}
Note: I have been trying to send email through my ISP mail server with no success - following all the suggestions that I could find on these forums. I just discovered that it would work fine if I did not attempt to alter the default credentials setting. If I included either: client.UseDefaultCredentials = true; or client.UseDefaultCredentials = false; in my code, then I would get a login failure message from the server. By not including either, it worked fine.