如何在 Delphi 应用程序中阅读 HTML 格式的电子邮件?

发布于 2024-10-18 16:21:10 字数 1535 浏览 1 评论 0原文

我创建了一个可以从 Exchange 2007 读取电子邮件的程序。但是,它只能读取纯文本格式的电子邮件正文。当我尝试检索 HTML 格式的电子邮件时,我的软件无法读取正文并且始终为空白。我正在使用 Delphi 2007 和 IMAP 9。

更新:

这是我的代码:

procedure TForm1.tmrCekTimer(Sender: TObject);
var
  TheFlags: TIdMessageFlagsSet;
  TheUID: string;
  TheMsg: TIdMessage;
  MailBoxName: string;
  MyClass: TComponent;
begin
  MailBoxName := 'INBOX';
  if TheImap.SelectMailBox(MailBoxName) = False then
  begin
    Screen.Cursor := crDefault;
    ShowMessage('Error selecting '+MailBoxName);
    Exit;
  end;
  TheMsg := TIdMessage.Create(nil);
  nCount := TheImap.MailBox.TotalMsgs;
  TheMsg.ContentType := 'multipart/alternative';
  TheMsg.Encoding := meMime;
  if nCount = 0 then begin
    StringGrid1.RowCount := 2;
    StringGrid1.Cells[0, 1] := '';
    StringGrid1.Cells[1, 1] := '';
    StringGrid1.Cells[2, 1] := '';
    StringGrid1.Cells[3, 1] := '';
    ShowMessage('There are no messages in '+MailBoxName);
  end else begin
    StringGrid1.RowCount := nCount + 1;
    for i := 0 to nCount-1 do begin
      TheImap.GetUID(i+1, TheUID);
      TheImap.UIDRetrieveFlags(TheUID, TheFlags);
      TheImap.UIDRetrieve(TheUID, TheMsg);
      //TheImap.UIDRetrieveHeader(TheUID, TheMsg);
      StringGrid1.Cells[0, i+1] := IntToStr(i+1);
      StringGrid1.Cells[1, i+1] := TheMsg.From.Address;
      //StringGrid1.Cells[1, i+1] := TheUID;
      if mfSeen in TheFlags then
        StringGrid1.Cells[2, i+1] := 'Yes'
      else begin
        StringGrid1.Cells[2, i+1] := 'No';
      end;
    end;
 end;

I have created a program that can read email from Exchange 2007. However, it can only read the body of the email in plain-text format. When I tried to retrieve email in HTML format, my software cannot read the body and it always blank. I am using Delphi 2007 and IMAP 9.

Update:

Here is my code:

procedure TForm1.tmrCekTimer(Sender: TObject);
var
  TheFlags: TIdMessageFlagsSet;
  TheUID: string;
  TheMsg: TIdMessage;
  MailBoxName: string;
  MyClass: TComponent;
begin
  MailBoxName := 'INBOX';
  if TheImap.SelectMailBox(MailBoxName) = False then
  begin
    Screen.Cursor := crDefault;
    ShowMessage('Error selecting '+MailBoxName);
    Exit;
  end;
  TheMsg := TIdMessage.Create(nil);
  nCount := TheImap.MailBox.TotalMsgs;
  TheMsg.ContentType := 'multipart/alternative';
  TheMsg.Encoding := meMime;
  if nCount = 0 then begin
    StringGrid1.RowCount := 2;
    StringGrid1.Cells[0, 1] := '';
    StringGrid1.Cells[1, 1] := '';
    StringGrid1.Cells[2, 1] := '';
    StringGrid1.Cells[3, 1] := '';
    ShowMessage('There are no messages in '+MailBoxName);
  end else begin
    StringGrid1.RowCount := nCount + 1;
    for i := 0 to nCount-1 do begin
      TheImap.GetUID(i+1, TheUID);
      TheImap.UIDRetrieveFlags(TheUID, TheFlags);
      TheImap.UIDRetrieve(TheUID, TheMsg);
      //TheImap.UIDRetrieveHeader(TheUID, TheMsg);
      StringGrid1.Cells[0, i+1] := IntToStr(i+1);
      StringGrid1.Cells[1, i+1] := TheMsg.From.Address;
      //StringGrid1.Cells[1, i+1] := TheUID;
      if mfSeen in TheFlags then
        StringGrid1.Cells[2, i+1] := 'Yes'
      else begin
        StringGrid1.Cells[2, i+1] := 'No';
      end;
    end;
 end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月棠 2024-10-25 16:21:10

MIME 编码的电子邮件(例如 HTML 电子邮件)的内容(如果还存在纯文本和/或附件)存储在 TIdMessage.MessageParts 属性中,而不是存储在 TIdMessage.Body 中 属性。您需要查看电子邮件的实际 ContentType 属性,以了解 TIdMessage 将电子邮件解析为哪个属性。

The contents of MIME-encoded emails, such as HTML emails (if plain-text and/or attachments are also present) are stored in the TIdMessage.MessageParts property, not in the TIdMessage.Body property. You need to look at the email's actual ContentType property to know which property TIdMessage parsed the email into.

丑丑阿 2024-10-25 16:21:10

使用 MAPI,我通常尝试以字符串形式获取 PR_BODY_HTML 属性;如果为空,我将检索 PR_HTML 属性。

  const
    PR_HTML = $10130102;
    PR_BODY_HTML = $1013001E;

这通常对我有用。当然,也许您完全使用不同的技术,但您并没有给我们太多的帮助......

Using MAPI, I usually try to get the PR_BODY_HTML property as string; if that’s empty, I retrieve the PR_HTML property.

  const
    PR_HTML = $10130102;
    PR_BODY_HTML = $1013001E;

This usually works for me. Of course, maybe you’re using different technology altogether, but you’re not giving us much to work with...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文