如何发送电子邮件与附件的文件没有扩展
本文关键字:文件 扩展 何发送 电子邮件 | 更新日期: 2023-09-27 18:09:53
我创建了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;
}