Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

dealing with email attachement

Posted on 2009-07-01
8
450 Views
Last Modified: 2013-11-29

Hello group,

Using a C# class I am able to open a file on POP3 server (Thanks to CodeProject) but now the issues is that the attached PDF file to email is in encoded into base64 (algorithm) I guess. How can I save it as a real PDF file now?

Any help is appreciated.

Regards,
ak

Content-Transfer-Encoding: base64
Content-ID: <931142921@25062009-2C73>
Content-Description: image002.jpg
Content-Location: image002.jpg
 
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIf
IiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/2wBDAQoLCw4NDhwQEBw7KCIoOzs7Ozs7
Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozv/wAARCACHAJoDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
 
and more ...

Open in new window

0
Comment
Question by:akohan
8 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 24760221
Well, in the example you provide, there is a jpeg file, not a pdf
Besides this, you can use Convert.FromBase64String() method:
http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx

so, having a string encoded in base64, you can write it to a file like:

byte[] binaryData;
            try {
                byte[] binaryData = System.Convert.FromBase64String(yourBase64String);
                System.IO.WriteAllBytes("yourFilePath", binaryData);
            }
            catch { }
0
 

Author Comment

by:akohan
ID: 24760637

Thank you so much for your help but one thing I'm not sure is how should I know or how does the function know where it starts and where it ends?

Once again thank you.

Regards.
0
 
LVL 6

Expert Comment

by:jwenting
ID: 24761961
Where the encoded content starts is defined in the protocol definition for the email format (in this cases POP3/SMTP.

Seeing the simple example you give, that would be right after the headers and any whitespace following them, so at the beginning of line 6.
If the file is an attachment instead of inlined in the email, it could well be contained as encoded data inside the headers.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 5

Expert Comment

by:basiclife
ID: 24766756
Headers should have a single cariage return between each of them and a double carriage return after the last one.
0
 
LVL 5

Expert Comment

by:basiclife
ID: 24766904
The carriage returns mentioned are actually \r\n (CR followed by a line feed)


eg:

Content-Transfer-Encoding: base64\r\n
Content-ID: <931142921@25062009-2C73>\r\n
Content-Description: image002.jpg\r\n
Content-Location: image002.jpg\r\n
\r\n
CONTENT_HERE
Also between each section should be something like this

------_=_NextPart_001_01C9FAF9.FBDB9745

As mentioned above, check the email specification for what to look for. Each section should tell you what the "boundary" is for the next section as shown below

Content-Type: multipart/mixed; boundary="=======AVGMAIL-31DB56D0======="


--=======AVGMAIL-31DB56D0=======
Content-Type: multipart/related;
    boundary=----_SmarterMail_NextPart_5110738565878520


Which shows you how one boundary leads on to another

Open in new window

0
 

Author Comment

by:akohan
ID: 24767313

Hello all,

Thanks for your comments and hints. Does this mean that I should read it line by line? Below I have pasted a smal portion of the PDF file (in base64). From what I have understood so far I think that I have to read a text file and keep an eye on those boundries like two numeric lines in below:



--------------080501060300010207080106--

--------------080107050402070004010208



Content-Type: application/pdf;
 name="myfile.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
 filename="test.pdf"

JVBERi0xLjQNJeLjz9MNCjEzMCAwIG9iag08PC9MaW5lYXJpemVkIDEvTCAyNjY0NTMvTyAx
MzIvRSAzODk4MS9OIDM2L1QgMjYzODA1L0ggWyA3MTYgNTc3XT4+DWVuZG9iag0gICAgICAg
ICAgICAgDQp4cmVmDQoxMzAgMjENCjAwMDAwMDAwMTYgMDAwMDAgbg0KMDAwMDAwMTI5MyAw
MDAwMCBuDQowMDAwMDAxMzc4IDAwMDAwIG4NCjAwMDAwMDE1MTIgMDAwMDAgbg0KMDAwMDAw
MTcwNyAwMDAwMCBuDQowMDAwMDAyNDIxIDAwMDAwIG4NCjAwMDAwMDI3MjggMDAwMDAgbg0K
MDAyNTk2MDkgMDAwMDAgbg0KMDAwMDI1OTc0MiAwMDAwMCBuDQowMDAwMjU5ODQ4IDAwMDAw
........................................................................
........................................................................
........................................................................
........................................................................
IG4NCjAwMDAyNjM1NDMgMDAwMDAgbg0KdHJhaWxlcg0KPDwvU2l6ZSAxMzA+Pg0Kc3RhcnR4
cmVmDQoxMTYNCiUlRU9GDQo=
--------------080107050402070004010208
Content-Type: multipart/alternative;
      boundary="=======AVGMAIL-3918184C======="

--=======AVGMAIL-3918184C=======
Content-Type: text/plain; x-avg=cert; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Description: "AVG certification"


No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 8.5.375 / Virus Database: 270.13.2/2214 - Release Date: 07/02/09 05=
:54:00

--=======AVGMAIL-3918184C=======--
--------------080107050402070004010208--


Thanks,
ak
0
 

Author Closing Comment

by:akohan
ID: 31599037

Thanks!
0
 

Author Comment

by:akohan
ID: 24842350

Thanks to all for your help.

0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question