Table of Contents
24.3.3 Class MimeMessage
The class MimeMessage (gb.mime) is always used together with the classes Mime and MimePart
- to generate the source text of an email from header and body or
- to split the source text of an email into header and body.
The class can be created. A new EMail of the type MimeMessage is created like this:
Dim hMimeMessage As MimeMessage hMimeMessage = New MimeMessage ( [ Contents As String ] )
- The content of the optional parameter 'Contents' determines the text in the mime message.
- If the parameter is missing, an empty mime message is generated.
24.3.3.1 Properties
The MimeMessage class has these properties, which are described in the following table.
| Property | Data type | Description |
|---|---|---|
| Sender | String | Sets the sender of the EMail or returns the sender. |
| To | String[] | Sets the recipient of the EMail or returns the recipient. |
| Cc | String[] | Sets additional recipients of the EMail in copy or returns these optional recipients. |
| Bcc | String[] | Sets further recipients of the EMail, but declared as hidden, or returns these optional recipients. |
| ReplyTo | String | Sets the ReplyTo header field or returns the ReplyTo header field. |
| Subject | String | Sets the subject of the email or returns the subject. |
| Id | String | Sets the ID of the EMail or returns the ID of the EMail. |
| Body | MimePart | Returns the content of the EMail. The content is text, for example in the encoding text/plain or text/html, into which multimedia objects in different encodings can optionally be inserted. However, this property ignores attachments. |
| Headers | .MimeMessage.Headers | MimeMessage.Headers[fieldname] returns a header field of an email defined via 'fieldname'. The virtual class 'MimeMessage.Headers' can be used like a read/write array. |
Table 24.3.3.1.1 : Properties of the MimeMessage class.
Comment:
- Using selected properties of the class MimeMessage you have direct read-write access to special fields in the header like To, From, Subject or Body, which are included in every EMai.
- You can read or set other header fields - such as the date in an EMail - using the property Headers[“fieldname”] = Headers[“Date”].
- You can only read or set the body part as text of the type MimePart (→ Chapter 24.3.2).
Example 1 - Setting the content of header fields
Use this source code snippet to set the value of a header field with the identifier 'FieldName':
Dim hMimeMessage As MimeMessage Dim sString As String hMimeMessage.Headers [ FieldName As String ] = sString
Example 2 - Reading the contents of header fields
To return the value of a header line (header field) with the identifier 'FieldName':
Dim hMimeMessage As MimeMessage Dim sString As String sString = hMimeMessage.Headers [ FieldName As String ]
24.3.3.2 Methods
The MimeMessage class has only one method. The ToString function assembles an email from its header and body parts:
Function ToString ( ) As String
24.3.3.3 Project
The following source code snippet allows you to read and display not only the contents of selected header fields but also body sub-header fields in an EMail source code. You will also receive information about the email attachments, if they exist. The information is preceded by an overview of the structure of the examined EMail:
Public Sub btnShowMimeMessageDetails_Click() Dim hMimeMessage As New MimeMessage Dim hMimePart, hMimePart2 As MimePart Dim sMessage, sEncoding As String Dim k As Integer = 1 hMimeMessage = New MimeMessage(txaMonitor.Text) $sBodyType = Scan(hMimeMessage.Part.Headers["Content-Type"], "*;*")[0] sMessage = ("D E T A I L S M I M E - M E S S A G E") txaMonitor.Insert(sMessage & gb.NewLine) txaMonitor.Insert(String$(String.Len(sMessage), "-") & gb.NewLine) txaMonitor.Insert(gb.NewLine) ' Parse mimemessage ... txaMonitor.Insert("+ " & $sBodyType & gb.NewLine) ParsePart(hMimeMessage.Body, True) ParsePart(hMimeMessage.Part, False) ' Show details ... txaMonitor.Insert(String$(80, "-") & gb.NewLine) txaMonitor.Insert(gb.NewLine) txaMonitor.Insert("CONTENT-TYPE EMAIL = " & Scan(hMimeMessage.Headers["Content-Type"], "*;*")[0] & gb.NewLine) txaMonitor.Insert("DATE = " & hMimeMessage.Headers["Date"] & gb.NewLine) txaMonitor.Insert("FROM = " & hMimeMessage.Sender & gb.NewLine) txaMonitor.Insert("RETURN-PATH = " & hMimeMessage.Headers["Return-Path"] & gb.NewLine) txaMonitor.Insert("SUBJECT = " & hMimeMessage.Subject & gb.NewLine) txaMonitor.Insert("TO = " & hMimeMessage.To & gb.NewLine) If hMimeMessage.Cc Then txaMonitor.Insert("CC = " & hMimeMessage.Cc & gb.NewLine) Endif ' txaMonitor.Insert("BCC = "; hMimeMessage.Bcc ' Blind means hidden ... txaMonitor.Insert("MESSAGE-ID = " & hMimeMessage.Id & gb.NewLine) txaMonitor.Insert("MIME-VERSION = " & hMimeMessage.Headers["MIME-Version"] & gb.NewLine) hMimePart = New MimePart hMimePart = hMimeMessage.Part hMimePart2 = New MimePart hMimePart2 = hMimeMessage.Part If Scan(hMimeMessage.Headers["Content-Type"], "*;*")[0] Like "*/mixed" Then ' : Attachment number >= 1 txaMonitor.Insert(gb.NewLine) sMessage = ("D E T A I L S F O R A T T A C H M E N T") txaMonitor.Insert(sMessage & gb.NewLine) txaMonitor.Insert(String$(String.Len(sMessage), "-") & gb.NewLine) txaMonitor.Insert(gb.NewLine) Endif For Each hMimePart2 In hMimePart If hMimePart2.Disposition = "attachment" Then txaMonitor.Insert(("Attachment ") & Str(k) & ":" & gb.NewLine) Inc k txaMonitor.Insert("Disposition = " & hMimePart2.Disposition & gb.NewLine) Select Case hMimePart2.ContentEncoding Case 0 sEncoding = "Default" Case 1 sEncoding = "7Bit" Case 2 sEncoding = "8Bit" Case 3 sEncoding = "Binary" Case 4 sEncoding = "Base64" Case 5 sEncoding = "QuotedPrintable" Case 6 sEncoding = "UUEncode" End Select txaMonitor.Insert("ContentEncoding = " & sEncoding & gb.NewLine) txaMonitor.Insert("ContentType = " & hMimePart2.ContentType & gb.NewLine) txaMonitor.Insert("FileName = " & hMimePart2.FileName & gb.NewLine) txaMonitor.Insert(gb.NewLine) Endif Next btnShowMimeMessageDetails.Enabled = False End ' btnShowMimeMessageDetails_Click()

