Sending email with attachments; attachment arrives without extension in Microsoft Outlook

StackOverflow https://stackoverflow.com/questions/23662638

  •  22-07-2023
  •  | 
  •  

I created an C# application using SMTPClient that emails out attachments used for debugging (so usually just images or text files, but other types can be sent out too). The attachments arrive fine in Outlook (web version) and Gmail, but for Microsoft Outlook 2013, the attachments arrive without extension. This is a problem since the majority of the target users are going to be using Outlook 2013, and I can't ask them to manually add the file extension when downloading the attachment.

I have already explicitly filled the ContentDisposition fields of the attachment, but it didn't work.

private MailMessage BuildEmail(Message msg)
{
    MailMessage email = null;

    email = new MailMessage();
    email.Subject = msg.Subject.Trim();
    email.SubjectEncoding = UTF8Encoding.UTF8;
    email.From = new MailAdress(msg.Sender);
    email.Body = msg.Body;
    email.IsBodyHtml = msg.HasRichText;
    email.BodyEncoding = UTF8Encoding.UTF8;

    foreach(String to in msg.Recipients)
        email.To.Add(new MailAddress(to));

    email.Priority = msg.Priority == CatPriority.Urgent ? MailPriority.High : MailPriority.Normal;

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

    return email;
}

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

Message and MessageAttachment are classes with the necessary information for creating the email and attachments. This is the method I'm using to get the MIME type information:

public static string GetMimeType(this string fileExtension)
{
    string mimeType = "application/unknown";
    string ext = fileExtension.ToLower();
    Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
    if (regKey != null && regKey.GetValue("Content Type") != null)
         mimeType = regKey.GetValue("Content Type").ToString();
    return mimeType;
}

And to send it:

smtp = new SmtpClient(host);
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(sender, password);
smtp.EnableSsl = true;
smtp.Timeout = 30000;
smtp.Send(email);
有帮助吗?

解决方案

Okay, after some debugging I realized the mistake was with the name being passed as an argument in Attachment(...). The second argument should recieve the name of the file with the extension (e.g. "file.txt"), and my variable, ma.FileName, only had the name by itself. So even if I specified the MIME type in the third argument, the method didn't know the type of file it's supposed to handle. Or at least Outlook didn't.

att = new Attachment(new MemoryStream(ma.FileContent), ma.FileName + ma.FileType, ma.FileType.GetMimeType());

Adding the extension solved the problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top