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

发布于 2024-08-17 22:56:02 字数 39 浏览 5 评论 0原文

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

How can I, using C#, Download an email attachment from my mail (for instance gmail)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残 2024-08-24 22:56:02
 //  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.
 //  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.
简单爱 2024-08-24 22:56:02

以下代码取自提取附件示例使用我们的 Rebex Mail 组件。 HOWTO:在 C# 中从 GMail 帐户下载电子邮件 博客文章。

// 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);
}

Following code is taken from Extract Attachments sample which comes with our Rebex Mail component. Downloading from a POP3 server is covered in the HOWTO: Download emails from a GMail account in C# blogpost.

// 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文