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

10

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


这个链接帮助我解决了同样的问题,不过需要试用30天。http://www.example-code.com/csharp/pop3_saveAttachments.asp 祝你编码愉快。 - Ravi Vanapalli
2个回答

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.

我在“attachment”类中没有这样的方法和属性,你使用了一些第三方库吗? - Johnny_D
1
我在哪里可以获取POP3Class库? - Muhammad Sannan Khalid

-3
以下代码摘自我们的Rebex Mail组件附带的提取附件示例。从POP3服务器下载已在C#中从GMail帐户下载电子邮件的HOWTO博客文章中介绍。
// 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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接