I am extending my Web App with simple CMD "Service", which should send verification email on newly registered user. My problem is by authenticating the Gmail account, where following exception is thrown:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."
I have tried authenticate on my own IMAP server, but it did not worked out too. Later I tried to use XAMP Mercury email server, which is not the best solution, due to depending entirely on local configuration, so I abandoned that idea. In future, i want to create a new google account for the app only, so no maintenance is required.
String body = "<head>" +
"Here comes some logo" +
"</head>" +
"<body>" +
"<h1>Account confirmation reqest.</h1>" + Environment.NewLine +
"<a>Dear User, </a>" + Environment.NewLine +
"<a>In order to be able to use musicshop app properly, we require You to confirm Your email address.</a>" + Environment.NewLine +
"<a>This is the last step towards using our app.</a>" + Environment.NewLine +
"<a>Pleas follow this hyperlink to confirm your address.</a>" + Environment.NewLine +
"<a>[Callback url]</a>" +
"</body>";
try
{
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential()
{
UserName = "myemail@gmail.com",
Password = "mypassword"
};
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Send("targetemail@targetdomain.xyz", "myemail@gmail.com", "Account verification", body);
}
catch (Exception ex)
{
}
I just want to be able to send an email via Gmail server, without any exceptions. Do I need any NuGet packages for that, use different approach?
If your Gmail account has 2-Step Verification enabled you will have to create an App-Specific Password to authenticate with instead.
Note also that SmtpClient is IDisposable
- you should be putting it in a using (var smtpClient = new SmtpClient("smtp.gmail.com", 587)) { ... }
block so that the SMTP connection RSETs, QUITs and closes correctly.
== edit ==
Also, it appears you have the from
and recipients
parameters switched around on smtpClient.Send
.
string body = "<head>" +
"Here comes some logo" +
"</head>" +
"<body>" +
"<h1>Account confirmation reqest.</h1>" + Environment.NewLine +
"<a>Dear User, </a>" + Environment.NewLine +
"<a>In order to be able to use musicshop app properly, we require You to confirm Your email address.</a>" + Environment.NewLine +
"<a>This is the last step towards using our app.</a>" + Environment.NewLine +
"<a>Pleas follow this hyperlink to confirm your address.</a>" + Environment.NewLine +
"<a>[Callback url]</a>" +
"</body>";
try
{
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential()
{
UserName = Config.Username,
Password = Config.Password,
};
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
//Oops: from/recipients switched around here...
//smtpClient.Send("targetemail@targetdomain.xyz", "myemail@gmail.com", "Account verification", body);
smtpClient.Send("myemail@gmail.com", "targetemail@targetdomain.xyz", "Account verification", body);
}
}
catch (Exception e)
{
Console.Error.WriteLine("{0}: {1}", e.ToString(), e.Message);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.