DELPHI中的电子邮件中的HTML [英] HTML inside an Email in DELPHI

查看:149
本文介绍了DELPHI中的电子邮件中的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过电子邮件直接发送HTML模板,无需复制粘贴页面,我的意思是将HTML代码发送为HTML页面而不是原始文本,主要原因是发送此HTML模板通过Delphi。



问题是我们只收到HTML中的文本,我的意思是这个部分:

 < p> Hola chicos!< / p> 
< p> Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。 Ut im im。。。。。。。。。。。。。。。。。。。。。。。。 Duis aute irure dolor in reprehenderit in featate velit esse cillum dolore eu fugiat nulla pariatur。异教徒圣诞节,不是亲戚,在屁股,; unt unt unt unt unt unt unt unt。。。。。。。。。。;;;;;;;

我无法得到它显示这些行:

 < table width =800border =0align =centercellpadding =0cellspacing =0background =http:// www.feelmalagahostel.com/emails/content.jpg\"> 
< table width =800border =0cellspacing =0cellpadding =0background =http://www.feelmalagahostel.com/emails/header_feel_summer.jpgstyle =background -position:top; background-repeat:no-repeat;>
< table width =800border =0align =centercellpadding =0cellspacing =0background =http://www.feelmalagahostel.com/emails/footer.jpg style =background-position:bottom; background-repeat:no-repeat;>

只是省略它们,显示标签图像,而不是背景图片。



所以这个问题我该怎么做才能在电子邮件中显示?并需要背景图像。



HTML代码:

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< body marginheight =0marginwidth =0>
< table width =800border =0align =centercellpadding =0cellspacing =0background =http://www.feelmalagahostel.com/emails/content.jpg >
< tr>
< td valign =top>
< table width =800border =0cellspacing =0cellpadding =0background =http://www.feelmalagahostel.com/emails/header_feel_summer.jpgstyle =background -position:top; background-repeat:no-repeat;>
< tr>
< td align =centervalign =top>
<! - header - >
< table width =730border =0align =centercellpadding =0cellspacing =0>
< tr>
< td height =230>& nbsp;< / td>
< / tr>
< tr>
< td height =100align =rightvalign =top>
< table border =0cellspacing =0cellpadding =0>
< tr>< / tr>
< / table>
< / td>
< / tr>
< tr>
< td height =10>& nbsp;< / td>
< / tr>
< tr>
< td height =61align =right>
< img src =https://www.auto21.ca/userImages/images/test%20drive%20logo%20FINAL.jpgwidth =347height =61/>
< / td>
< / tr>
< / table>
<! - 结束标题 - >
< br />
< br />
<! - content - >
< table width =600border =0cellspacing =0cellpadding =0align =center>
< tr>
< td valign =top> < font face =Arial,Helvetica,sans-serifsize =2>
< p> Hola chicos!< / p>
< p> Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。 Ut im im。。。。。。。。。。。。。。。。。。。。。。。。 Duis aute irure dolor in reprehenderit in featate velit esse cillum dolore eu fugiat nulla pariatur。异教徒圣诞节,不是亲戚,在屁股,; unt unt unt unt unt unt unt unt。。。。。。。。。。;;;;;;;
< p> Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,totam rem aperiam,eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vita dicta sunt explicabo。 Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit,sed quia dueuntur magni dolores eos qui ratione voluptatem sequi nesciunt。 ;;; am
< p> velit,sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem。 Ut im????????????????????????????? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae dueatur,vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?< / p>
< / font>
< / td>
< / tr>
< / table>
<! - 最终内容 - >
< / td>
< / tr>
< / table>
< / td>
< / tr>
< / table>
< table width =800border =0align =centercellpadding =0cellspacing =0background =http://www.feelmalagahostel.com/emails/footer.jpg style =background-position:bottom; background-repeat:no-repeat;>
< tr>
< td valign =top>
<! - footer - >
< table width =600border =0cellspacing =0cellpadding =0align =center>
< tr>
< td height =300valign =top>< / td>
< / tr>
< tr height =250>
< td>< / td>
< / tr>
< / table>
<! - 结束页脚 - >
< / td>
< / tr>
< / table>
< / body>

< / html>


解决方案

有很多方法可以使用Delphi发送电子邮件。 / p>

我最喜欢的是通过SMTP类 TIdSMTP ,作为 INDY项目的一部分 ,这是灵活和强大的。



要发送带有HTML内容的邮件使用辅助类 TIdMessageBuilderHtml ,其中在我看来,不仅使事情变得更容易,而且留下了更加优雅和易于理解的代码。



发送此类电子邮件的相关部分是:

  var 
Builder:TIdMessageBuilderHtml;
消息:TIdMessage;
SMTP:TIdSMTP;
begin
Builder:= TIdMessageBuilderHtml.Create;
try
Builder.Html.Add('< HTML>< HEAD>< TITLE>您的图片< / TITLE>< / HEAD>< BODY>');
Builder.Html.Add('< p>< strong>您的图片< / strong>< / p>');
Builder.Html.Add('< div>');
Builder.Html.Add('< img border =0src =cid:theimage>');
Builder.Html.Add('< / div>');
Builder.PlainText.Add('这个邮件有你的图像'
+'使用HTML查看器来看它');
Builder.HtmlFiles.Add(ImageFileName,'theimage');
Msg:= Builder.NewMessage();
try
Msg.Recipients.EMailAddresses:= eRecipients.Text;
Msg.From.Name:= eFromName.Text;
Msg.From.Address:= eFromEmail.Text;
Msg.Subject:='A image for you';
SMTP:= TIdSMTP.Create;
尝试
SMTP.Host:= eSMTPHost.Text;
SMTP.Username:= eSMTPUser.Text;
SMTP.Password:= eSMTPPass.Text;
SMTP.Connect;
try
SMTP.Send(Msg);
finally
SMTP.Disconnect;
结束
SMTP.Send(Msg);
finally
SMTP.Free;
结束
finally
Msg.Free;
结束
finally
Builder.Free;
结束
结束


Im trying to send HTML template via E-Mail directly without Copy-Paste of the Page, i mean to just put the HTML code and send it as that HTML page not Raw Text, the main reason is to send this HTML template via Delphi.

The problem is we get only the text from the HTML, i mean only this part:

<p>Hola chicos!</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

and what i cant get it to show are these lines :

<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" background="http://www.feelmalagahostel.com/emails/content.jpg">
<table width="800" border="0" cellspacing="0" cellpadding="0" background="http://www.feelmalagahostel.com/emails/header_feel_summer.jpg" style="background-position:top; background-repeat:no-repeat;">
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" background="http://www.feelmalagahostel.com/emails/footer.jpg" style="background-position:bottom; background-repeat:no-repeat;">

It just omits them, Shows the tag images and not the background ones.

so thats the question how do i do it to show in an Email? and need the background images.

the HTML code:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<body marginheight="0" marginwidth="0">
    <table width="800" border="0" align="center" cellpadding="0" cellspacing="0" background="http://www.feelmalagahostel.com/emails/content.jpg">
        <tr>
            <td valign="top">
                <table width="800" border="0" cellspacing="0" cellpadding="0" background="http://www.feelmalagahostel.com/emails/header_feel_summer.jpg" style="background-position:top; background-repeat:no-repeat;">
                    <tr>
                        <td align="center" valign="top">
                            <!--header-->
                            <table width="730" border="0" align="center" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td height="230">&nbsp;</td>
                                </tr>
                                <tr>
                                    <td height="100" align="right" valign="top">
                                        <table border="0" cellspacing="0" cellpadding="0">
                                            <tr></tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td height="10">&nbsp;</td>
                                </tr>
                                <tr>
                                    <td height="61" align="right">
                                        <img src="https://www.auto21.ca/userImages/images/test%20drive%20logo%20FINAL.jpg" width="347" height="61" />
                                    </td>
                                </tr>
                            </table>
                            <!--end header-->
                            <br/>
                            <br/>
                            <!--content-->
                            <table width="600" border="0" cellspacing="0" cellpadding="0" align="center">
                                <tr>
                                    <td valign="top"> <font face="Arial, Helvetica, sans-serif" size="2">
    <p>Hola chicos!</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci </p>
    <p>velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
    </font> 
                                    </td>
                                </tr>
                            </table>
                            <!--end content-->
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <table width="800" border="0" align="center" cellpadding="0" cellspacing="0" background="http://www.feelmalagahostel.com/emails/footer.jpg" style="background-position:bottom; background-repeat:no-repeat;">
        <tr>
            <td valign="top">
                <!--footer-->
                <table width="600" border="0" cellspacing="0" cellpadding="0" align="center">
                    <tr>
                        <td height="300" valign="top"></td>
                    </tr>
                    <tr height="250">
                        <td></td>
                    </tr>
                </table>
                <!--end footer-->
            </td>
        </tr>
    </table>
</body>

</html>

解决方案

There are many ways to send emails with Delphi.

My favorite is to do it via SMTP class TIdSMTP, part of the INDY Project, which is flexible and powerful.

To send a mail with HTML content use of the auxiliary class TIdMessageBuilderHtml, which in my opinion not only makes things easier but that leaves a much more elegant and easy to understand code.

The relevant part to send such email is:

var
  Builder: TIdMessageBuilderHtml;
  Msg: TIdMessage;
  SMTP: TIdSMTP;
begin
  Builder := TIdMessageBuilderHtml.Create;
  try
    Builder.Html.Add('<HTML><HEAD><TITLE>An Image for you</TITLE></HEAD><BODY>');
    Builder.Html.Add('<p><strong>An image for you</strong></p>');
    Builder.Html.Add('<div>');
    Builder.Html.Add('  <img border="0" src="cid:theimage" >');
    Builder.Html.Add('</div>');
    Builder.PlainText.Add('This mail have an image for you. '
      + 'Use a HTML viewer to see it');
    Builder.HtmlFiles.Add(ImageFileName, 'theimage');
    Msg := Builder.NewMessage();
    try
      Msg.Recipients.EMailAddresses := eRecipients.Text;
      Msg.From.Name := eFromName.Text;
      Msg.From.Address := eFromEmail.Text;
      Msg.Subject := 'A image for you';
      SMTP := TIdSMTP.Create;
      try
        SMTP.Host := eSMTPHost.Text;
        SMTP.Username := eSMTPUser.Text;
        SMTP.Password := eSMTPPass.Text;
        SMTP.Connect;
        try
          SMTP.Send(Msg);
        finally
          SMTP.Disconnect;
        end;
        SMTP.Send(Msg);
      finally
        SMTP.Free;
      end;
    finally
      Msg.Free;
    end;
  finally
    Builder.Free;
  end;
end;

这篇关于DELPHI中的电子邮件中的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆