如何保存电子邮件附件在C# [英] How to save email attachments in C#

查看:518
本文介绍了如何保存电子邮件附件在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

  //首先,你可能想使用POP3Class这是邮件支持类。

     POP3Class的POP3 =新POP3Class();
     pop3.DoConnect(your.mail.server,110,用户名,密码);
     pop3.GetStat();


  //然后就可以使用下面的code存储附件。

     MailMessage电子邮件=新MailMessage();
     Mail.Load(参数[0]);

     Console.WriteLine(
     消息包含{0}附件。
     mail.Attachments.Count
     );

    //如果消息没有附件,只是退出
    如果(mail.Attachments.Count == 0)
    返回0;

    的foreach(附件附件mail.Attachments)
   {
   //保存文件
   Console.WriteLine(保存{0}({1})。
   attachment.FileName,attachment.MediaType);
   attachment.Save(attachment.FileName);
   }


//希望有所帮助。
 

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

解决方案

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

这篇关于如何保存电子邮件附件在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆