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

时间:2014-06-21 04:46:37

标签: c# email attachment email-attachments

我创建了C#Console应用程序,但我无法发送带有名为“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);
    }

1 个答案:

答案 0 :(得分:0)

尝试这种方式

        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;
        }