I have simple console app and trying to send mail via SMTP class in C# but no mail is sent, app is closed and no exception is thrown as well. Problem line is client.Send(msg).
using (var client = new SmtpClient()) {
client.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = True;
client.Timeout = 20000;
client.Credentials = new NetworkCredential(@ConfigurationManager.AppSettings["senderEmailAddress"].ToString(),@ConfigurationManager.AppSettings["senderEmailPassword"].ToString());
client.Host = "smtp.gmail.com;
using (var msg = new MailMessage()) {
msg.From = new MailAddress(@ConfigurationManager.AppSettings["senderEmailAddress"].ToString());
msg.Sender = new MailAddress(@ConfigurationManager.AppSettings["senderEmailAddress"].ToString());
msg.Subject = fileName;
msg.Attachments.Add(new Attachment(OutputFilePath));
msg.To.Add(EmailAddress);
client.Send(msg); }}
private void smtp_SendCompleted(object sender, AsyncCompletedEventArgs e) {
if (e.Cancelled == true || e.Error != null) {
throw new Exception(e.Cancelled ? "EMail sedning was canceled." : "Error: " + e.Error.ToString());
}
}
Is possible at least to get anyhow exception?