예제 #1
0
        /// <summary>
        /// Create the message to send
        /// </summary>
        /// <param name="mailMetadata">Mail metadata</param>
        /// <returns>Message to send</returns>
        /// <exception cref="FileNotFoundException">An attachment does not exist</exception>
        private MimeMessage CreateMessage(MailMetadata mailMetadata)
        {
            var mailMessage = new MimeMessage();

            mailMessage.From.Add(
                new MailboxAddress(
                    this.MailConfiguration.SendingDisplayName,
                    this.MailConfiguration.SendingEmail));
            mailMessage.To.AddRange(mailMetadata.MainReceivers);
            mailMessage.Cc.AddRange(mailMetadata.SecondaryReceivers);
            mailMessage.Bcc.AddRange(mailMetadata.HiddenReceivers);
            mailMessage.Subject = mailMetadata.MailSubject;

            //Create the body
            var bodyBuilder = new BodyBuilder();

            if (mailMetadata.ContentFormat == TextFormat.Text)
            {
                bodyBuilder.TextBody = mailMetadata.MailContent;
            }
            else
            {
                bodyBuilder.HtmlBody = mailMetadata.MailContent;
            }

            //Add attachments
            foreach (var attachment in mailMetadata.Attachments)
            {
                if (!attachment.Exists)
                {
                    throw new FileNotFoundException($"The file {attachment.Name} does not exist");
                }
                bodyBuilder
                .Attachments
                .Add(attachment.Name, attachment.OpenRead());
            }

            mailMessage.Body       = bodyBuilder.ToMessageBody();
            mailMessage.Date       = new DateTimeOffset();
            mailMessage.Importance = mailMetadata.MailImportance;
            mailMessage.Priority   = mailMetadata.MailPriority;

            return(mailMessage);
        }
예제 #2
0
 /// <summary>
 /// Send an email
 /// </summary>
 /// <returns>Sending result</returns>
 public async Task <MailSendingResult> SendAsync(MailMetadata mailMetadata)
 {
     //Create the message
     return(await this.SendAsync(this.CreateMessage(mailMetadata)));
 }