We help IT Professionals succeed at work.

Get Attachment from mail in asp.net

Mahesh Yadav
Mahesh Yadav asked
on
Medium Priority
916 Views
Last Modified: 2012-05-12
I am using SMTPop to read the mails in asp.net C#,
I am able to read the mail but unable to get the attachment.
My code is as follows:


string bodyofmail ;
            SmtPop.POP3Client pop = new SmtPop.POP3Client();
            pop.Open("mail.server.com", 110, "username", "password");
            SmtPop.POPMessageId[] messages = pop.GetMailList();
      if (messages != null)
       {
              foreach (SmtPop.POPMessageId id in messages)
              {            
                    SmtPop.POPReader reader = pop.GetMailReader(id);
                    SmtPop.MimeMessage msg = new SmtPop.MimeMessage ();
     
                    // read message
                    msg.Read (reader);
           
            if (msg.Attachments != null)
            {
                // do something with first attachment
                SmtPop.MimeAttachment attach = msg.Attachments[0];
               
                if (attach.Attachments.Count > 0)
                    bodyofmail = attach.Attachments[0].Body;
                else
                    bodyofmail = attach.Body;

                    if (attach.Filename == "data")
                    {
                        // read data from attachment
                        Byte[] b = Convert.FromBase64String(attach.Body);

                        MemoryStream mem = new System.IO.MemoryStream(b, false);
                        BinaryFormatter f = new BinaryFormatter();

                        DataClass data = (DataClass)f.Deserialize(mem);
                        mem.Close();
                    }
                }          
            }
              }
      pop.Quit ();


Always I am getting attach.FileName = ""
Also unable to find the namespace for DataClass

Please advice
Comment
Watch Question

 try this..
                                // Identify the file name.
                                filename = System.IO.Path.GetFileName(Folderpath);
                                Response.Clear();
                                // Specify the Type of the downloadable file.
                                Response.ContentType = "application/octet-stream";
                                // Set the Default file name in the FileDownload dialog box.
                                Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                                Response.Flush();
                                // Download the file.
                                Response.WriteFile(path);

Author

Commented:
Thanks kovilpattiBalu,

I tried your code, but nither it is giving any error nor it is saving the file. I can have multiple attachments of any type.

Please advice.

The problem occures when we are using the first attachment to read the attachment, as in attachment list the second onwards are the actual attachments.

Author

Commented:
As the solution given by experts doesn't match my problem

Explore More ContentExplore courses, solutions, and other research materials related to this topic.