gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:28:43 25 4
gpt4 key购买 nike

我创建了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com