Attachments.Add() 缺少多个文件

请检查下面的简单邮件方法。出于测试目的,我通过文件位置路径字符串手动附加了两个文件,如您所见 -mail.Attachments.Add(new Attachment(@"C:\Users... 但是这种方法的问题是,当我运行它时,第一个附件与电子邮件一起附加,但总是缺少第二个附件。我在这里做错了什么?任何想法?提前致谢


public static bool SendEmail(string password, string from, string to, string cc, string subject, string[] attachedFiles, string body, string host, int port)

        {

            try

            {

                MailMessage mail = new MailMessage(from, to);



                //foreach (var attachedFile in attachedFiles)

                //{

                //    mail.Attachments.Add(new Attachment(attachedFile.ToString()));

                //}



                mail.Attachments.Add(new Attachment(@"C:\Users\liaka\Desktop\Jordan\FileMailer\FileMailer\Backlog_07_12_2018.xlsx"));

                mail.Attachments.Add(new Attachment(@"C:\Users\liaka\Desktop\Jordan\FileMailer\FileMailer\test.txt"));


                mail.Subject = subject;

                mail.Body = body;

                mail.CC.Add(cc);


                var client = new SmtpClient(host, port)

                {

                    Credentials = new NetworkCredential(from, password),

                    EnableSsl = true

                };

                client.Send(mail);

                return true;

            }

            catch (Exception ex)

            {

                return false;

            }


        }


ibeautiful
浏览 395回答 1
1回答

茅侃侃

根据我的检查,您的MailMessage()方法使用看起来不错。我已经测试了我自己,它可以很好地处理多个附加文件。现在你只能检查哪些是-1)您的文件确实存在于您提供的文件路径中。(通过使用 Windows 文件资源管理器地址栏访问该路径进行检查)2)您尝试附加的文件(特别是缺少哪个文件)不是空文件(因为当我运行测试空文件时没有附加但是使用gmail SMTP进行测试)如果上述事情是正确的,那么我认为这可能是您的其他电子邮件设置的问题?
打开App,查看更多内容
随时随地看视频慕课网APP