Reputation: 273
Having some trouble sending email from my application..
I get the following Exception
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: Failure sending mail.
The exception pops up when running: smtpClient.Send(mailMessage);
Function that should send mail:
public static void SendEmail(string toAddress, string subject, string body, bool isBodyHtml = true)
{
var mailMessage = new MailMessage();
mailMessage.To.Add(toAddress);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHtml;
var smtpClient = new SmtpClient { EnableSsl = false };
smtpClient.Send(mailMessage);
}
WebConfig configuration
<system.net>
<mailSettings>
<!-- Method#1: Send emails over the network -->
<smtp deliveryMethod="Network" from="[email protected]">
<network host="gmail.com" userName="myusername" password="mypw" port="465" />
</smtp>
</mailSettings>
</system.net>
Any help appreciated
Upvotes: 2
Views: 1065
Reputation: 1460
This is how im sending gmail:
static void SendMail(string sSubject, string sBody)
{
const string senderID = "[email protected]"; // use sender's email id here..
const string toAddress = "[email protected]";
const string senderPassword = "passwords are fun"; // sender password here...
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com", // smtp server address here...
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
Timeout = 30000,
};
MailMessage message = new MailMessage(senderID, toAddress, sSubject, sBody);
smtp.Send(message);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
LogThis("Error sending mail:" + ex.Message);
Console.ResetColor();
}
}
Upvotes: 3