如何在 C# 中保存电子邮件附件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2040321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save email attachments in C#
提问by user247841
How can I, using C#, Download an email attachment from my mail (for instance gmail)?
我如何使用 C# 从我的邮件(例如 gmail)中下载电子邮件附件?
回答by Martin Vobr
Following code is taken from Extract Attachments samplewhich comes with our Rebex Mailcomponent. Downloading from a POP3 server is covered in the HOWTO: Download emails from a GMail account in C#blogpost.
以下代码取自Rebex 邮件组件附带的提取附件示例。HOWTO:从 C#博文中的 GMail 帐户下载电子邮件中介绍了从 POP3 服务器下载。
// Load mail message from disk
MailMessage mail = new MailMessage ();
mail.Load (args[0]);
Console.WriteLine (
"Message contains {0} attachments.",
mail.Attachments.Count
);
// If message has no attachments, just exit
if (mail.Attachments.Count == 0)
return 0;
foreach (Attachment attachment in mail.Attachments)
{
// Save the file
Console.WriteLine ("Saving '{0}' ({1}).",
attachment.FileName, attachment.MediaType);
attachment.Save (attachment.FileName);
}
回答by Riya Ken
// Firstly you might want to use POP3Class which is mail support class.
POP3Class Pop3= new POP3Class();
pop3.DoConnect("your.mail.server",110,"username","password");
pop3.GetStat();
// and then you can use the below code for storing an attachment.
MailMessage mail = new MailMessage ();
Mail.Load (args[0]);
Console.WriteLine (
"Message contains {0} attachments.",
mail.Attachments.Count
);
// If message has no attachments, just exit
if (mail.Attachments.Count == 0)
return 0;
foreach (Attachment attachment in mail.Attachments)
{
// Save the file
Console.WriteLine ("Saving '{0}' ({1}).",
attachment.FileName, attachment.MediaType);
attachment.Save (attachment.FileName);
}
// Hope that helps.