private MailMessage GetMailMessage(MailObject mailObject) { try { MailMessage mm = new MailMessage(); mm.BodyEncoding = UTF8Encoding.UTF8; mm.IsBodyHtml = true; mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; MailAddress senderMailAddress = new MailAddress(mailObject.Account.SenderEMail, mailObject.Account.SenderName); mm.From = senderMailAddress; mm.ReplyToList.Add(senderMailAddress); mm.To.Add(GetRecipients(mailObject.Recipients)); mm.Subject = mailObject.Subject; mm.Headers.Add("Disposition-Notification-To", mailObject.Account.SenderEMail); //for delivery receipt mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; Attachment att; foreach (string litem in mailObject.FileAttachments) { att = new Attachment(litem); mm.Attachments.Add(att); } AlternateView alternateView; string htmlBody = mailObject.Body; if (mailObject.Signature.Trim().Length > 0) { LinkedResource resource = new LinkedResource(mailObject.Signature.Trim()); resource.ContentId = Guid.NewGuid().ToString(); htmlBody = String.Format(htmlBody + @"<br><a href='" + mailObject.SignLink + @"'><img src=""cid:{0}"" /></a>", resource.ContentId); alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html); alternateView.LinkedResources.Add(resource); } else { alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html); } mm.AlternateViews.Add(alternateView); return(mm); } catch (Exception ex) { throw ex; } }
private void SendMail() { #region GetMailObject MailObject mailObject = new MailObject(); mailObject.Server = Tools.GetMailServerDetails(); mailObject.Account = Tools.GetMailAccountDetails(); if (Tools.GetEnvValue("RECIPIENTS").Trim().Length > 0) { mailObject.Recipients = txtEMail.Text.Trim() + "," + Tools.PBDeCrypt(Tools.GetEnvValue("RECIPIENTS").Trim()).Trim(); } else { mailObject.Recipients = txtEMail.Text.Trim(); } mailObject.Subject = txtSubject.Text.Trim(); string body = File.ReadAllText(Tools.GetEnvValue("HTMLBODY").Trim()); mailObject.Body = Formatter.FormatEmailBody(body, _roomNo, _checkInDate, _guestName, _emailTo); mailObject.Signature = Tools.GetEnvValue("SIGNATURE").Trim(); mailObject.SignLink = Tools.GetEnvValue("SIGNLINK").Trim(); string[] files = new string[listBox1.Items.Count]; int idx = 0; foreach (MailAttachments litem in listBox1.Items) { files[idx] = litem.GetFilePath(); idx++; } mailObject.FileAttachments = files; #endregion Mail mail = new Mail(mailObject); mail.SendMailCompletted += mail_SendMailCompletted; Thread workerThread = new Thread(mail.SendMail); workerThread.Start(null); while (!workerThread.IsAlive) { ; } }
public void SendMail(object obj) { MailResult res = null; try { MailObject mailObject = _mailObject; SmtpClient client = new SmtpClient(); client.Host = mailObject.Server.Host; client.Port = mailObject.Server.Port; client.EnableSsl = mailObject.Server.EnableSSL; client.Timeout = mailObject.Server.TimeOut; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(mailObject.Account.ID, mailObject.Account.Password); client.Send(GetMailMessage(mailObject)); res = new MailResult(); res.Required = true; res.Success = true; res.Message = "Mail Sent"; OnSendMailCompletted(res); } catch (Exception ex) { res = new MailResult(); res.Required = true; res.Success = false; res.Message = ex.Message; OnSendMailCompletted(res); } }
public Mail(MailObject mailObject) { _mailObject = mailObject; }