开发者

How to save email attachments in C# [closed]

开发者 https://www.devze.com 2022-12-16 03:57 出处:网络
开发者_C百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. Fo
开发者_C百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消