HanDs
管理员

[Delphi文章] 使用indy10发送网页 





学习中请遵循国家相关法律法规,黑客不作恶。没有网络安全就没有国家安全

本站需要登陆后才能查看

本文转自 http://www.delphibbs.com/keylife/iblog_show.asp?xid=31017

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>');
html.Add('<head>');
html.Add('</head>');
html.Add('<body><h1>Hello</h1>');
html.Add('<img src="cid:YouImg.jpg" />'); //这里这个CID是什么意思,还没弄明白。
html.Add('This is a picture!</body>');
html.Add('</html>');

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;

--------------------------------------

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:
<img src="cid:PrettyPic">
then you add this header:
Attachment.ExtraHeaders.Values['Content-ID'] := '<PrettyPic>';

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 := [email protected]';
// 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('<html>');
IdHTML.Body.Add(' <head>');
IdHTML.Body.Add(' <title>Hello</title>');
IdHTML.Body.Add(' </head>');
IdHTML.Body.Add(' <body title="' + AMessage.References + '" background="cid:BackgroundStars">');
IdHTML.Body.Add(' Hello, friends.<br>');
IdHTML.Body.Add(' <br>');
IdHTML.Body.Add(' <img src="cid:PrettyPic" alt="' + ExtractFileName(AFile) + '" name="' + ExtractFileName(AFile) + '" title="Just an image included.">');
IdHTML.Body.Add(' </body>');
IdHTML.Body.Add('</html>');
// Add the attachment. Don't forget the extra headers!
Attachment := TIdAttachment.Create(AMessage.MessageParts, AFile);
Attachment.ExtraHeaders.Values['Content-ID'] := '<PrettyPic>';
Attachment.ContentType := 'image/jpeg';
idStars := TIdAttachment.Create(AMessage.MessageParts, ExtractFilePath(ParamStr(0)) + sStars);
idStars.ExtraHeaders.Values['Content-ID'] := '<BackgroundStars>';
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

--------------------------------------

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('<html>');
Body.Add(' <head>');
Body.Add(' <title>Hello</title>');
Body.Add(' </head>');
Body.Add(' <body title="' + MailMessage.References + '" background="cid:BackgroundStars">');
Body.Add(' Hello, friends.<br>');
Body.Add(' <br>');
Body.Add(' <img src="cid:PrettyPic" alt="' + ExtractFileName(Filename) + '" name="' + ExtractFileName(Filename) + '" title="Just an image included.">');
Body.Add(' </body>');
Body.Add('</html>');
end;


with TIdAttachment.Create(MailMessage.MessageParts, filename) do begin
ExtraHeaders.Values['Content-ID'] := '<PrettyPic>';
ContentType := 'image/jpeg';
end;

with TIdAttachment.Create(MailMessage.MessageParts, filename) do begin
ExtraHeaders.Values['Content-ID'] := '<BackgroundStars>';
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;


学习中请遵守法律法规,本网站内容均来自于互联网,本网站不负担法律责任
使用 i nd y1 发送 网页
#1楼
发帖时间:2016-7-9   |   查看数:0   |   回复数:0
游客组
快速回复