User Tools

Site Tools


Sidebar

Network and communication

k24:k24.3:k24.3.2:start

24.3.2 Class MimePart

The class MimePart of the component gb.mime represents a part in the text body of a MimeMessage → chapter 24.3.3 Class MimeMessage. A blank line formally separates the header of an email from the body. You can create this class.

24.3.2.1 Properties

The MimePart class has the following properties:

PropertyData typeDescription
CountIntegerReturns the number of parts of a MimeMessage.
ContentDispositionStringSets the header field 'Content-Disposition' of a MimeMessage or returns its value.
ContentEncodingIntegerReturns the type of encoding of a part in a MimeMessage (7Bit, 8Bit, Base64, Binary, Default, QuotedPrintable, UUEncoding.
ContentTypeStringReturns the value of the header field 'ContentType' of a MimeMessage.
ContentIdStringSets the header field 'Content-ID' of a MimeMessage or returns its value.
DispositionStringSets the header field 'Disposition' of a MimeMessage or returns its value.
FileNameStringReturns the file name of the attachment specified in the header field 'Disposition' of a MimeMessage. If NULL is returned, then no file was specified.
MessageMimeMessageSets the message part within a MimePart structure or returns this message part.
DataStringReturns the automatically decoded user data of a part of a MimeMessage. Be sure to check the type of the data.
HeadersMimePart.HeadersMimePart.Headers[fieldname] Returns the specified header fields of a part.

Table 24.3.2.1.1 : Properties of the class MimePart

Example: source code excerpts of an email of type MimeMessage.

[1] ...
[2] Return-path: <xxxx.xx@freenet.de>
[3] ...
[4] To: xxxx.xx@freenet.de
[5] From: YYYY <yyy.zzz@freenet.de>
[6] Subject: Bild-Datei
[7] Message-ID: <56828856.4070805@freenet.de>
[8] Date: Tue, 29 Dec 2015 14:19:18 +0100
[9] ...
[10] MIME-Version: 1.0
[11] Content-Type: multipart/mixed; boundary="000100040309080505050507"
[12] ...
[13] <LEERZEILE TRENNT HEADER UND BODY>
[14] This is a multi-part message in MIME format.
[15] --000100040309080505050507
[16] Content-Type: multipart/alternative; boundary="030406090702040408090509"
[17]
[18] --030406090702040408090509
[19] Content-Type: text/plain; charset=utf-8; format=flowed
[20] Content-Transfer-Encoding: 7bit
[21]
[22] Hallo Hans,
[23] im Anhang die angeforderte Bild-Datei '8.png'.
[24] Mit freundlichem Gruss
[25] Honsek
[26]
[27] --030406090702040408090509
[28] Content-Type: text/html; charset=utf-8
[29] Content-Transfer-Encoding: 7bit
[30]
[31] <html>
[32]   <head>
[33]     <meta http-equiv="content-type" content="text/html; charset=utf-8">
[34]   </head>
[35]   <body text="#000000" bgcolor="#FFFFFF">
[36]     <font size="+1">Hallo Hans,<br>
[37]       im Anhang die angeforderte Bild-Datei '8.png'.<br>
[38]       Mit freundlichem Gruss<br>
[39]       Honsek<br>
[40]     </font>
[41]   </body>
[42] </html>
[43]
[44] --030406090702040408090509--
[45]
[46] --000100040309080505050507
[47] Content-Type: image/png; name="8.png"
[48] Content-Transfer-Encoding: base64
[49] Content-Disposition: attachment; filename="8.png"
[50]
[51] iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAO0lEQVQYlX2PyQ0AMAjDnKr7
[52] r5x+egvIEyxwZDBFOoASxJoAEWUB0J6Zhedi5QFSh/0lkDnAd/pWK2sORyMOEhaL7BAAAAAA
[53] SUVORK5CYII=
[54]
[55] --000100040309080505050507--
Struktur:

+ multipart/mixed
|
+-+ multipart/alternative  2
| |
| +-- text/plain  0
| |
| +-- text/html  0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ANHANG 1 :  8.png attachment image/png

Figure 24.3.2.1.1: Representation of the structure of the above MimeMessage.

A parser outputs the structure of a MimeMessage. All parts of the MimeMessage and their hierarchy are represented symbolically.

  • In Figure 24.3.2.1.1 you can see the structure of an e-mail source text, which in the example consists of several parts (type: multipart/mixed) - a text part and an attachment part.
  • The text part consists of two texts (type: multipart/alternative) with the same content, but with different, alternative formats. The first text is of type text/plain and the second is of type text/html. The text parts are not further nested.
  • Attached is the image 8.png of type image/png.

Comment:

  • In lines 1 to 55 you see an excerpt from the source text of an email. In contrast to the structure displayed below, you see the concrete content.
  • The delimitations of the individual parts - the so-called boundary - are highlighted in colour. Each new boundary begins with two hyphens followed by the value of the boundary. The end of a part is marked by -valueoftheboundary–. Each new delimitation is followed by a separate (sub-)header with different header fields. An example of this is shown in line 46 and lines 47 to 49.

24.3.2.2 Methods

The MimePart class has only two methods:

MethodDescription
Add ( Part As MimePart )Adds 'Part' as part of the current MimeMessage.
ToString ( ) The function adds all parts created with the Add method to a MimeMessage.

Table 24.3.2.2.1 : Methods of the class MimePart

A description of the use of the two methods can be found in a project in chapter 24.4.3.

24.3.3 Example Structure Parser

The presented parser creates a view on the structure of a MimeMessage. The header and the individual parts in the body of a MimeMessage are examined:

B1
Figure 24.3.3.1: Illustration 2 of the structure of another MimeMessage

The source code for the parser is given in full:

[1] ' Gambas class file
[2]
[3] Private $iLevel As Integer
[4] Private $iFlag As Integer
[5] Private $sBodyType As String
[6] Private $iAttachmentCount As Integer
[7]
[8] Public Sub Form_Open()
[9]   FMain.Center
[10]   FMain.Utility = True
[11]   btnGetStructureMimeMessage.Enabled = False
[12] End ' Form_Open()
[13]
[14] Public Sub btnClose_Click()
[15]   FMain.Close
[16] End ' btnClose_Click()
[17]
[18] Public Sub btnGetStructureMimeMessage_Click()
[19]   Dim hMimeMessage As New MimeMessage
[20]   Dim sMessage As String
[21]
[22]   hMimeMessage = New MimeMessage(txaMonitor.Text)
[23]
[24]   $sBodyType = Scan(hMimeMessage.Part.Headers["Content-Type"], "*;*")[0]
[25]
[26]   sMessage = ("S T R U C T U R E  M I M E - M E S S A G E")
[27]   txaMonitor.Insert(sMessage & gb.NewLine)
[28]   txaMonitor.Insert(String$(String.Len(sMessage), "-") & gb.NewLine)
[29]   txaMonitor.Insert(gb.NewLine)
[30]
[31] ' Parser
[32]   txaMonitor.Insert("+ " & $sBodyType & gb.NewLine)
[33]   ParsePart(hMimeMessage.Body, True)
[34]   ParsePart(hMimeMessage.Part, False)
[35]
[36]   btnGetStructureMimeMessage.Enabled = False
[37]
[38] End ' btnGetStructureMimeMessage_Click()
[39]
[40] Public Sub ParsePart(hPart As MimePart, IsBody As Boolean)
[41]    Dim hChild As MimePart
[42]    Dim sMessage As String
[43]
[44]    If IsBody = True Then
[45]       txaMonitor.Insert(String$($iLevel, "| ") & "| " & gb.NewLine)
[46]       txaMonitor.Insert(String$($iLevel, "| ") & "+-" & If(hPart.Count, "+ ", "- "))
[47]       sMessage = hPart.ContentType & " " & hPart.FileName
[48]       sMessage &= " " & hPart.Count & gb.NewLine
[49]       txaMonitor.Insert(sMessage)
[50]    Else
[51]       If hPart.Disposition = "attachment" And Str(hPart.Count) = 0 Then
[52]          If $iFlag = 0 Then
[53]             txaMonitor.Insert("|" & gb.NewLine)
[54]             Inc $iFlag
[55]          Endif
[56]          sMessage = ("+ Attachment ") & Str($iAttachmentCount + 1) & ": " & " " & hPart.FileName
[57]          sMessage &= " " & hPart.Disposition & " " & hPart.ContentType & gb.NewLine
[58]          txaMonitor.Insert(sMessage)
[59]          Inc $iAttachmentCount
[60]       Endif
[61]    Endif ' IsBody ?
[62]
[63]    Inc $iLevel
[64]      For Each hChild In hPart
[65]          ParsePart(hChild, IsBody) ' Recursive call!
[66]      Next
[67]    Dec $iLevel
[68]
[69]    txaMonitor.Pos = txaMonitor.Length
[70]
[71] End ' ParsePart(...)
[72]
[73] Public Sub OpenMimeMessageFile_Click()
[74]   btnGetStructureMimeMessage.Enabled = False
[75]   Dialog.Title = ("Select a MimeMessage-File ...")
[76]   Dialog.Filter = ["*.txt", ("Text files"), "*", ("All files")]
[77]   Dialog.ShowHidden = False
[78]   Dialog.Path = Application.Path &/ "InBox"
[79]   If Dialog.Openfile(False) Then Return ' False: 'Multiselect' ist ausgeschaltet
[80]
[81]   txaMonitor.Clear
[82]   txaMonitor.Text = File.Load(Dialog.Path)
[83]
[84]   btnGetStructureMimeMessage.Enabled = True
[85]   $iLevel = 0
[86]   $iFlag = 0
[87]   $iAttachmentCount = 0
[88]
[89]   Catch
[90]   Message.Info(Error.Text)
[91]   btnGetStructureMimeMessage.Enabled = True
[92]
[93] End ' OpenMimeMessageFile_Click()

Comment:

  • The actual parser is described in the procedure ParsePart(hPart As MimePart, IsBody As Boolean).
  • You pass the email source text as MimeMessage and the parameter IsBody to the parser. The second parameter is necessary to distinguish between a text part of the MimeMessage (which for the mime type text/html can optionally also contain (encoded) images) and the attachments.
  • Note the recursive call of the parser in line 65.

Minisini wrote in this context: Other than that, a MIME message is a recursive structure. Normally mail clients hide that by rendering the message content linearly. So you have to parse the parts recursively … .

For your own structure analyses, you need the source texts of e-mails.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.3/k24.3.2/start.txt · Last modified: 16.08.2022 (external edit)

Page Tools