indy10发送网页

本文介绍了如何使用Delphi2009和Indy10库发送带有图片的HTML邮件,包括设置内容ID、添加附件以及设置邮件各项属性的详细步骤。

KeyLife富翁笔记

作者 : lqcros
标题 : 使用indy10发送网页
关键字:
分类 : 个人专区
密级 : 公开

 

delphi2009 indy10通过
procedure TformMain.butnSendMailClick(Sender: TObject);
var
  html: TStrings;
  filename: string;
begin
  filename := ExtractFilePath(Application.ExeName) + 'YouImg.jpg';
  html := TStringList.Create;
  html.Add('');
  html.Add('');
  html.Add('');
  html.Add('

Hello

');
  html.Add(' '); //这里这个CID是什么意思,还没弄明白。
  html.Add('This is a picture!');
  html.Add('');
  butnSendMail.Enabled := False;
  try
    with mesgMessage do begin
      Clear;
      From.Text := Trim(editFrom.Text);
      Recipients.EMailAddresses := Trim(editTo.Text);
      Subject := Trim(editSubject.Text);
    end;
    with TIdText.Create(mesgMessage.MessageParts, nil) do begin
      ContentType := 'multipart/alternative';
//      ParentPart :=-1;
    end;
    with TIdText.Create(mesgMessage.MessageParts, nil) do begin
      Body.Text := html.Text;
      ContentType := 'text/html';
//      ParentPart := 0;
    end;
    with TIdAttachmentFile.Create(mesgMessage.MessageParts, filename) do begin
      ContentID := filename;
      ContentType := 'image/*';
      ContentDisposition := 'inline';
//      ParentPart := 0;
      ExtraHeaders.Values['content-id'] := ExtractFileName(Filename);
      DisplayName := ExtractFileName(Filename);
    end;
  mesgMessage.ContentType:='multipart/mixed';
//  mesgMessage.SaveToFile('1.txt');
    with smtpSendMail do begin
      Host := Trim(editSMTPServer.Text);
      Connect;
      try
        Send(mesgMessage);
      finally Disconnect; end;
    end;
    Status('Completed');
  finally butnSendMail.Enabled := True; end;
end;

2009-2-22 15:04:32   

查看评语»»»

2009-2-26 11:15:21 

I   copied&pasted   this   code   from   an   existing   unit   I   have,   and   it   should   do   exactly   what   you   want.   The   trick   however   isn't   just   adding   the   image(s)   as   attachment   but   also   telling   OE   or   Outlook   where   to   find   them.   To   do   this,   use   the   "cid:imagename.jpg"   tag   in   your   HTML   code   for   the   images.   Also,   use   the   ExtraHeader   property   to   set   'Content-ID'   to   the   name   of   the   image   as   it   appears   in   your   cid:   tag.   Thus,   if   you   use:  
        
  then   you   add   this   header:  
      Attachment.ExtraHeaders.Values['Content-ID']   :=   ' ';  
  Now,   the   image   MUST   still   be   added   as   an   attachment   since   pure   HTML   cannot   handle   inline   images.   But   if   you   use   this   technique   then   whomever   reads   this   email   will   see   a   background   image   and   a   second   image   as   part   of   the   email,   exactly   like   you   wanted   it   to   appear.   (If   their   browser   can   display   HTML   emails.   Otherwise   they   see   plain   text.)   The   _attached_   will   be   embedded   in   your   email   this   way.  
  Here's   the   (stripped)   code   example.   I   hope   it   still   works.   :D  
  procedure   TFormMain.SendMail(Recipient,   Address:   string);  
  const  
      sStars   =   'BackgroundStars.jpg';  
  var  
      AdressItem:   TIdEMailAddressItem;  
      AFile:   string;  
      AMessage:   TIdMessage;  
      ASMTP:   TIdSMTP;  
      AStream:   TMemoryStream;  
      Attachment:   TIdAttachment;  
      IdBody:   TIdText;  
      IdHTML:   TIdText;  
      idStars:   TIdAttachment;  
      resStars:   TStream;  
      TempFile:   TStream;  
  begin  
      Screen.Cursor   :=   crHourGlass;  
      AFile   :=   FileListBox.FileName;  
      if   FileExists(AFile)   then   begin  
          AMessage   :=   TIdMessage.Create(nil);  
          AMessage.NoDecode   :=   False;  
          AMessage.NoEncode   :=   False;  
          AMessage.ContentType   :=   'multipart/mixed';  
          AMessage.Encoding   :=   meMIME;  
          AMessage.MsgId   :=   'PrettyPic';  
          AMessage.References   :=   ChangeFileExt(ExtractFileName(AFile),   '');  
          //   Set   recipients.  
          AdressItem   :=   AMessage.Recipients.Add;  
          AdressItem.Name   :=   Recipient;  
          AdressItem.Address   :=   Address;  
          //   Set   subject.  
          AMessage.Subject   :=   'Hello.';  
          //   Set   sender.  
          AMessage.Sender.Name   :=   'Workshop   Alex';  
          AMessage.Sender.Address   :=   'someone@somewhere.org';  
          //   Set   from.  
          AMessage.From.Name   :=   AMessage.Sender.Name;  
          AMessage.From.Address   :=   AMessage.Sender.Address;  
          //   Create   plain   body.  
          IdBody   :=   TIdText.Create(AMessage.MessageParts);  
          IdBody.ContentType   :=   'text/plain';  
          IdBody.Body.Add('Hello,   friends.');  
          IdBody.Body.Add('');  
          //   Add   more   to   the   plain-text   bodypart.  
          //   Create   HTML   body.  
          IdHTML   :=   TIdText.Create(AMessage.MessageParts);  
          IdHTML.ContentType   :=   'text/html;   charset=US-ASCII';  
          IdHTML.ContentTransfer   :=   '7bit';  
          IdHTML.Body.Add('');  
          IdHTML.Body.Add('     ');  
          IdHTML.Body.Add('         Hello');  
          IdHTML.Body.Add('     ');  
          IdHTML.Body.Add('     ');  
          IdHTML.Body.Add('         Hello,   friends.
');  
          IdHTML.Body.Add('        
');  
          IdHTML.Body.Add('         ');  
          IdHTML.Body.Add('     ');  
          IdHTML.Body.Add('');  
          //   Add   the   attachment.   Don't   forget   the   extra   headers!  
          Attachment   :=   TIdAttachment.Create(AMessage.MessageParts,   AFile);  
          Attachment.ExtraHeaders.Values['Content-ID']   :=   ' ';  
          Attachment.ContentType   :=   'image/jpeg';  
          idStars   :=   TIdAttachment.Create(AMessage.MessageParts,   ExtractFilePath(ParamStr(0))   +   sStars);  
          idStars.ExtraHeaders.Values['Content-ID']   :=   ' ';  
          idStars.ContentType   :=   'image/jpeg';  
          //   Now   send   the   thing...  
          ASMTP   :=   TIdSMTP.Create(nil);  
          ASMTP.Host   :=   'mail.whatever.org';  
          ASMTP.Port   :=   25;  
          ASMTP.AuthenticationType   :=   atNone;  
          ASMTP.Connect;  
          try  
              ASMTP.Send(AMessage);  
          except  
              on   E:   Exception   do   ShowMessageFmt('Error:   %s',   [E.Message]);  
          end;  
          ASMTP.Disconnect;  
          AMessage.Free;  
          ASMTP.Free;  
      end;  
      Screen.Cursor   :=   crDefault;  
  end;  
  Comment   from   CesarHdez    
  Date:   06/07/2004   04:23PM   PDT  
    Author   Comment      
  Thank   you   very   much,   the   code   you   posted   works   great!!!  
  Thanks,  
  Cesar     

2009-3-3 15:35:21 

delphi7 indy9 通过
procedure TForm1.Button1Click(Sender: TObject);
var
    filename: string;
begin
filename := ExtractFilePath(Application.ExeName) + 'YouImg.jpg';
//MemoInfo.Clear;
//1:对所必须要的信息进行进行检验
//校验服务器属性
if (Trim(HostName.Text)='') or (Trim(HostPort.Text)='') then
begin
ShowMessage('请设置所要连接的SMTP服务器属性!');
HostPort.Text:='25';
HostName.SetFocus;
Exit;
end;
//检测地址信息
if (Trim(EditFrom.Text)='') or (Trim(EditTo.Text)='') then
begin
ShowMessage('请输入收信人或者发信人地址!');
EditFrom.SetFocus ;
Exit;
end;
//用户账号检验
if (Trim(EditUser.Text)='') or (Trim(EditPass.Text)='') then
begin
ShowMessage('请正确输入用户登录帐号和密码!');
EditUser.SetFocus;
Exit;
end;
//设置连接到服务器属性
with IdSMTP do
begin
    Host := Trim(HostName.Text); //SMTP服务器地址
    Port := StrToInt(Trim(HostPort.Text)); //SMTP服务器端口
    UserName := Trim(EditUser.Text); //用户账号
    Password := Trim(EditPass.Text); // 用户密码
end;
//IdEncoderMIME1.EncodeString(
//连接到服务器
MemoInfo.Lines.Add('第一步:准备连接到服务器!'+HostName.Text);
try
    IdSMTP.Connect(); //调用 Connect连接服务器
    MemoInfo.Lines.Add('---------------------返回信息--连接服务器'+ ' ' + IntToStr(IdSMTP.LastCmdResult.NumericCode) );
    MemoInfo.Lines.AddStrings(IdSMTP.LastCmdResult.Text);
except //连接失败
begin
    MemoInfo.Lines.Add('无法连接到服务器!'+HostName.Text);
    Exit;
end ;
end;
MemoInfo.Lines.Add('第二步:服务器要求验证');
//身份验证
//检测SMTP服务器是否需要验证
if (IdSMTP.AuthSchemesSupported.IndexOf ( 'LOGIN' ) <> -1) then
begin //服务器要求验证
    MemoInfo.Lines.Add('服务器要求验证');
    IdSMTP.AuthenticationType:=atlogin;
end
else
begin //服务器不要求验证
    MemoInfo.Lines.Add('服务器不需要验证');
end;
MemoInfo.Lines.Add('---------------------返回信息--要求验证'+ ' ' + IntToStr(IdSMTP.LastCmdResult.NumericCode) );
MemoInfo.Lines.AddStrings(IdSMTP.LastCmdResult.Text);
MemoInfo.Lines.Add('第三步:开始验证');
try
    if IdSMTP.Authenticate then //验证通过
        MemoInfo.Lines.Add('服务器验证通过')
    else
        MemoInfo.Lines.Add('服务器验证失败'); //验证失败
except
    MemoInfo.Lines.Add('服务器验证失败1');
    IdSMTP.Disconnect;
    exit;
end;
MemoInfo.Lines.Add('---------------------返回信息--验证'+ ' ' + IntToStr(IdSMTP.LastCmdResult.NumericCode) );
MemoInfo.Lines.AddStrings(IdSMTP.LastCmdResult.Text);
//对MailMessage属性进行赋值
with MailMessage do
begin
          NoDecode   :=   False;
NoEncode   :=   False;
ContentType   :=   'multipart/mixed';
Encoding   :=   meMIME;
MsgId   :=   'PrettyPic';
References   :=   ChangeFileExt(ExtractFileName(filename),   '');
    Subject := EditSubject.Text; //邮件主题
//    Body.Assign(MemoMesg.Lines);//邮件正文
    From.Address:=Trim(EditFrom.Text); //发信人地址
    Recipients.EMailAddresses := Trim(editTo.Text); //收件人地址
    CCList.EMailAddresses := EditCC.Text;    //抄送
    BCCList.EMailAddresses:= EditBCC.Text;         //暗送
//    for indexnum:=0 to ListBox1.Items.Count -1 do
//    TIdAttachment.Create(MailMessage.MessageParts,ListBox1.Items.Strings[indexnum]);
end;
with TIdText.Create(MailMessage.MessageParts, nil) do begin
//      ContentType := 'multipart/mixed';//'multipart/alternative';
          ContentType   :=   'text/plain';
          Body.Add('Hello,   friends.');
          Body.Add('');
    end;
    with TIdText.Create(MailMessage.MessageParts, nil) do begin
    ContentType   :=   'text/html;   charset=US-ASCII';
ContentTransfer   :=   '7bit';
Body.Add('');
Body.Add('     ');
Body.Add('         Hello');
Body.Add('     ');
Body.Add('     ');
Body.Add('         Hello,   friends.
');
Body.Add('        
');
Body.Add('         ');
Body.Add('     ');
Body.Add('');
    end;
    with TIdAttachment.Create(MailMessage.MessageParts, filename) do begin
    ExtraHeaders.Values['Content-ID']   :=   ' ';
    ContentType   :=   'image/jpeg';
    end;
    with TIdAttachment.Create(MailMessage.MessageParts, filename) do begin
    ExtraHeaders.Values['Content-ID']   :=   ' ';
    ContentType   :=   'image/jpeg';
    end;
  MailMessage.SaveToFile('1.txt');
MemoInfo.Lines.Add('第四步:开始发送邮件');
//发送信件
try
    IdSMTP.Send(MailMessage);
    if IdSMTP.LastCmdResult.NumericCode = 250 then
    MemoInfo.Lines.Add('---------------------发送成功'+ ' ' + IntToStr(IdSMTP.LastCmdResult.NumericCode) )
    else
    MemoInfo.Lines.Add('---------------------发送失败'+ ' ' + IntToStr(IdSMTP.LastCmdResult.NumericCode) );
except
    MemoInfo.Lines.Add('发送失败');
end;
MemoInfo.Lines.Add('---------------------返回信息—发送邮件成功'+ ' ' + IntToStr(IdSMTP.LastCmdResult.NumericCode) );
MemoInfo.Lines.AddStrings(IdSMTP.LastCmdResult.Text);
MemoInfo.Lines.Add('第五步:断开服务器');
IdSMTP.Disconnect;
MemoInfo.Lines.Add('---------------------返回信息--连接断开' + ' ' + IntToStr(IdSMTP.LastCmdResult.NumericCode) );
MemoInfo.Lines.AddStrings(IdSMTP.LastCmdResult.Text);
//发送完成
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值