I'm creating app and it return error. This is part of configuration SMTP connection in web.config file:
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com"
port="587"
userName="mymail@gmail.com"
password="mypassword"
enableSsl="true"
defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
And below is code in Helpers controller:
namespace Subscription.Helpers
{
public static class MailHelper
{
private static SmtpClient _smtpClient;
static MailHelper()
{
_smtpClient = new SmtpClient();
}
public static void SendEmail(List<string> recipientAddress, string subject, string news)
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("mymail@gmail.com", "Google.com", Encoding.UTF8);
foreach(string address in recipientAddress)
{
mail.Bcc.Add(new MailAddress(address));
}
mail.Subject = subject;
mail.Body = news;
mail.SubjectEncoding = Encoding.UTF8;
mail.BodyEncoding = Encoding.UTF8;
mail.Priority = MailPriority.High;
_smtpClient.Send(mail);
}
}
}
}
When I want to send email to all addresses from database it return unhandled exception with communicate: "The SMTP host was not specified" and line "_smtpClient.Send(mail);" in source error. What I'm doing wrong?
First add descriptions to your tag like this:
<smtp deliveryMethod="Network" from="username@gmail.com">
Then in your code behind add smtp.EnableSsl = true; and remove it from you web.config.
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.