邮件如何以text/html格式发送,同时有带附件?
小弟的代码单独发送可以以text/html格式发送.如果有附件就会变成以text/plain格式发送.
代码如下:
var Msg:TIdMessage;
tatt:TIdAttachment;
i:integer;
text:TIdText;
idMesg:TIdMessagePart;
begin
self.IdSMTP1.Username:='*********';
self.IdSMTP1.Password:='********';
self.IdSMTP1.Host:='smtp.163.com';
self.IdSMTP1.Port:=25;
self.IdSMTP1.AuthenticationType:=atLogin;
self.IdSMTP1.Connect();
self.IdSMTP1.Authenticate;
Msg:=TIdMessage.Create(Self);
msg.Clear;
Memo1.Lines.Add('<html>');
Memo1.Lines.Add('<head>');
Memo1.Lines.Add('<title>Untitled Document</title>');
Memo1.Lines.Add('<meta http-equiv="Content-Type" content="text/html; charset=gb2312">');
Memo1.Lines.Add('ffffffffff');
Memo1.Lines.Add('</head>');
TIdAttachment.Create(Msg.MessageParts,'E:\Test\delphi_mail\face-5.gif');
msg.Body.Text:=self.Memo1.Text;
Msg.From.Address:='[email protected]';
Msg.Subject:='123';
msg.ContentType:='text/html';
Msg.Recipients.EMailAddresses:='[email protected]';
self.IdSMTP1.Send(msg);
self.IdSMTP1.DisconnectSocket;
self.IdSMTP1.Disconnect;
self.IdSMTP1.Destroy;
小弟的代码单独发送可以以text/html格式发送.如果有附件就会变成以text/plain格式发送.
代码如下:
var Msg:TIdMessage;
tatt:TIdAttachment;
i:integer;
text:TIdText;
idMesg:TIdMessagePart;
begin
self.IdSMTP1.Username:='*********';
self.IdSMTP1.Password:='********';
self.IdSMTP1.Host:='smtp.163.com';
self.IdSMTP1.Port:=25;
self.IdSMTP1.AuthenticationType:=atLogin;
self.IdSMTP1.Connect();
self.IdSMTP1.Authenticate;
Msg:=TIdMessage.Create(Self);
msg.Clear;
Memo1.Lines.Add('<html>');
Memo1.Lines.Add('<head>');
Memo1.Lines.Add('<title>Untitled Document</title>');
Memo1.Lines.Add('<meta http-equiv="Content-Type" content="text/html; charset=gb2312">');
Memo1.Lines.Add('ffffffffff');
Memo1.Lines.Add('</head>');
TIdAttachment.Create(Msg.MessageParts,'E:\Test\delphi_mail\face-5.gif');
msg.Body.Text:=self.Memo1.Text;
Msg.From.Address:='[email protected]';
Msg.Subject:='123';
msg.ContentType:='text/html';
Msg.Recipients.EMailAddresses:='[email protected]';
self.IdSMTP1.Send(msg);
self.IdSMTP1.DisconnectSocket;
self.IdSMTP1.Disconnect;
self.IdSMTP1.Destroy;
解决方案 »
- 版主请进!
- Database Desktop数据库,没搞过,征求各位高招了--------有点急
- VC6.0写的ATL COM组件,在CB5中注册后运行程序,提示:无法定位程序输入点@$xp$32FltTestlib...
- delphi5开发人员指南 原代码发生错误
- 让RadioButton被选中的问题
- 请问ADO能用'事务'吗?在线等待~~~~~~~~~~~~~~
- 时钟控件事件不能自动触发,造成死循环,各位大吓帮我解决啊,先谢谢拉!
- 函数问题
- 【雪山飞车】在炎热的夏天里体验在雪山上飞速驰骋的快感,感受无比刺激的游戏人生!界面美观,操作方便,不妨一玩!
- 用DELPHI打开WORD2000的问题
- 请教高手问题,急??
- 怎样旋转矢量图形?
var AMessage:TIdMessage;
AdressItem:TIdEMailAddressItem;
IdBody:TIdText;
IdHTML:TIdText;
Attfn:TIdAttachment;
AttImage:array of TIdAttachment;
Imagelist:TStrings;
i,FResult:integer;
s:string;
begin
FResult:=-1;
AMessage:=TIdMessage.Create(nil);
try
AMessage.NoDecode := False;
AMessage.NoEncode := False;
AMessage.ContentType:= 'multipart/mixed';
AMessage.Encoding := meMIME;
// Set recipients.
AdressItem := AMessage.Recipients.Add;
AdressItem.Name := FRecName;
AdressItem.Address := FRecAddress;
// Set subject.
AMessage.Subject := FSubject;
// Set sender.
AMessage.Sender.Name:= FSmtpUserName;
AMessage.Sender.Address:=FSmtpAddress;
// 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';
if FTextBody<>nil then IdBody.Body.Assign(FTextBody);
IdBody.Body.Add(''); // Add more to the plain-text bodypart.
// Create HTML body.
if FHTMLBody<>nil then
begin
IdHTML := TIdText.Create(AMessage.MessageParts);
IdHTML.ContentType := 'text/html';
IdHTML.Body.Assign(FHTMLBody)
end;
{ else
begin
IdHTML.Body.Add('<html>');
IdHTML.Body.Add('</html>');
end;
}
// Add the attachment. Don't forget the extra headers!
for i:=0 to FAttachmentList.Count-1 do
begin
if not fileexists(FAttachmentList[i]) then continue;
s:=FAttachmentList[i];
Attfn:=TIdAttachment.Create(AMessage.MessageParts, FAttachmentList[i]);
end;{ Imagelist:=TStringlist.Create;
self.GetHTMLImage(IdHTML.Body, ImageList);
if (Imagelist<>nil) and (Imagelist.Count>0) then
begin
setlength(AttImage,Imagelist.Count);
for i:=0 to Imagelist.Count-1 do
begin
s:=Imagelist[i];
Attfn:=TIdAttachment.Create(AMessage.MessageParts, Imagelist[i]);
// AttImage[i]:=TIdAttachment.Create(AMessage.MessageParts, Imagelist[i]);
// AttImage[i].ExtraHeaders.Values['Content-ID']:='<PrettyPic'+inttostr(i)+'>';
// AttImage[i].ContentType:='image/jpeg';
end;
end;
}
// Now send the thing...
ASMTP.Host:=FSmtpHost;
ASMTP.Port:=FSmtpPort;
ASMTP.Username:=FSmtpUserName;
ASMTP.Password:=FSmtpPassWord;
ASMTP.AuthenticationType:=FSmtpAuthenticationType;
ASMTP.Connect;
try
ASMTP.Send(AMessage);
FResult:=0;
except
on E: Exception do
begin
ShowMessageFmt('Error: %s', [E.Message]);
FResult:=-1;
end;
end;
ASMTP.Disconnect;
finally
AMessage.Free;
end;
Result:=FResult;
end;