Provides properties and methods for presenting an e-mail message.
IDispatch
IMail
[Visual Basic 6.0] Public Class Mail
[Visual C++] public: interface Mail
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Public Properties
| Attachments | Gets an array of the attachments of the email message. This property is obsoleted by AttachmentList. |
| AttachmentList | Gets a collection of the attachments of the email message. |
| BodyHeaders | Gets the headers of the body part. |
| Cc | Gets an array of e-mail addresses that receive a carbon copy (CC) of the e-mail message. This property is obsoleted by CcList. |
| CcList | Gets a collection of e-mail addresses that receive a carbon copy (CC) of the e-mail message. |
| Content | Gets the content of the email message. |
| From | Gets the e-mail address of the sender. |
| Headers | Gets the HeaderCollection for headers of the e-mail message. |
| HtmlBody | Gets the html body of the e-mail message. |
| LicenseCode | Sets your Mail object license code. |
| IsEncrypted | Gets whether the email is encrypted by digital certificate. |
| IsReport | Gets whether the email is a delivery report. |
| IsSigned | Gets whether the email has a digital signature. |
| OriginalBodyFormat | Gets the body text format of the e-mail message. |
| Priority | Gets the priority of the e-mail message. |
| ReceivedDate | Gets the date time when the server received this email. |
| ReplyTo | Gets the email address to reply this email. |
| SentDate | Gets the date time when the sender sent this email. |
| Subject | Gets the subject of the e-mail message. |
| Tag | Gets or sets an object that contains data to associate with the Mail. |
| TextBody | Gets the text body of the e-mail message. |
| To | Gets an array of recipient e-mail addresses. This property is obsoleted by ToList. |
| ToList | Gets a collection of recipient e-mail addresses. |
Public Methods
| Clear | Clears all data in Mail class. |
| DecodeTNEF | Parses the winmail.dat (TNEF attachment) automatically in current mail instance. |
| Decrypt | Decrypts the encrypted email by digital certificate. |
| GetReport | Gets detailed information of a delivery report. |
| Load | Loads a RFC822 message binary email content to current Mail instance. |
| LoadOMSG | Loads a Outlook MSG (*.msg) binary email content to current Mail instance. |
| LoadOMSGFile | Loads a Outlook MSG (*.msg) file to current Mail instance. |
| LoadFile | Loads a RFC822 message (*.eml) file to current Mail instance. |
| ParseTNEF | Parses the winmail.dat (TNEF attachment). |
| SaveAs | Saves the e-mail message to a file (*.eml file). |
| SaveAsOMSG | Saves the e-mail message to a Outlook MSG file (*.msg file). |
| VerifySignature | Verifies the digital signature of the email. |
Example
[Visual Basic 6.0, VBScript, Visual C++] The following example demonstrates how to parse email with EAGetMail POP3 & IMAP4 ActiveX Object. To get the full samples of EAGetMail, please refer to Samples section.
[Visual Basic 6.0]
Private Function FormatHtmlTag(ByVal src As String) As String
src = Replace(src, ">", ">")
src = Replace(src, "<", "<")
FormatHtmlTag = src
End Function
' we parse the email file and generate a html + attachment folder for the email, once the html is create,
' you can open this html with IE and view the content and get the attachments.
Private Sub ParseEmailToHtml(emlFile As String)
On Error GoTo ErrorHandle
Dim pos
pos = InStrRev(emlFile, ".")
Dim mainName
Dim htmlName
mainName = Mid(emlFile, 1, pos - 1)
htmlName = mainName & ".htm"
Dim tempFolder As String
tempFolder = mainName
Dim oMail As New EAGetMailObjLib.Mail
'For evaluation usage, please use "TryIt" as the license code, otherwise the
'"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then
'"trial version expired" exception will be thrown.
oMail.LicenseCode = "TryIt"
oMail.LoadFile emlFile, False
If Err.Number <> 0 Then
MsgBox Err.Description
Exit Sub
End If
On Error Resume Next
If oMail.IsEncrypted Then
Set oMail = oMail.decrypt(Nothing)
If Err.Number <> 0 Then
MsgBox Err.Description
Err.Clear
End If
End If
If oMail.IsSigned Then
oMail.VerifySignature
If Err.Number <> 0 Then
MsgBox Err.Description
Err.Clear
End If
End If
On Error GoTo ErrorHandle
Dim oTools As New EAGetMailObjLib.Tools
' Decode winmail.dat (TNEF) automatically'
' also convert RTF body to HTML body automatically'
oMail.DecodeTNEF
Dim html As String
html = oMail.HtmlBody
Dim hdr As String
hdr = hdr & "<font face=""Courier New,Arial"" size=2>"
hdr = hdr & "<b>From:</b> " + FormatHtmlTag(oMail.From.Name & "<" & oMail.From.Address & ">") + "<br>"
Dim i, addressList
Set addressList = oMail.ToList
If (addressList.Count > 0) Then
hdr = hdr & "<b>To:</b> "
For i = 0 To addressList.Count - 1
hdr = hdr & FormatHtmlTag(addressList.Item(i).Name & "<" & addressList.Item(i).Address & ">")
hdr = hdr & ";"
Next
hdr = hdr & "<br>"
End If
Set addressList = oMail.CcList
If (addressList.Count > 0) Then
hdr = hdr & "<b>Cc:</b> "
For i = 0 To addressList.Count - 1
hdr = hdr & FormatHtmlTag(addressList.Item(i).Name & "<" & addressList.Item(i).Address & ">")
hdr = hdr & ";"
Next
hdr = hdr & "<br>"
End If
hdr = hdr & "<b>Subject:</b>" & FormatHtmlTag(oMail.Subject) & "<br>" & vbCrLf
Dim attList
Set attList = oMail.AttachmentList
If (attList.Count > 0) Then
If Not oTools.ExistFile(tempFolder) Then
oTools.CreateFolder (tempFolder)
End If
hdr = hdr & "<b>Attachments:</b>"
For i = 0 To attList.Count - 1
Dim att As Attachment
Set att = attList.Item(i)
Dim attname
attname = tempFolder & "\" & att.Name
att.SaveAs attname, True
hdr = hdr & "<a href=""" & attname & """ target=""_blank"">" & att.Name & "</a> "
If Len(att.ContentID) > 0 Then
'show embedded image.
html = Replace(html, "cid:" + att.ContentID, attname)
ElseIf InStr(1, att.ContentType, "image/", vbTextCompare) = 1 Then
'show attached image.
html = html & "<hr><img src=""" & attname & """>"
End If
Next
End If
hdr = "<meta http-equiv=""Content-Type"" content=""text-html; charset=utf-8"">" & hdr
html = hdr & "<hr>" & html
oTools.WriteTextFile htmlName, html, 65001
oMail.Clear
Exit Sub
ErrorHandle:
MsgBox "Failed to generate html file for the email; " & Err.Description
End Sub
[VBScript]
Function FormatHtmlTag(ByVal src)
src = Replace(src, ">", ">")
src = Replace(src, "<", "<")
FormatHtmlTag = src
End Function
' we parse the email file and generate a html + attachment folder for the email, once the html is create,
' you can open this html with IE and view the content and get the attachments.
Sub ParseEmailToHtml(ByVal emlFile)
Dim pos
pos = InStrRev(emlFile, ".")
Dim mainName
Dim htmlName
mainName = Mid(emlFile, 1, pos - 1)
htmlName = mainName & ".htm"
Dim tempFolder
tempFolder = mainName
Dim oMail
Set oMail = CreateObject("EAGetMailObj.Mail")
'For evaluation usage, please use "TryIt" as the license code, otherwise the
'"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then
'"trial version expired" exception will be thrown.
oMail.LicenseCode = "TryIt"
oMail.LoadFile emlFile, False
If oMail.IsEncrypted Then
Set oMail = oMail.decrypt(Nothing)
End If
If oMail.IsSigned Then
oMail.VerifySignature
End If
Dim oTools
Set oTools = CreateObject("EAGetMailObj.Tools")
' Decode winmail.dat (TNEF) automatically'
' also convert RTF body to HTML body automatically'
oMail.DecodeTNEF
Dim html
html = oMail.HtmlBody
Dim hdr
hdr = hdr & "<font face=""Courier New,Arial"" size=2>"
hdr = hdr & "<b>From:</b> " + FormatHtmlTag(oMail.From.Name & "<" & oMail.From.Address & ">") + "<br>"
Dim i, addressList
Set addressList = oMail.ToList
If (addressList.Count > 0) Then
hdr = hdr & "<b>To:</b> "
For i = 0 To addressList.Count - 1
hdr = hdr & FormatHtmlTag(addressList.Item(i).Name & "<" & addressList.Item(i).Address & ">")
hdr = hdr & ";"
Next
hdr = hdr & "<br>"
End If
Set addressList = oMail.CcList
If (addressList.Count > 0) Then
hdr = hdr & "<b>Cc:</b> "
For i = 0 To addressList.Count - 1
hdr = hdr & FormatHtmlTag(addressList.Item(i).Name & "<" & addressList.Item(i).Address & ">")
hdr = hdr & ";"
Next
hdr = hdr & "<br>"
End If
hdr = hdr & "<b>Subject:</b>" & FormatHtmlTag(oMail.Subject) & "<br>" & vbCrLf
Dim attList
Set attList = oMail.AttachmentList
If (attList.Count > 0) Then
If Not oTools.ExistFile(tempFolder) Then
oTools.CreateFolder (tempFolder)
End If
hdr = hdr & "<b>Attachments:</b>"
For i = 0 To attList.Count - 1
Dim att
Set att = attList.Item(i)
Dim attname
attname = tempFolder & "\" & att.Name
att.SaveAs attname, True
hdr = hdr & "<a href=""" & attname & """ target=""_blank"">" & att.Name & "</a> "
If Len(att.ContentID) > 0 Then
'show embedded image.
html = Replace(html, "cid:" + att.ContentID, attname)
ElseIf InStr(1, att.ContentType, "image/", vbTextCompare) = 1 Then
'show attached image.
html = html & "<hr><img src=""" & attname & """>"
End If
Next
End If
hdr = "<meta http-equiv=""Content-Type"" content=""text-html; charset=utf-8"">" & hdr
html = hdr & "<hr>" & html
oTools.WriteTextFile htmlName, html, 65001
oMail.Clear
End Sub
[Visual C++]
#include "stdafx.h"
#include <windows.h>
#include <atlstr.h>
#include "eagetmailobj.tlh"
using namespace EAGetMailObjLib;
CString _FormatHtmlTag(LPCTSTR lpszSrc)
{
CString src = lpszSrc;
src.Replace(_T(">"), _T(">"));
src.Replace(_T("<"), _T("<"));
return src;
}
// we parse the email file and generate a html + attachment folder for the email, once the html is create,
// you can open this html with IE and view the content and get the attachments.
void
ParseEmailToHtml(CString &emlFile)
{
try
{
int pos = emlFile.ReverseFind(_T('.'));
CString htmlFile = emlFile.Mid(0, pos) + _T(".htm");
CString attachmentFolder = emlFile.Mid(0, pos);
IMailPtr oMail;
oMail.CreateInstance(__uuidof(EAGetMailObjLib::Mail));
oMail->LicenseCode = _T("TryIt");
oMail->LoadFile(emlFile.GetString(), VARIANT_FALSE);
if (oMail->IsEncrypted == VARIANT_TRUE)
{
try
{
//this email is encrypted, we decrypt it by user default certificate.
// you can also use specified certificate like this
// oCert = new Certificate();
// oCert.Load("c:\\test.pfx", "pfxpassword", Certificate.CertificateKeyLocation.CRYPT_USER_KEYSET)
// oMail = oMail.Decrypt(oCert);
oMail = oMail->Decrypt(NULL);
}
catch (_com_error &ep)
{
_tprintf(_T("%s\r\n"), (TCHAR*)ep.Description());
oMail->LoadFile(emlFile.GetString(), VARIANT_FALSE);
}
}
if (oMail->IsSigned == VARIANT_TRUE)
{
try
{
//this email is digital signed.
ICertificatePtr oCert = oMail->VerifySignature();
_tprintf(_T("This email contains a valid digital signature.\r\n"));
//you can add the certificate to your certificate storage like this
//cert.AddToStore(Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER,
// "addressbook");
// then you can use send the encrypted email back to this sender.
}
catch (_com_error &ep)
{
_tprintf(_T("%s\r\n"), (TCHAR*)ep.Description());
}
}
// Decode winmail.dat (TNEF) automatically'
// also convert RTF body to HTML body automatically
oMail->DecodeTNEF();
CString html = (TCHAR*)oMail->HtmlBody;
CString header;
header.Preallocate(1024 * 5);
header.Append(_T("<font face=\"Courier New,Arial\" size=2>"));
header.Append(_T("<b>From:</b> "));
CString tempValue = (TCHAR*)oMail->From->Name;
tempValue += _T("<");
tempValue += (TCHAR*)oMail->From->Address;
tempValue += _T(">");
header.Append(_FormatHtmlTag(tempValue.GetString()));
header.Append(_T("<br>"));
IAddressCollectionPtr addressList = oMail->ToList;
if (addressList->Count > 0)
{
header.Append(_T("<b>To:</b> "));
for (long i = 0; i < addressList->Count; i++)
{
IMailAddressPtr pAddr = addressList->GetItem(i);
tempValue = (TCHAR*)pAddr->Name;
tempValue += _T("<");
tempValue += (TCHAR*)pAddr->Address;
tempValue += _T(">");
header.Append(_FormatHtmlTag(tempValue.GetString()));
header.Append(_T(";"));
}
header.Append(_T("<br>"));
}
addressList->Clear();
addressList = oMail->CcList;
if (addressList->Count > 0)
{
header.Append(_T("<b>Cc:</b> "));
for (long i = 0; i < addressList->Count; i++)
{
IMailAddressPtr pAddr = addressList->GetItem(i);
tempValue = (TCHAR*)pAddr->Name;
tempValue += _T("<");
tempValue += (TCHAR*)pAddr->Address;
tempValue += _T(">");
header.Append(_FormatHtmlTag(tempValue.GetString()));
header.Append(_T(";"));
}
header.Append(_T("<br>"));
}
header.Append(_T("<b>Subject:</b>"));
header.Append(_FormatHtmlTag((TCHAR*)oMail->Subject));
header.Append(_T("<br>"));
IAttachmentCollectionPtr attList = oMail->AttachmentList;
if (attList->Count > 0)
{
::CreateDirectory(attachmentFolder.GetString(), NULL);
header.Append(_T("<b>Attachments:</b>"));
for (long i = 0; i < attList->Count; i++)
{
IAttachmentPtr pAtt = attList->GetItem(i);
CString name = (TCHAR*)pAtt->Name;
CString attName = attachmentFolder;
attName.Append(_T("\\"));
attName.Append((TCHAR*)pAtt->Name);
pAtt->SaveAs(attName.GetString(), VARIANT_TRUE);
header.Append(_T("<a href=\""));
header.Append(attName);
header.Append(_T("\" target=\"_blank\">"));
header.Append((TCHAR*)pAtt->Name);
header.Append(_T("</a> "));
CString contentID = (TCHAR*)pAtt->ContentID;
CString contentType = (TCHAR*)pAtt->ContentType;
if (contentID.GetLength() > 0)
{
CString find = _T("cid:");
find.Append(contentID);
//show embedded image.
html.Replace(find, attName);
}
else if (_tcsnicmp(contentType.GetString(), _T("image/"), _tcslen(_T("image/"))) == 0)
{
//show attached image
html.Append(_T("<hr><img src=\""));
html.Append(attName);
html.Append(_T("\">"));
}
}
}
header.Insert(0, _T("<meta HTTP-EQUIV=\"Content-Type\" Content=\"text-html; charset=utf-8\">"));
html = header + "<hr>" + html;
IToolsPtr oTools;
oTools.CreateInstance(__uuidof(EAGetMailObjLib::Tools));
oTools->WriteTextFile(htmlFile.GetString(), html.GetString(), CP_UTF8);
}
catch (_com_error &exp)
{
_tprintf(_T("%s\r\n"), (TCHAR*)exp.Description());
}
}
[Delphi]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils, EAGetMailObjLib_TLB;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure ConvertMailToHtml(fileName: WideString);
procedure GenerateHtmlForEmail(emlFile: WideString; htmlFile: WideString; attachmentFolder: WideString);
function FormatAddresses(addresses: IAddressCollection; prefix: WideString): WideString;
public
{ Public declarations }
end;
const
CRYPT_MACHINE_KEYSET = 32;
CRYPT_USER_KEYSET = 4096;
CERT_SYSTEM_STORE_CURRENT_USER = 65536;
CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072;
Var
Form1: TForm1;
Implementation
{$R *.dfm}
procedure TForm1.ConvertMailToHtml(fileName: WideString);
var
htmlFile, attachmentFolder: WideString;
oTools: TTools;
begin
// Remove .eml extension
htmlFile := LeftStr(fileName, length(fileName) - 4) + '.htm';
attachmentFolder := LeftStr(fileName, length(fileName) - 4);
oTools := TTools.Create(Application);
if not oTools.ExistFile(htmlFile) then
begin
// We haven't generate the html for this email, generate it now.
GenerateHtmlForEmail(fileName, htmlFile, attachmentFolder);
end;
end;
procedure TForm1.GenerateHtmlForEmail(emlFile: WideString; htmlFile: WideString; attachmentFolder: WideString);
var
oMail: TMail;
oTools: TTools;
attachments: IAttachmentCollection;
oAttachment: IAttachment;
i: integer;
html, header, attachmentName: WideString;
begin
oTools := TTools.Create(Application);
oMail := TMail.Create(Application);
oMail.LicenseCode := 'TryIt';
oMail.LoadFile(emlFile, false);
try
if oMail.IsEncrypted then
oMail.Load(oMail.Decrypt(nil).Content);
except
on ep:Exception do
ShowMessage('Decrypt Error: ' + ep.Message);
end;
try
if oMail.IsSigned then
oMail.VerifySignature();
except
on ep:Exception do
ShowMessage('Verify Digital Signature Error: ' + ep.Message);
end;
// Decode winmail.dat (TNEF stream) automatically.
// also convert RTF body to HTML automatically.
oMail.DecodeTNEF();
html := oMail.HtmlBody;
header := header + '<font face="Courier New,Arial" size="2">';
header := header + '<b>From:</b> ' + oMail.From.Name + ' <' + oMail.From.Address + '>' + '<br>';
header := header + FormatAddresses(oMail.ToList, 'To');
header := header + FormatAddresses(oMail.CcList, 'Cc');
header := header + '<b>Subject:</b>' + oMail.Subject + '<br>' + #13#10;
// Parse attachment
attachments := oMail.AttachmentList;
if(attachments.Count > 0) then
begin
// Create a temporal folder to store attachments.
if not oTools.ExistFile(attachmentFolder) then
oTools.CreateFolder(attachmentFolder);
header := header + '<b>Attachments:</b> ';
for i:= 0 to attachments.Count - 1 do
begin
oAttachment := attachments.Item[i];
attachmentName := attachmentFolder + '\' + oAttachment.Name;
oAttachment.SaveAs(attachmentName, true);
header := header + '<a href="' + attachmentName + '" target="_blank">' + oAttachment.Name + '</a> ';
// Show embedded images
if oAttachment.ContentID <> '' then
begin
// StringReplace doesn't support some non-ascii characters very well.
html := StringReplace(html, 'cid:' + oAttachment.ContentID, attachmentName, [rfReplaceAll, rfIgnoreCase]);
end
end;
end;
header := '<meta http-equiv="Content-Type" content="text-html; charset=utf-8">' + header;
html := header + '<hr>' + html;
oTools.WriteTextFile(htmlFile, html, 65001);
end;
function TForm1.FormatAddresses(addresses: IAddressCollection; prefix: WideString): WideString;
var
value: WideString;
i: integer;
begin
if addresses.Count = 0 then
begin
result := '';
exit;
end;
value := '<b>' + prefix + ':</b> '; // To or Cc
for i := 0 to addresses.Count - 1 do
begin
if(addresses.Item[i].Name = '') then
value := value + '<' + addresses.Item[i].Address + '>'
else
value := value + addresses.Item[i].Name + ' <' + addresses.Item[i].Address + '>';
if (i < addresses.Count - 1) then
value := value + '; ';
end;
result := value + '<br>';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
ConvertMailToHtml('c:\my folder\test.eml');
except
on ep:Exception do
ShowMessage('Error: ' + ep.Message);
end;
end;
end.
See Also
Mail.From Property
Mail.ToList Property
Mail.CcList Property
Mail.TextBody Property
Mail.HtmlBody Property
Mail.AttachmentList Property
Online Tutorials
Parse Email in VB6 - Tutorial
Parse winmail.dat(TNEF) in VB6 - Tutorial
Parse Email in Delphi - Tutorial
Parse winmail.dat(TNEF) in Delphi - Tutorial
Parse Email in VC++- Tutorial
Parse winmail.dat(TNEF) in VC++ - Tutorial