We help IT Professionals succeed at work.
Get Started

Get Attachment from mail in asp.net

Mahesh Yadav
Mahesh Yadav asked
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;
                    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);
      pop.Quit ();

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

Please advice
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE