/// <summary> /// Send outbound message /// </summary> /// <param name="correlationId"> /// The correlation Id. /// </param> /// <param name="emailInformation"> /// The email Information. /// </param> public void Send(Guid correlationId, EmailInformation emailInformation) { MailMessage mailMessage = new MailMessage { From = new MailAddress(emailInformation.From, emailInformation.FromDisplayName), Subject = emailInformation.Subject }; string emailTempDir = Path.Combine(this.pickupDirectoryFullBasePath, Guid.NewGuid().ToString()); this.smtpClient.PickupDirectoryLocation = emailTempDir; try { Directory.CreateDirectory(emailTempDir); foreach (string toAdress in emailInformation.To) { mailMessage.To.Add(toAdress); } if (!string.IsNullOrEmpty(emailInformation.HtmlBody) && !string.IsNullOrEmpty(emailInformation.TextBody)) { mailMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(emailInformation.HtmlBody, new ContentType(HtmlContentType))); mailMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(emailInformation.TextBody, new ContentType(TextContentType))); } else if (!string.IsNullOrEmpty(emailInformation.HtmlBody)) { mailMessage.Body = emailInformation.HtmlBody; mailMessage.IsBodyHtml = true; } else if (!string.IsNullOrEmpty(emailInformation.TextBody)) { mailMessage.Body = emailInformation.TextBody; mailMessage.IsBodyHtml = false; } this.smtpClient.Send(mailMessage); string emailFilePath = Directory.GetFiles(emailTempDir).First(); byte[] emailFileContent = File.ReadAllBytes(emailFilePath); var viewBlob = this.blobContainer.GetBlockBlobReference(string.Format("{0}-view.eml", correlationId)); viewBlob.Properties.ContentType = "message/rfc822"; var downloadBlob = this.blobContainer.GetBlockBlobReference(string.Format("{0}-download.eml", correlationId)); using (MemoryStream ms = new MemoryStream(emailFileContent)) { ms.Position = 0; viewBlob.UploadFromStream(ms); ms.Position = 0; downloadBlob.UploadFromStream(ms); } } finally { Directory.Delete(emailTempDir, true); } }
/// <summary> /// The send. /// </summary> /// <param name="emailInformation"> /// The email information. /// </param> /// <param name="correlationId">the correlation id</param> public void Send(EmailInformation emailInformation, Guid?correlationId) { if (emailInformation == null) { throw new ArgumentNullException("emailInformation"); } if (emailInformation.From == null || emailInformation.To == null || emailInformation.To.Count < 1) { throw new ArgumentException("From or To values are missing", "emailInformation"); } SMTP smtpInstance; string userName; string password; if (emailInformation.IsTest) { userName = GetSetting(SendGridTestUserSetting); password = GetSetting(SendGridTestPasswordSetting); smtpInstance = SMTP.GetInstance(new NetworkCredential(userName, password), port: 587); } else { userName = GetSetting(SendGridUserSetting); password = GetSetting(SendGridPasswordSetting); smtpInstance = SMTP.GetInstance(new NetworkCredential(userName, password), port: 587); } IMail mail = Mail.GetInstance(); mail.From = new MailAddress(emailInformation.From, emailInformation.FromDisplayName); mail.Subject = emailInformation.Subject; foreach (string to in emailInformation.To) { mail.AddTo(to); } mail.DisableGoogleAnalytics(); if (!string.IsNullOrEmpty(emailInformation.HtmlBody)) { mail.Html = emailInformation.HtmlBody; } if (!string.IsNullOrEmpty(emailInformation.TextBody)) { mail.Text = emailInformation.TextBody; } if (!string.IsNullOrEmpty(emailInformation.ReplayTo)) { mail.ReplyTo = new[] { new MailAddress(emailInformation.ReplayTo) }; } if (!string.IsNullOrEmpty(emailInformation.Category)) { mail.SetCategory(emailInformation.Category); } Dictionary <string, string> uniqueIdentifiers; if (emailInformation.UniqueIdentifiers != null) { uniqueIdentifiers = new Dictionary <string, string>(emailInformation.UniqueIdentifiers); } else { uniqueIdentifiers = new Dictionary <string, string>(); } if (correlationId.HasValue) { uniqueIdentifiers["correlation_id"] = correlationId.ToString(); } if (uniqueIdentifiers.Count > 0) { mail.AddUniqueIdentifiers(uniqueIdentifiers); } smtpInstance.DeliverAsync(mail); }