Lect22 - Emails IV
Lect22 - Emails IV
Objectives:
Learn about the MIME format
Learn how to write a basic MIME parser
1. Overview of MIME
The original SMTP protocol (RFC 821) and Message Format (RFC 822)
were designed to construct mail and send it using only text in ASCII
encoding (7 bits).
That is, the mail is still sent using only ASCII text, but other types of
data can be sent by first encoding them into ASCII characters and
appending them to the mail.
MIME-Version
Content-Type
Content-Transfer-Encoding
Content-Disposition
Content-ID
Content-Description
1.1 MIME-Version:
1.2 Content-Type:
This header is used to specify the media (data) type and subtype in
the body of a message and to fully specify the representation of such
data.
There are seven main types defined, namely: text, image, audio,
video, application, multipart and message. A number of sub-types
are defined under each of these categories.
The headers are then followed by a blank line and then the body of
the part.
An email message may also have alternative parts, where the MUA is
expected to select one of the options. In that case, the
multipart/alternative is used. The format is:
1.3 Content-Transfer-Encoding:
1.4 Content-Disposition:
1.5 Content-ID:
1.6 Content-Description:
------=_NextPart_000_002B_01C31DF9.7CF57870
Content-Type: multipart/alternative;
boundary="----=_NextPart_001_002C_01C31DF9.7CF57870"
------=_NextPart_001_002C_01C31DF9.7CF57870
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Salaam,
Regards,
Bashir
------=_NextPart_001_002C_01C31DF9.7CF57870
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
------=_NextPart_001_002C_01C31DF9.7CF57870--
------=_NextPart_000_002B_01C31DF9.7CF57870
Content-Type: application/msword;
name="KeyboardShortcuts.doc"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="KeyboardShortcuts.doc"
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAACAAAAoQAAAA
EAAAowAAAAEAAAD+////AAAAAJ8AAACgAAAA////////////////////////////////////////
//deleted
AAAAAAAAogEAAAAAAACiAQAAAAAAAKIBAAAAAAAAogEAABQAAAAAAAAAAAAAALYBAAAAAAAA
AAAAAAB+LAAAAAAAAH4sAAAAAAAAfiwAACwAAACqLAAADAIAALYBAAAAAAAAETYAAO4AAADC
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAA=
------=_NextPart_000_002B_01C31DF9.7CF57870
Content-Type: image/gif;
name="doc1.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="doc1.gif"
R0lGODlhlgBYALMAAABSSgBjWgBrUgBrYwBzUgB7YwhjYwhrYwh7YxBaUiFrY1KllKXOzt739//3
9////ywAAAAAlgBYAAAE/lDISau9OOvNu/9gKI5kaZ5oqq5s675wLM90bd94ru987//AoHBILBqP
//deleted
YC4EYZGnaLSBDQNYxi9QO+Id3DXMmFCVqEQBBjK0VsnHjbOc42yM5s7kQGKABndhiOS5mVm2
XwMtNcO8LtjKVs4Bdz/EMWS4UA2wjbSkJ03pSlv60pjOtKY3zelOe/rToA61qEdN6lKb+tScjgAA
ADs=
------=_NextPart_000_002B_01C31DF9.7CF57870
Content-Type: image/gif;
name="graph1.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="graph1.gif"
R0lGODlhZgHdAPcAAAQCBISChERCRMTCxCQiJKSipGRiZOTi5BQSFJSSlFRSVNTS1DQyNLSytHRy
//deleted
0lcuD2gBQzZQAgB+YHsP+C/4LsDVCTSASzGIwKY0IIHLwc0FXquAZH1qkB3QYKD1vTBxHHWCL
AwGwgAZ4KVCqGdnABwkogYJCgIIXYrjF1XGmgGdQgxBYQHgEQAEOCPA50rq4NgEBADs=
------=_NextPart_000_002B_01C31DF9.7CF57870--
2. MIME Parser
To process a MIME message, a mail reader needs to parse through
the message and extract the components based on MIME headers
contained in the message.
using System;
using System.IO;
using System.Text;
using System.Web.Mail;
if (mainBoundary.Length != 0)
MoveToPart(mainBoundary);
}
return sb.ToString();
}
if (boundary.StartsWith("\""))
boundary = boundary.Substring(boundary.IndexOf('\"')
+1);
if (boundary.EndsWith("\""))
boundary = boundary.Substring(0, boundary.Length-1);
}
}
}
if (mainBoundary.Length != 0)
message.Body =
Encoding.ASCII.GetString(NextAttachment().Content);
else {
message.Body = reader.ReadToEnd();
hasMorePart = false;
}
return message;
}
if (line.IndexOf(boundary+"--") > 0)
hasMorePart = false;
else
hasMorePart = true;
return filename;
}
if (s.IndexOf(boundary+"--") > 0)
hasMorePart = false;
else
hasMorePart = true;
return sb.ToString();
}
MoveToPart(mainBoundary);
}
}
Note:
To use the MIME parser above, you need to follow the following
steps:
Connect to a POP server and obtain a socket,
Use the socket to create StreamReader and StreamWriter
objects
Use the StreamWriter to issue a RETR command to retrieve a
particular message and read the response indicator line.
Use the StreamReader object to create an instance of
MimeParser.
Finally, use the following public property and methods of the
MimeParser instance shown below to obtain the Message and
the Attachments that may be contained in the message.
public MailMessage MainMessage
public bool HasMoreAttachment()
public Attachment NextAttachment()