求助:用Indy的TIdSMTP发送HTML格式的邮件,并且带附件。。。。

weyoung 2005-10-17 05:11:07
邮件要写一个程序,用于给用户发送邮件,邮件中带附件,考虑到美观,所以邮件内容用HTML格式,
却老是搞不定,请各位帮忙。

代码如下:

Var
html: TStrings;
htmlPart: TIdText;
begin
html := TStringList.Create();
html.Append('<html>');
html.Append('<head>');
html.Append('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">');
html.Append('<title id="ridTitle">' + BUS_NAME + '</title>');
html.Append('<style><!-- body { font-family: "宋体", "黑体"; font-size: 12pt; color: #000000; margin-top: 0px; margin-left: 10px; } td {');
html.Append('font-size: 12px;');
html.Append('}');
html.Append('--></style>');
html.Append('</head>');
html.Append('<body topmargin="0">');
html.Append('<table width="678" height="394" border="0" align="center" cellpadding="0" cellspacing="0">');
html.Append(' <tr>');
html.Append(' <td height="84"><img src="' + BUS_MAIL_IMAGE + 'banner.jpg" width="793" height="124"></td>');
html.Append(' </tr>');
html.Append(' <tr>');
html.Append(' <td height="108" valign="top" background="' + BUS_MAIL_IMAGE + 'background.gif">');
html.Append(' <div align="center"></div>');
html.Append(' <font size=2>');
html.Append('   对方电话:' + sGuestPhone + '<br>');
html.Append('   传真时间:' + sTime + '<br>');
html.Append(' </font>');
html.Append(' </td>');
html.Append(' </tr>');
html.Append(' <tr>');
html.Append(' <td height="158" valign="top" background="' + BUS_MAIL_IMAGE + 'background.gif">');
html.Append(' <div align="center"></div></td>');
html.Append(' </tr>');
html.Append(' <tr>');
html.Append('</table>');
html.Append('</body>');
html.Append('</html>');

Try
Try
With IdMessage1 do
begin
Clear;
Body.Clear;
CCList.Clear;
MessageParts.Clear;
Charset := 'gb2312';

ContentType := 'text/html';
Body.Assign(html);

From.Text := SMTP_USEREMAIL;
ReplyTo.EMailAddresses := SMTP_USEREMAIL;
Subject := '您的朋友' + sGuestPhone + '给您的传真';
Priority := TIdMessagePriority(0);
ReceiptRecipient.Text := '';
end;
if FileExists(sAttach) then TIdAttachment.Create(IdMsgSend.MessageParts, sAttach); //附件

SMTP.AuthenticationType := atLogin; {Simple Login}
SMTP.Username := SMTP_USERNAME;
SMTP.Password := SMTP_PASSWORD;
SMTP.Host := SMTP_ADDR;
SMTP.Port := SMTP_PORT;

SMTP.Connect;
Try
SMTP.Send(IdMsgSend);
WriteLog('发送成功!' + sLogMsg);
finally
SMTP.Disconnect;
end;
Except
On E:Exception do
begin
WriteLog('在发送邮件通知时发生错误,错误消息:' + E.Message);
Exit;
end;
end;
Finally
html.Free;
end;
end;

问题主要出在ContentType这段上面。
一、
上面这段代码,发送出去后,用户收到的邮件中,HTML格式不能正确显示(显示的是HTML的源代码),附件能正常显示。
如果把if FileExists(sAttach) 这一行屏蔽掉,则用户收到的邮件虽然没有附件,但HTML内容能正常显示。

//ContentType := 'multipart/mixed';
//htmlPart := TIdText.Create(IdMsgSend.MessageParts, html);
//htmlPart.ContentType := 'text/html; charset=gb2312';
//htmlPart.Body.Assign(html);

二、
如果把
ContentType := 'text/html';
Body.Assign(html);
屏蔽掉,改为:
ContentType := 'multipart/mixed';
htmlPart := TIdText.Create(IdMsgSend.MessageParts, html);
htmlPart.ContentType := 'text/html; charset=gb2312';
htmlPart.Body.Assign(html);
则用户收到的邮件里,附件是正常的,但正文却没有任何内容,一片空白。。。

请各位帮忙指点一下,多谢!
...全文
1413 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
weyoung 2005-10-17
  • 打赏
  • 举报
回复
问题已经解决。多谢aiirii(ari-淘金坑) !
顺便把上面的答案也贴出来。

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 := '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('<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
weyoung 2005-10-17
  • 打赏
  • 举报
回复
楼上的,解决方案看不到啊。注册这个会员要收费的?晕。。
aiirii 2005-10-17
  • 打赏
  • 举报
回复
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21014266.html

1,594

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧