邮件如何以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;

解决方案 »

  1.   

    function TSendEMail.SendMail:integer;
    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;