如何在C#中保存电子邮件附件

时间:2010-01-11 06:39:00

标签: c# .net email attachment pop3

如何使用C#从我的邮件中下载电子邮件附件(例如gmail)?

2 个答案:

答案 0 :(得分:0)

 //  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.

答案 1 :(得分:-3)

以下代码来自我们的Extract Attachments sample组件附带的Rebex Mail。从HOWTO: Download emails from a GMail account in C#博客文章

中介绍了从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);
}