【问题标题】:Mandrill.Net Adding Multiple AttachmentsMandrill.Net 添加多个附件
【发布时间】:2021-03-13 03:54:50
【问题描述】:

我正在尝试通过 Mandrill.Net 发送电子邮件,但在尝试添加多个附件时遇到了问题。我有以下代码将附件列表创建为IEnumerable,但出现错误

Unable to cast object of type 'System.Collections.Generic.List`1[Mandrill.Models.Attachment]' to type 'System.Collections.Generic.IEnumerable`1[Mandrill.Models.EmailAttachment]'.

代码如下:

                try
                {
                    EmailService ems = new EmailService();
                    EmailMessage msg = new EmailMessage();
                    List<EmailAddress> ToAdd = new List<EmailAddress>();
                    EmailAddress MainTo = new EmailAddress();
                    MainTo.Email = qe.ToAddress;
                    MainTo.Type = "bcc";
                    ToAdd.Add(MainTo);
                    msg.FromEmail = qe.FromAddress;
                    msg.FromName = qe.FromName;
                    msg.AddHeader("Reply-To", qe.ReplyTo);
                    msg.To = ToAdd;
                    msg.Subject = qe.Subject;
                    msg.Html = qe.Body;
                    msg.TrackClicks = true;
                    msg.TrackOpens = true;
                    //  Need to add in Email Attachments
                    List<EmailAttachment> lea = new QueuedEmailModels().GetEmailAttachmentsByQueuedEmailID(qe.QueuedEmailsID); // Gets List of Attachments in DB
                    List<Attachment> lma = new List<Attachment>();

                    foreach(BRPA.EmailAttachment ea in lea)
                    {

                        byte[] array = File.ReadAllBytes(System.Web.Hosting.HostingEnvironment.MapPath("~/UploadedFiles/" + ea.AttachmentID));
                        Attachment at = new Attachment
                        {
                            Content = Convert.ToBase64String(array),
                            Name = ea.AttachmentName,
                            Type = ""
                        };
                        lma.Add(at);

                    }
                    msg.Attachments = (IEnumerable<Mandrill.Models.EmailAttachment>)lma.AsEnumerable();

                    //  End of Attachments
                    await ems.SendMailviaMandrill(msg);

                    qe.Status = "Sent";
                    qe.DateTimeSent = DateTime.Now.AddHours(2);
                    await ctx.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

谢谢

保罗

【问题讨论】:

    标签: email mandrill


    【解决方案1】:

    糟糕 - 找到答案了。

    我有一个 EmailAttachment 的类覆盖了 Mandrill.Models.EmailAttachment

    指定全名,一切正常。

    【讨论】:

    • 您好,您使用的是 web api,您是在声明单例吗?如果您使用的是单例,您能否分享如何在使用的控制器中声明它?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2017-10-12
    • 1970-01-01
    • 2021-12-09
    • 2021-10-30
    • 2012-01-04
    • 2014-12-17
    相关资源
    最近更新 更多