public bool SendEmailWithAttachment(HtmlEmailStruct htmlEmailStruct) { try { if (!bSMTPInitialized) { throw new Exception("Error: SMTP Not initialized"); } MailMessage mail = new MailMessage(); mail.From = new MailAddress(htmlEmailStruct.SenderEmail); mail.To.Add(htmlEmailStruct.ReceiverEmail); mail.Subject = htmlEmailStruct.EmailTitle; mail.Body = htmlEmailStruct.EmailBody; if (!string.IsNullOrEmpty(htmlEmailStruct.AttachmentPath)) { var attachment = new System.Net.Mail.Attachment(htmlEmailStruct.AttachmentPath); mail.Attachments.Add(attachment); } emailClient.Send(mail); return(true); } catch (Exception ex) { _logger.LogItem("EmailService SendEmailWithAttachment() To - email send failed: " + ex.Message); return(false); } }
public bool SendMultiPartEmail(HtmlEmailStruct htmlEmailStruct) { try { if (!bSMTPInitialized) { throw new Exception("Error: SMTP Not initialized"); } // Initialize the message with the plain text body: MailMessage msg = new MailMessage(htmlEmailStruct.SenderEmail, htmlEmailStruct.ReceiverEmail, htmlEmailStruct.EmailTitle, htmlEmailStruct.EmailBody); // Convert the html body to a memory stream: Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(htmlEmailStruct.HTMLBody); MemoryStream htmlStream = new MemoryStream(bytes); // Add the HTML body to the message: AlternateView htmlView = new AlternateView(htmlStream, MediaTypeNames.Text.Html); msg.AlternateViews.Add(htmlView); // Ship it! emailClient.Send(msg); htmlView.Dispose(); htmlStream.Dispose(); return(true); } catch (Exception ex) { _logger.LogItem("EmailService SendMultiPartEmail() To - email send failed: " + ex.Message); return(false); } }