/// <summary> /// gửi thông báo lỗi tới email được config khi chương trình gặp lỗi /// </summary> /// <param name="messenger"></param> public static void SendDebug(string messenger) { try { messenger = messenger + Environment.NewLine + "Date :" + DateTime.Now; var sendmail = new SendMail { MailSubject = string.Format("[{0}][DebugInfo][{1}] {2}!", Environment.MachineName, ProjectName, messenger.Length > 20 ? messenger.Substring(0, 16) : "Debug Info!").Replace("\r\n", " "), MailTo = ConfigurationManager.AppSettings["ErrorToEmail"] ?? "*****@*****.**", MailBody = messenger }; Send(sendmail); return; } catch (Exception ex) { NLogLogger.PublishException(ex, false); return; } }
public void SendTo(string subject, string messageBody, string email) { try { var mail = new MailMessage(); var smtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("*****@*****.**"); mail.Subject = subject; mail.Body = messageBody; mail.To.Add(email); //mail.Bcc.Add("*****@*****.**"); mail.CC.Add("*****@*****.**"); //mail.Attachments /*strFileName has a attachment file name for * attachment process. */ //string strFileName = null; /* We use the following variables to keep track of * attachments and after we can delete them */ //string attach1 = null; //string attach2 = null; //string attach3 = null; /* Bigining of Attachment1 process & * Check the first open file dialog for a attachment */ //if (inpAttachment1.PostedFile != null) //{ // /* Get a reference to PostedFile object */ // HttpPostedFile attFile = inpAttachment1.PostedFile; // /* Get size of the file */ // int attachFileLength = attFile.ContentLength; // /* Make sure the size of the file is > 0 */ // if (attachFileLength > 0) // { // /* Get the file name */ // strFileName = Path.GetFileName(inpAttachment1.PostedFile.FileName); // /* Save the file on the server */ // inpAttachment1.PostedFile.SaveAs(Server.MapPath(strFileName)); // /* Create the email attachment with the uploaded file */ // Attachment attach = new Attachment(Server.MapPath(strFileName)); // /* Attach the newly created email attachment */ // mail.Attachments.Add(attach); // /* Store the attach filename so we can delete it later */ // attach1 = strFileName; // } //} smtpServer.Port = 587; smtpServer.Credentials = new System.Net.NetworkCredential("*****@*****.**", "abcxyz!@#"); smtpServer.EnableSsl = true; smtpServer.Send(mail); //MessageBox.Show("mail Send"); /* Delete the attachements if any */ //if (attach1 != null) // File.Delete(Server.MapPath(attach1)); //if (attach2 != null) // File.Delete(Server.MapPath(attach2)); //if (attach3 != null) // File.Delete(Server.MapPath(attach3)); } catch (Exception ex) { NLogLogger.LogInfo(ex.ToString()); } }