当前位置: 首页>>代码示例>>C#>>正文


C# EmailMessage.AddHeader方法代码示例

本文整理汇总了C#中EmailMessage.AddHeader方法的典型用法代码示例。如果您正苦于以下问题:C# EmailMessage.AddHeader方法的具体用法?C# EmailMessage.AddHeader怎么用?C# EmailMessage.AddHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在EmailMessage的用法示例。


在下文中一共展示了EmailMessage.AddHeader方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenerateProspectiveMailMessage

        /// <summary>
        ///     Creates a MailMessage for the current MailAttribute instance.
        /// </summary>
        protected EmailMessage GenerateProspectiveMailMessage(MailAttributes mail)
        {
            //create base message
            var message = new EmailMessage
            {
                FromName = mail.From.DisplayName,
                FromEmail = mail.From.Address,
                To = mail.To.Union(mail.Cc).Select(t => new EmailAddress(t.Address, t.DisplayName)),
                BccAddress = mail.Bcc.Any() ? mail.Bcc.First().Address : null,
                Subject = mail.Subject,
                Important = mail.Priority == MailPriority.High ? true : false
            };

            // We need to set Reply-To as a custom header
            if (mail.ReplyTo.Any())
            {
                message.AddHeader("Reply-To", string.Join(" , ", mail.ReplyTo));
            }

            // Adding content to the message
            foreach (var view in mail.AlternateViews)
            {
                var reader = new StreamReader(view.ContentStream, Encoding.UTF8, true, 1024, true);

                var body = reader.ReadToEnd();

                if (view.ContentType.MediaType == MediaTypeNames.Text.Plain)
                {
                    message.Text = body;
                }
                if (view.ContentType.MediaType == MediaTypeNames.Text.Html)
                {
                    message.Html = body;
                }
            }

            // Going through headers and adding them to the message
            mail.Headers.ToList().ForEach(h => message.AddHeader(h.Key, h.Value));

            // Adding the attachments
            var attachments = new List<EmailAttachment>();
            foreach (var mailAttachment in mail.Attachments.Select(attachment => ActionMailerNext.Utils.AttachmentCollection.ModifyAttachmentProperties(attachment.Key, attachment.Value, false)))
            {
                using (var stream = new MemoryStream())
                {
                    mailAttachment.ContentStream.CopyTo(stream);
                    var base64Data = Convert.ToBase64String(stream.ToArray());
                    attachments.Add(new EmailAttachment
                    {
                        Content = base64Data,
                        Name = mailAttachment.Name,
                        Type = mailAttachment.ContentType.MediaType,
                    });
                }
            }

            message.Attachments = attachments;

            return message;
        }
开发者ID:cyberid41,项目名称:kawaldesa,代码行数:63,代码来源:MandrillMailSender.cs


注:本文中的EmailMessage.AddHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。