3

我有一个 Delphi 6 应用程序,它生成电子邮件,我发送到我的 Evernote 电子邮件地址,这是一个用于通过电子邮件发送文档的特殊电子邮件地址,以便它们自动存储到我的 Evernote 帐户中。

我已经使用 Indy 9.x TIdSMTP组件成功创建了 HTML 文档并将它们发送到我的 Evernote 电子邮件地址。我将Content-Type设置为' text/html '。只要我不向电子邮件添加任何附件,它就可以正常工作。一旦我添加了附件,关于生成的电子邮件的某些内容就会使 Evernote 网络界面将电子邮件解释为原始 HTML. 换句话说,我在文档显示区域中看到原始 HTML,就好像我在浏览器中完成了“查看源代码”一样,而不是看到呈现的网页。我添加的电子邮件附件是一个 AVI 文件和一个 WAV 文件(如果重要的话)。当我添加附件时,它们都正确显示在 Evernote 网络显示区域的电子邮件底部。

重复一遍,只要我不添加附件,文档就会在 Evernote 网络界面中显示为漂亮的网页。如果我添加附件,我会看到原始 HTML。谁能建议我可以尝试解决此问题的方法?我在下面附上了我用来将生成的文档发送到我的 Evernote 电子邮件地址的代码。名为body的变量包含一个完全格式化的 HTML 文档。

更新:我将电子邮件发送到非 Evernote 电子邮件地址,以便我可以看到原始电子邮件消息。我发现添加附件会使 TIdSMTP 将它生成的多部分电子邮件的第一部分的 Content-Type 更改回“text/plain”,尽管事实上我在代码中将其设置为“text/html”时我创建消息。我将查看 Indy 的源代码,看看我是否能找出问题所在。

function easySendEmail(
                theIdSmtp               : TIdSmtp;
                destEMailAddress        : string;
                subject                 : string;
                body                    : string;
                emailServerSettings     : TEmailServerSettingsRecord;
                aryAttachmentFilenames  : TDynamicStringArray;
                connectTimeOut_ms       : integer;
                bUseEHLO                : boolean;
                authLoginType           : TAuthenticationType): boolean;
var
    IdMsg: TIdMessage;
    aryAttachments: TDynamicIdAttachmentArray;
    i: integer;
begin
    aryAttachments := nil;
    IdMsg := nil;

    destEMailAddress := Trim(destEMailAddress);

    if destEMailAddress = '' then
       raise Exception.Create('(TframeEmailServerSettings.easySendEmail) The destination E-mail address is empty.');

    subject := Trim(subject);

    if subject = '' then
        raise Exception.Create('(TframeEmailServerSettings.easySendEmail) The subject line is empty.');

    body := Trim(body);

    if body = '' then
        raise Exception.Create('(TframeEmailServerSettings.easySendEmail) The message body is empty.');

    try
        with emailServerSettings do
        begin
            // Build a test message and send it.
            IdMsg := TIdMessage.Create(nil);
            IdMsg.Recipients.EMailAddresses := destEMailAddress;
            {
                Most SMTP servers require the sending E-mail address as the
                user name for the authentication.  However, if we
                encounter one that doesn't work this way then re-using
                the authentication user name as the From address
                will not work.
            }
            IdMsg.From.Name    := APPLICATION_NAME_EVERMAIL;
            IdMsg.From.Address := user_name;
            IdMsg.Subject := subject;
            IdMsg.Body.Text := body;

            IdMsg.ContentType :=  'text/html';
            // IdMsg.ContentType :=  'text/plain';

            theIdSmtp.Host := host;
            theIdSmtp.Username := user_name;
            theIdSmtp.Password := password;
            theIdSmtp.Port := port_number;
            // Use EHLO method.
            theIdSmtp.UseEhlo := true;
            // Login method of authentication.
            theIdSmtp.AuthenticationType := atLogin;

            // Add the attachments.

            // >>> If I comment out the code below the document shows
            //  up as a rendered web page in the Evernote web interface.
            //  If I uncomment it and therefore add attachments, the
            //  document shows up as raw HTML.
            {
            if Length(aryAttachmentFilenames) > 0 then
            begin
                SetLength(aryAttachments, Length(aryAttachmentFilenames));

                for i := Low(aryAttachmentFilenames) to High(aryAttachmentFilenames) do
                    // Add each attachment.
                    aryAttachments[i] := TIdAttachment.Create(IdMsg.MessageParts, aryAttachmentFilenames[i]);
            end; // if Length(aryAttachmentFilenames) > 0 then
            }

            // Connect to the desired SMTP server.  N second time-out.
            theIdSmtp.Connect(connectTimeOut_ms);

            // Send it.
            theIdSmtp.Send(IdMsg);

            // If we got here than the test succeeded.  Set the flag
            //  indicating the current settings are valid.
            Result := true;
        end; // with mergeEditsWithOriginal do

    finally
        theIdSmtp.Disconnect;

        if Assigned(IdMsg) then
            IdMsg.Free;
    end; // try
end;
4

1 回答 1

6

存在附件时,您的代码设置不TIdMessage正确。试试这个:

function easySendEmail( 
                theIdSmtp               : TIdSmtp; 
                destEMailAddress        : string; 
                theSubject              : string; 
                theBody                 : string; 
                emailServerSettings     : TEmailServerSettingsRecord; 
                aryAttachmentFilenames  : TDynamicStringArray; 
                connectTimeOut_ms       : integer; 
                bUseEHLO                : boolean; 
                authLoginType           : TAuthenticationType): boolean; 
var 
  IdMsg: TIdMessage; 
  i: integer; 
begin 
  destEMailAddress := Trim(destEMailAddress); 
  if destEMailAddress = '' then 
    raise Exception.Create('(TframeEmailServerSettings.easySendEmail) The destination E-mail address is empty.'); 

  theSubject := Trim(theSubject); 
  if theSubject = '' then 
    raise Exception.Create('(TframeEmailServerSettings.easySendEmail) The subject line is empty.'); 

  theBody := Trim(theBody); 
  if theBody = '' then 
    raise Exception.Create('(TframeEmailServerSettings.easySendEmail) The message body is empty.'); 

  IdMsg := TIdMessage.Create(nil); 
  try 
    with emailServerSettings do 
    begin 
      // Build a test message and send it. 
      IdMsg.Recipients.EMailAddresses := destEMailAddress; 
      { 
        Most SMTP servers require the sending E-mail address as the 
        user name for the authentication.  However, if we 
        encounter one that doesn't work this way then re-using 
        the authentication user name as the From address 
        will not work. 
      } 
      IdMsg.From.Name    := APPLICATION_NAME_EVERMAIL; 
      IdMsg.From.Address := user_name; 
      IdMsg.Subject := theSubject; 

      // Add the attachments. 
      if Length(aryAttachmentFilenames) > 0 then 
      begin 
        with TIdText.Create(IdMsg.MessageParts, nil) do
        begin
          Body.Text := 'An HTML viewer is required to see this message'; 
          ContentType := 'text/plain';
        end; 

        with TIdText.Create(IdMsg.MessageParts, nil) do
        begin
          Body.Text := theBody; 
          ContentType := 'text/html';
        end; 

        // Add each attachment. 
        for i := Low(aryAttachmentFilenames) to High(aryAttachmentFilenames) do 
          TIdAttachment.Create(IdMsg.MessageParts, aryAttachmentFilenames[i]); 

        IdMsg.ContentType := 'multipart/mixed'; 
      end else
      begin
        IdMsg.Body.Text := theBody; 
        IdMsg.ContentType := 'text/html'; 
      end; // if Length(aryAttachmentFilenames) > 0 then 

      theIdSmtp.Host := host; 
      theIdSmtp.Username := user_name; 
      theIdSmtp.Password := password; 
      theIdSmtp.Port := port_number; 
      // Use EHLO method. 
      theIdSmtp.UseEhlo := true; 
      // Login method of authentication. 
      theIdSmtp.AuthenticationType := atLogin; 

      // Connect to the desired SMTP server.  N second time-out. 
      theIdSmtp.Connect(connectTimeOut_ms); 
      try
        // Send it. 
        theIdSmtp.Send(IdMsg); 

        // If we got here than the test succeeded.  Set the flag 
        //  indicating the current settings are valid. 
        Result := true; 

      finally
        theIdSmtp.Disconnect; 
      end;
    end; // with emailServerSettings do 
  finally 
    IdMsg.Free; 
  end; // try 
end; 
于 2012-06-05T18:46:49.293 回答