File Attachment error with smtpop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MFrederickson
    New Member
    • Sep 2011
    • 1

    File Attachment error with smtpop

    Hello! I'm using smtpop to try and download an e-mail from the server. I haven't been able to get past the part in the code where it attempts to get the filename from the attachment (attach.FileNam e) This comes back as "". I was wondering what the issue was as I have *not* modified the source in any way.

    Here is my code:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SmtPop;
    using System.IO;
    using System.Runtime.Remoting.Messaging;
    
    namespace SMTPAttachmentDownloader
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                bool SuccessFile = false;
                // connect to pop3 server
                POP3Client pop = new POP3Client();
                
                pop.ReceiveTimeout = 3 * 60000; // Set the timeout to 3 minutes
                try 
                {
                    pop.Open("pop.server.com", 110, "user@domain.com", "xxxxx");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                // retrieve messages list from pop server
                POPMessageId[] messages = pop.GetMailList();
    
                if (messages != null)
                {
                    // run through available messages in POP3 server
                    foreach (POPMessageId id in messages)
                    {
                        POPReader reader = pop.GetMailReader(id);
                        MimeMessage msg = new MimeMessage();
    
                       
    
                        // read the message
                        msg.Read(reader);
                        //check for authentication
                        ////you could check the e-mail address sender, but this is easily spoofed.
                        //if (msg.AddressSender.ToString() == "malkerz@gmail.com")
                        ////another idea for this would be to put a 3 char code (SFW) in the front of the subject line
                        string testChallenge = "SFW";
                        string subject = msg.Subject.ToString();
                        string testPassword = subject.Substring(1, 3);
                        if (testChallenge == testPassword)
                     
                        {
                            if (msg.Attachments != null)
                            {
                                // retrieve attachements
                                foreach (MimeAttachment attach in msg.Attachments)
                                {
                                    
                                    Console.WriteLine(msg.Attachments.ToString());
    
                                    Console.WriteLine(attach.Filename.ToString());
    
                                    if (attach.Filename != "")
                                    {
                                        
    
                                        // read data from attachment
                                        Byte[] b = Convert.FromBase64String(attach.Body);
                                        
                                        
                                        // save attachment to disk
    
                                        System.IO.MemoryStream mem = new System.IO.MemoryStream(b, false);
                                        FileStream outStream = File.OpenWrite(attach.Filename);
                                        mem.WriteTo(outStream);
                                        mem.Close();
                                        outStream.Flush();
                                        outStream.Close();
                                        SuccessFile = true;
    
                                    }//end IF
                                }//end foreach
                            }//end Attachment check If
                        }//end sender verification
                        //delete message
                        if (SuccessFile)
                        {
                            pop.Dele(id.Id);
                        }
                    }
                }
                //close the connection to POP3 server
                pop.Quit();
    
            }
        }
    }
    Attached Files
  • Heeralal
    New Member
    • Nov 2011
    • 1

    #2
    Hi MFrederickson,

    Did you found your answer, I am also facing the same problem.

    Comment

    Working...