c# - 如何发送没有扩展名的文件附件的电子邮件?

标签 c# email attachment email-attachments

我创建了 C# 控制台应用程序,但我无法发送带有名为“testfile”且没有扩展名的文件附件的电子邮件。

发送成功,但只有第一个附件(“test.png”) 如何发送此文件?

这是我的代码:

internal static void SendTest()
{
    MailMessage mail = new MailMessage("Punter@gmail.com", "Michael378@live.com",
                                       "Success", "Attachment email");

    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
    SmtpServer.Credentials = new System.Net.NetworkCredential("Punter@gmail.com",      "BrudoTass");
        SmtpServer.EnableSsl = true;

        string test1 = @"C:\Users\Admin\Desktop\test.png";          //Picture
        string test2 = @"C:\Users\Admin\Desktop\testfile";   //File without extension
        var attachment1 = new Attachment(test1);
        var attachment2 = new Attachment(test2);

        mail.Attachments.Add(attachment1);
        mail.Attachments.Add(attachment2);

        SmtpServer.Send(mail);
    }

最佳答案

试试这个方法

        foreach (MessageAttachment ma in msg.Attachments)
        mail.Attachments.Add(BuildAttachment(ma));

        private Attachment BuildAttachment(MessageAttachment ma)
        {
            Attachment att = null;

            if (ma == null || ma.FileContent == null)
                    return att;

            att = new Attachment(new MemoryStream(ma.FileContent), ma.FileName + ma.FileType, ma.FileType.GetMimeType());
            att.ContentDisposition.CreationDate = ma.CreationDate;
            att.ContentDisposition.ModificationDate = ma.ModificationDate;
            att.ContentDisposition.ReadDate = ma.ReadDate;
            att.ContentDisposition.FileName = ma.FileName;
            att.ContentDisposition.Size = ma.FileSize;

            return att;
        }

关于c# - 如何发送没有扩展名的文件附件的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24338385/

相关文章:

c# - 两个富文本框的相同滚动条

带有 Airflow 的电子邮件

android - 如何在我的 Android 应用程序中打开电子邮件附件?

WordPress:如何获取附件的日期?

c# - 为什么.NET Core DI 容器不注入(inject) ILogger?

c# - 如何配置 MassTransit 在失败时重试 context.Publish()?

c# - 将列表存储在所有用户都可以访问的内存中

c# - Dynamics 365 Web API 电子邮件发送

python - 在 Python 中发送带有 HTML+plain_text 电子邮件的 PDF 附件

iphone - 如何从 iPhone 发送文件(txt/XML)到服务器(网络或电子邮件)