private async Task <bool> SendMessage(EmailCommunication email) { try { Mail mail = new Mail(); mail.From = new Email(email.Sender); mail.Subject = email.Subject; mail.AddContent(new Content("text/plain", email.Body)); AddRecipients(email, mail); AddAttachments(email, mail); string type = Constants.MetaSettings.Types.Api; string code = Constants.MetaSettings.Codes.SendGridApiKey; IMetaSettingRepository settingRepository = _DataRepositoryFactory.GetDataRepository <IMetaSettingRepository>(); string apiKey = settingRepository.GetSetting(type, code).Value; dynamic sg = new SendGrid.SendGridAPIClient(apiKey, "https://api.sendgrid.com"); dynamic response = await sg.client.mail.send.post(requestBody : mail.Get()); return(true); } catch (Exception e) { throw e; } }
public virtual bool SendMail(string sender, string recipients, string subject, string body, IEnumerable <string> ccRecipients, IEnumerable <string> bccRecipients, IEnumerable <string> attachments) { EmailCommunication email = new EmailCommunication() { Sender = sender, Recipients = recipients, Subject = subject, Body = body, CcRecipients = ccRecipients, BccRecipients = bccRecipients, Attachments = attachments }; return(SendMail(email)); }
protected override bool SendMail(EmailCommunication email) { OutlookEmailCommunication outlookEmail = (OutlookEmailCommunication)email; try { // need to install Outlook on the machine/server for this to work // commenting out the code until/if Outlook is installed //// create the outlook application. //_outlook = new Outlook.Application(); //if (_outlook == null) // throw new Exception("Outlook App creation failed."); //// create a new mail item. //Outlook.MailItem mailItem = _outlook.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; //// compose email //mailItem.HTMLBody = email.Body; //if (email.Attachments != null) //{ // foreach (string file in email.Attachments) // { // Outlook.Attachment attachment = mailItem.Attachments.Add(file); // } //} //mailItem.Subject = email.Subject; //mailItem.To = email.Recipients; //if (outlookEmail.OutlookSendType == OutlookSendType.SendDirect) // ((Outlook._MailItem)mailItem).Send(); //else if (outlookEmail.OutlookSendType == OutlookSendType.ShowModal) // mailItem.Display(true); //else if (outlookEmail.OutlookSendType == OutlookSendType.ShowModeless) // mailItem.Display(false); //mailItem = null; return(true); } catch (Exception e) { throw e; } }
private void AddRecipients(EmailCommunication email, Mail mail) { Personalization personalization = new Personalization(); string[] recipients = email.Recipients.Split(';'); recipients.ToList().ForEach(r => personalization.AddTo(new Email(r))); if (email.CcRecipients != null) { email.CcRecipients.ToList().ForEach(r => personalization.AddCc(new Email(r))); } if (email.BccRecipients != null) { email.BccRecipients.ToList().ForEach(r => personalization.AddBcc(new Email(r))); } mail.AddPersonalization(personalization); }
private void AddAttachments(EmailCommunication email, Mail mail) { if (email.Attachments != null) { foreach (string filePath in email.Attachments) { string fileContents = Convert.ToBase64String(File.ReadAllBytes(filePath)); string contentType = "application/octet-stream"; //new FileExtensionContentTypeProvider().TryGetContentType(filePath, out contentType); //contentType = contentType ?? "application/octet-stream"; Attachment attachment = new Attachment(); attachment.Content = fileContents; attachment.Type = contentType; attachment.Filename = Path.GetFileName(filePath); attachment.Disposition = "attachment"; mail.AddAttachment(attachment); } } }
protected abstract bool SendMail(EmailCommunication email);
protected override bool SendMail(EmailCommunication email) { return(SendMessage(email).Result); }