⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 despop3client.aspx.htm

📁 电子邮件
💻 HTM
📖 第 1 页 / 共 5 页
字号:
			</tr>
			</tbody></table>
			
			
			
			
						
			
			
			
			
			
			
			
			
			<span id="intelliTXT">
			<div id="contentdiv">
			
			<!-- Main Page Contents Start -->
			

<!-- Article Starts -->

<ul class="download">
<li><a href="http://www.codeproject.com/KB/IP/despop3client/despop3client_src.zip">Download source files - 12.6 Kb</a> 
</li><li><a href="http://www.codeproject.com/KB/IP/despop3client/despop3client_demo.zip">Download demo project - 15.8 Kb</a> </li></ul>
<h2>Introduction</h2>
<p>I was asked some time ago to develop software which involved
extracting bodies and subject lines from emails. "Humm ...", I thought,
"connect to mail server on 110, send POP3 commands, receive data,
sorted!". Indeed, at my first attempt it was a piece of cake: reading
emails - no problem. Colleagues working at my company were evangelizing
about what we could do: "Yeah mate, we can automatically process
emails, no sweat".</p>
<p>Clients would then ask more questions: "can we send it in rich text
or HTML?". "Yeah, sure we can!!". "What about processing them
automatically?". "Hey - you're talking to the email kings!!". "What
about processing multiple attachments, WAV's MP3's JPEG's?". "Ermmm ...
can I get back to you on that ...". Wasn't as easy as I'd thought ...</p>
<p>The reason why I found it quite difficult to code initially was
mainly due to how MIME is written and how extremely ugly it can look at
first glance. Here's a sample, which contains two multipart blocks
(I'll explain this later):</p><div class="SmallText" id="premain0" style="width: 100%; cursor: pointer;"><img preid="0" src="despop3client.aspx_files/minus.gif" id="preimg0" width="9" height="9"><span preid="0" style="margin-bottom: 0pt;" id="precollapse0"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre0" lang="text">Received: by Mailserver
        id <a href="http://www.codeproject.com/KB/IP/%22mailto:01C3EFF7.990BBDF0@TEST%22">01C3EFF7.990BBDF0@TEST</a>; Tue, 11 Feb 2003 17:02:00 -0000
Message-ID: <a href="http://www.codeproject.com/KB/IP/%22mailto:2CB86919E23ED51191840060080C3DAE02320B76@MAILSERVER%22">2CB86919E23ED51191840060080C3DAE02320B76@MAILSERVER</a>
From: Desmond McCarter
To: testemail
Subject: FW: my subject
Date: Tue, 11 May 2003 17:01:59 -0000
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="----_=_NextPart_000_01C3EFF7.990BD65A"</pre><div class="SmallText" id="premain1" style="width: 100%; cursor: pointer;"><img preid="1" src="despop3client.aspx_files/minus.gif" id="preimg1" width="9" height="9"><span preid="1" style="margin-bottom: 0pt;" id="precollapse1"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre1" lang="text">This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C3EFF7.990BD65A
Content-Type: text/plain;
        charset="iso-8859-1"
        
        
        
-----Original Message-----
From: Lisa Cleary [mailto:lisa@cleary.com]
Sent: 11 May 2003 16:17
To: 'Desmond McCarter'
Subject: RE: Test

------_=_NextPart_000_01C3EFF7.990BD65A
Content-Type: application/vnd.ms-excel;
        name="test.xls"
Content-Transfer-Encoding: base64</pre><div class="SmallText" id="premain2" style="width: 100%; cursor: pointer;"><img preid="2" src="despop3client.aspx_files/minus.gif" id="preimg2" width="9" height="9"><span preid="2" style="margin-bottom: 0pt;" id="precollapse2"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre2" lang="text">0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAEAAAAwQEAAAAAAAAA
EAAA/v///wAAAAD+////AAAAAL0BAAC+AQAAvwEAAMABAAD/////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////8J
CBAAAAYFAP4czQfJQAAABgEAAOEAAgCwBMEAAgAAAOIAAABcAHAADQAAV0ggU21pdGggTmV3cyAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIEIAAgCwBGEBAgAAAMABAAA9AQQA
AQD8AJwAAgAOABkAAgAAABIAAgAAABMAAgAAAK8BAgAAALwBAgAAAD0AEgAXB6b/WC+gIzgAAQAA
AAEAWAJAAAIAAACNAAIAAAAiAAIAAAAOAAIAAQC3AQIAAADaAAIAAAAxABoAyAAAAP9/kAEAAAAA
.</pre>
<hr>

<h2>MIME (Multipurpose Internet Mail Extensions): A quick and dirty guide</h2>
<p>Data that is transferred over the Internet is sent as a collection
of bytes (i.e. a collection of 8 bits). This information includes text
files, CSVs or even JPEGs or movies. "Hey" you might say "you can't
send binary data as a collection of bytes!". Yes you can, with a
suitable encoding scheme: using the base 64 algorithm for example
(check out the <code>System.Convert.ToBase64String</code> method in
your .NET framework). This information (we're talking in email context)
also includes the subject, body and forwarded items. For the client
(sending the email) and the server (reading the email) should
understand each other and, in order to do that, they must conform (send
and receive data) in MIME format.</p>
<p>In the snipped MIME example (above), you can see and easily understand the basic fields:</p>
<p><strong>"From:" - </strong>who sent the email, <strong>"To:"</strong> - who is receiving the email, <strong>"Subject:"</strong> - the subject of the email and <strong>"Date:"</strong> - the date/time the email was sent.</p>
<p>The <strong>"Content-Type:"</strong> determines what type of content
the email contains. In a simple text email (i.e. with no attachments)
this is normally "text/plain". You can see however (I hope you can
anyway) that this email actually contains an attachment: <i>test.xls</i>.
Emails that contain attachments have a MIME content type of
"multipart/mixed". This means that the email contains data sectioned
into multiple parts: the body and attachments (or in this case
"attachment") etc. The boundary
(boundary="----_=_NextPart_000_01C3EFF7.990BD65A") identifies where
these parts start and stop. The body in my example (and in most emails)
is the first part of this multipart email. The start of the body
identified at the first boundary declaration:</p><div class="SmallText" id="premain3" style="width: 100%; cursor: pointer;"><img preid="3" src="despop3client.aspx_files/minus.gif" id="preimg3" width="9" height="9"><span preid="3" style="margin-bottom: 0pt;" id="precollapse3"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre3" lang="text">------_=_NextPart_000_01C3EFF7.990BD65A
Content-Type: text/plain;
        charset="iso-8859-1"




-----Original Message-----
From: Lisa Cleary [mailto:lisa@cleary.com]
Sent: 11 May 2003 16:17
To: 'Desmond McCarter'
Subject: RE: Test</pre>
<p>You can also see from the above MIME text that this part also
contains its content type, i.e. the format of the body: text/plain. All
parts of a multipart email have their header definitions first, then an
empty line, then the actual body.</p>
<p>The next part of this multipart email is the attachment:</p><div class="SmallText" id="premain4" style="width: 100%; cursor: pointer;"><img preid="4" src="despop3client.aspx_files/minus.gif" id="preimg4" width="9" height="9"><span preid="4" style="margin-bottom: 0pt;" id="precollapse4"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre4" lang="text">------_=_NextPart_000_01C3EFF7.990BD65A
Content-Type: application/vnd.ms-excel;
        name="test.xls"
Content-Transfer-Encoding: base64

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAEAAAAwQEAAAAAAAAA
EAAA/v///wAAAAD+////AAAAAL0BAAC+AQAAvwEAAMABAAD/////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////8J
CBAAAAYFAP4czQfJQAAABgEAAOEAAgCwBMEAAgAAAOIAAABcAHAADQAAV0ggU21pdGggTmV3cyAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIEIAAgCwBGEBAgAAAMABAAA9AQQA
AQD8AJwAAgAOABkAAgAAABIAAgAAABMAAgAAAK8BAgAAALwBAgAAAD0AEgAXB6b/WC+gIzgAAQAA
AAEAWAJAAAIAAACNAAIAAAAiAAIAAAAOAAIAAQC3AQIAAADaAAIAAAAxABoAyAAAAP9/kAEAAAAA
.</pre>
<p>Note again, the second and final "multipart part" (i.e. the
attachment) start off with the boundary declaration. Also note that the
content type is defined, as well as the name and encoding scheme used
to convert the attachment, enabling it to be sent over the internet in
byte format. You need to take note that had this email had another
attachment, then the second attachment (the third "multipart part")
would start off with the same boundary declaration and so on.</p>
<h2>MIME is in fact object oriented</h2>
<p>The first mistake I made when building a POP3 library was to develop
it in a language that was unsuitable: C. It took too long to write and
did indeed get very dirty. It took about 3 weeks to develop and test my
library, whereas in C# it took a day and a half!! The reason for this
is that MIME, you can say, is an object oriented format: each part of a
multipart email (even the body of a simple text/plain mail + main
headers etc.) can be thought of as being objects. This is one of the
main reasons why I wrote it in C# (could have used Java or even J2EE
but ...).</p>
<h2>Code</h2>
<p>The code I have written starts off with a class called <code>Pop3Client</code>. This class is used to instantiate connection to a POP3 server:</p><div class="SmallText" id="premain5" style="width: 100%; cursor: pointer;"><img preid="5" src="despop3client.aspx_files/minus.gif" id="preimg5" width="9" height="9"><span preid="5" style="margin-bottom: 0pt;" id="precollapse5"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre5" lang="cs">Pop3Client email = <span class="code-keyword">new</span> Pop3Client(<span class="code-string">"</span><span class="code-string">user"</span>, <span class="code-string">"</span><span class="code-string">password"</span>, <span class="code-string">"</span><span class="code-string">mail.server.com"</span>);</pre>
<p>You then open the Inbox as follows:</p><div class="SmallText" id="premain6" style="width: 100%; cursor: pointer;"><img preid="6" src="despop3client.aspx_files/minus.gif" id="preimg6" width="9" height="9"><span preid="6" style="margin-bottom: 0pt;" id="precollapse6"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre6" lang="cs">email.OpenInbox();</pre>
<p>To go to the first email, then you call the <code>NextEmail()</code> method, which returns <code lang="cs"><span class="code-keyword">true</span></code> if there is a "next email" or <code lang="cs"><span class="code-keyword">false</span></code> if no such email exists. There is also a <code>IsMultipart</code>
singleton, which you can use to check and see whether the email has
multiple parts (i.e. attachments). Here's an example of how the code
might look:</p><div class="SmallText" id="premain7" style="width: 100%; cursor: pointer;"><img preid="7" src="despop3client.aspx_files/minus.gif" id="preimg7" width="9" height="9"><span preid="7" style="margin-bottom: 0pt;" id="precollapse7"> Collapse</span></div><pre style="margin-top: 0pt;" id="pre7" lang="cs"><span class="code-keyword">try</span> {
    Pop3Client email = <span class="code-keyword">new</span> Pop3Client(<span class="code-string">"</span><span class="code-string">user"</span>, <span class="code-string">"</span><span class="code-string">password"</span>, <span class="code-string">"</span><span class="code-string">mail.server.com"</span>);
    email.OpenInbox();

    <span class="code-keyword">while</span>( email.NextEmail())

    {
        <span class="code-keyword">if</span>(email.IsMultipart)
        {
            IEnumerator enumerator = email.MultipartEnumerator;
            <span class="code-keyword">while</span>(enumerator.MoveNext())
            {
                Pop3Component multipart = (Pop3Component)
                enumerator.Current;
                <span class="code-keyword">if</span>( multipart.IsBody )
                {
                    Console.WriteLine(<span class="code-string">"</span><span class="code-string">Multipart body:"</span>+
                    multipart.Body);
                }
                <span class="code-keyword">else</span>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -