简体   繁体   English

MailItem SaveAs()方法FIletypes

[英]MailItem SaveAs() Method FIletypes

When I pass olMHTML it saves it with .msg extension, but if I change the extension to .mht it works as an mht file perfectly and if I don't I have trouble opening the file with .msg extension. 当我传递olMHTML时,它会以.msg扩展名保存它,但如果我将扩展名更改为.mht,它将完美地作为mht文件工作,如果我不这样做,则无法使用.msg扩展名打开文件。 If I save it as olMSGUnicode, it saves with no extension but adding the .msg extension makes it work perfectly as a .msg file. 如果我将其保存为olMSGUnicode,它将保存没有扩展名,但添加.msg扩展名使其完美地作为.msg文件。

What is the correct way to save as .mht as olMHTML seems to be .msg format although it seems wrong because as I said I have trouble opening the msg file but works fine when renamed to .mht, and olMSGUnicode while doesn't save with an extension by itself, opens file as an .msg file. 保存为.mht的正确方法是什么,因为olMHTML似乎是.msg格式,虽然它似乎是错误的,因为正如我所说我无法打开msg文件但在重命名为.mht时工作正常,而olMSGUnicode虽然没有保存一个扩展本身,打开文件作为.msg文件。

To be clear, why is olMHTML saving as msg instead of mht? 要清楚,为什么olMHTML保存为msg而不是mht? olMSGUnicode works fine for msg files although they save with no extension adding .msg makes them work perfect. olMSGUnicode适用于msg文件,虽然它们保存没有扩展添加.msg使它们工作完美。 As does changing olMHTML to .mht from .msg. 正如从.msg将olMHTML更改为.mht一样。

EDIT: By doing a File.Move() after saving I can rename it to .mht and it works fine, but it would be much more convenient to just save int the correct format. 编辑:通过保存后执行File.Move()我可以将其重命名为.mht并且它工作正常,但只是保存正确的格式将更方便。

EDIT2: Code by commenter's request: EDIT2:评论者的要求:

        if (!filename.EndsWith(".mht"))
        {
            filename = filename + ".mht";
        }
        try
        {
            message.SaveAs(path + filename, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMHTML);
            MessageBox.Show(String.Format("Success saving file {0} at {1}", filename, path), "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            string newfilename = filename.Substring(0, filename.Length - 4) + ".mht";
            File.Move(path + filename, path + newfilename);
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(String.Format("Error {0} while trying to save {1} at {2}", ex, filename, path), "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

I realized I hadn't updated the if statement to not be .msg, it is working now. 我意识到我没有更新if语句不是.msg,它现在正在工作。 But still doesn't explain why when using olMHTML as save type, I get a file with no extension rather than with an .mht extension and I have to specify it while with other SaveAs types I do not. 但仍然没有解释为什么当使用olMHTML作为保存类型时,我得到一个没有扩展名而不是扩展名为.mht的文件,我必须指定它而不是其他的SaveAs类型。 But that is a question for another day. 但这是另一天的问题。 I figure the answer out. 我想出了答案。

I am not sure how it is possible to get a wrong extension if you are the one who specifies the file name (which includes the extension) when calling MailItem.SaveAs. 如果您是在调用MailItem.SaveAs时指定文件名(包括扩展名)的人,我不确定如何获得错误的扩展名。

Make sure you specify fully qualified file name that includes the directory, filename, and extension. 确保指定包含目录,文件名和扩展名的完全限定文件名。

The OlSaveAsType enumeration page in MSDN provides the table of values and corresponding file extensions: MSDN中的OlSaveAsType枚举页面提供值表和相应的文件扩展名:

olTXT Text format (.txt) olTXT文本格式(.txt)

olRTF Rich Text format (.rtf) olRTF富文本格式(.rtf)

olTemplate Microsoft Outlook template (.oft) olTemplate Microsoft Outlook模板(.oft)

olMSG Outlook message format (.msg) olMSG Outlook邮件格式(.msg)

olDoc Microsoft Office Word format (.doc) olDoc Microsoft Office Word格式(.doc)

olHTML HTML format (.html) olHTML HTML格式(.html)

olVCard VCard format (.vcf) olVCard VCard格式(.vcf)

olVCal VCal format (.vcs) olVCal VCal格式(.vcs)

olICal iCal format (.ics) olICal iCal格式(.ics)

olMSGUnicode Outlook Unicode message format (.msg) olMSGUnicode Outlook Unicode消息格式(.msg)

olMHTML MIME HTML format (.mht) olMHTML MIME HTML格式(.mht)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM