即使附件显示正确,电子邮件也不会显示回形针图标

Emails do not display the paperclip icon even though the attachment is displayed correctly

当我发送带有附件的电子邮件时,它们被正确发送,我可以在大多数客户端中看到它们,但回形针没有显示。

Outlook 显示此行为。

如果没有像“bluewin.ch”这样的其他电子邮件客户端,在回形针不可用时它甚至不显示附件,这不会是一个大问题。

EML 生成器代码:

public static class EmlBuilder {

        private const string NewLine = "\r\n";

        /// <summary>
        /// Builds and .eml File https://en.wikipedia.org/wiki/MIME#Content-Type
        /// Opens it 
        /// And cleans up the temporary file as soon as it isn't needed anymore
        /// </summary>
        public static void BuildAndOpen(string receiver = null, string subject = null, string signature = null, params string[] attachments) {
            //header
            var version          = "MIME-VERSION: 1.0";
            var draftFlag        = $"X-Unsent: {true.ToInt()}";
            var from             = "From: <>"; 
            var xReceiver        = $"X-Receiver: {receiver}";
            var to               = $"To: {receiver}";
            var subj             = $"Subject: {subject}";

            //boundaries
            var boundary         = $"frontier";
            var contentType      = $"Content-Type: multipart/mixed; boundary={boundary}";
            var boundaryLine     = $"--{boundary}";

            //header-content
            var headerContent = string.Empty;
            headerContent     += version     + NewLine; 
            headerContent     += draftFlag   + NewLine;
            headerContent     += contentType + NewLine;
            headerContent     += from        + NewLine;

            if (subject.IsDefined()) {
                headerContent += subj        + NewLine;
            }
            if (receiver.IsDefined()) {
                headerContent += xReceiver   + NewLine;
                headerContent += to          + NewLine;
            }

            //between header and body
            var headerBodyBoundary = string.Empty;
            headerBodyBoundary += boundaryLine                             + NewLine;
            headerBodyBoundary += "Content-Type: text/html" + NewLine; 

            //body-content
            var bodyContent = string.Empty; 
            bodyContent    += string.Empty + NewLine; //TODO settings

            //add signature
            bodyContent += signature;

            //all attachments after body
            var fullAttachmentContent = string.Empty;

            var attachmentBoundary      = string.Empty;
            attachmentBoundary         += boundaryLine                              + NewLine;
            attachmentBoundary         += "Content-Type: application/{0}; name={1}" + NewLine;
            attachmentBoundary         += "Content-Transfer-Encoding: Base64"       + NewLine;
            attachmentBoundary         += "Content-Location: attachment"            + NewLine;
            attachmentBoundary         += string.Empty                              + NewLine;
            var singleAttachmentContent = string.Empty;
            singleAttachmentContent    += "{0}"                                     + NewLine;

            foreach (var attachment in attachments) {
                fullAttachmentContent += NewLine.Combine(
                    string.Format(attachmentBoundary,      Path.GetExtension(attachment), Path.GetFileName(attachment)), 
                    string.Format(singleAttachmentContent, Convert.ToBase64String(File.ReadAllBytes(attachment)))
                );
            }

            //final-file
            var tmpPath       = Path.GetTempPath();
            var date          = DateTime.Now.ToString("yyyyMMddTHHmmssfff");
            var guid          = Guid.NewGuid().ToString("N"); //no-hyphens
            var fileExtension = ".eml";
            var filePath      = Path.Combine(tmpPath, "_".Combine(date, guid) + fileExtension);
            File.WriteAllText(filePath, NewLine.Combine(headerContent, headerBodyBoundary, bodyContent, fullAttachmentContent), Encoding.Default);

            var            clients            = Applications.GetRunningEmailClients();
            List<Process>  unmanagedProcesses = new List<Process>();
            clients.ForEach(c => unmanagedProcesses.AddRange(c.GetProcesses()));

            //open file
            var managedProcess = Process.Start(filePath);

            //this is a workaround for the default behaviour of email clients
            //if a client is running when opening an .eml file, then the process of starting the file
            //exits immediately and is handed over to the already running client instance
            //at this point it's impossible to tell if the file is free to clean up yet

            Task.Run(() => {
                while (!managedProcess.HasExited) { Thread.Sleep((int)TimeSpan.FromSeconds(60).TotalMilliseconds); }
                if (unmanagedProcesses.Any()) unmanagedProcesses.ForEach(Tools.WaitForHasExited);
                try {
                    File.Delete(filePath);
                }
                catch { /*leave file orphaned*/ }
            });
        }
    }

我什至不知道如何定义 google 搜索这个问题,因为我无法避免使用非常常用的词,如“附件”、“电子邮件”、“outlook”等.

对于如何修复或调试此问题的任何想法,我将不胜感激。

如果需要任何进一步的信息,请随时询问。

回形针图标在 Outlook 中的可见性由名为 属性(DASL 名称 "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B")的 SmartNoAttach 决定,属性 可以使用 MailItem.PropertyAccessor.SetProperty - 使用上面的 DASL 名称将它设置为 false。

我找到问题了。

行:

attachmentBoundary += "Content-Location: attachment" + NewLine;

应该是

attachmentBoundary += "Content-Disposition: attachment" + NewLine;

通过此更改,附件可以正确显示。