How to save email attachments in C# [closed]
Asked Answered
C

2

10

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

Club answered 11/1, 2010 at 6:38 Comment(1)
This link helped me to resolve the same problem, but this has 30 day trial. example-code.com/csharp/pop3_saveAttachments.asp Happy codingEmmittemmons
N
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.
Nikitanikki answered 7/6, 2012 at 11:9 Comment(2)
I don't have such methods and properties in attachment class, did you used some 3rd parties libs?Intolerable
where can i get POP3Class library ?Borrowing
D
-3

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);
}
Discant answered 8/7, 2010 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.