Sending emails in Delphi

0

I'm using the following code snippet for sending emails in Delphi, but when I try to send an attachment, the attachment becomes part of the email body. What could be wrong?

function SendEmail(sendTo, subject, body: string;
  attachFiles: TStringList; smtpHost: string; smtpPort: Integer; smtpUser,
  smtpPass: string; tls: TIdUseTLS; respoderPara: string): boolean;
var
    smtp: TIdSmtp;
    ssl: TIdSSLIOHandlerSocketOpenSSL;
    msg: TIdMessage;
    i: Integer;
    replyTo: TIdEMailAddressItem;
    IdAttachmentFile  : TIdAttachmentFile;
begin
    smtp:=TIdSmtp.Create(nil);
    ssl:=TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    msg:=TIdMessage.Create(nil);
    try

        try
            smtp.Host := smtpHost;
            smtp.Port := smtpPort;
            smtp.Username := smtpUser;
            smtp.Password := smtpPass;


            //smtp.OnConnected :=IdSMTP1Connected;
            //smtp.OnDisconnected :=IdSMTP1Disconnected;
            //smtp.OnFailedRecipient :=IdSMTP1FailedRecipient;
            //smtp.OnStatus :=IdSMTP1Status;
            //smtp.OnTLSNotAvailable :=IdSMTP1TLSNotAvailable;
            //smtp.OnWork :=IdSMTP1Work;

            if not (tls=utNoTLSSupport) then
            begin
                ssl.Destination := smtpHost + ':' + IntToStr(smtpPort);
                ssl.Host := smtpHost;
                ssl.Port := smtpPort;
                ssl.SSLOptions.Method := sslvTLSv1;

                //ssl.OnStatusInfo:=IdSSLIOHandlerSocketOpenSSL1StatusInfo;
                //ssl.OnGetPassword:=IdSSLIOHandlerSocketOpenSSL1GetPassword;
                //ssl.OnStatus:=IdSSLIOHandlerSocketOpenSSL1Status;

                smtp.IOHandler := ssl;
                smtp.UseTLS := tls;
            end;

            msg.Recipients.EMailAddresses := sendTo;
            msg.Subject := subject;
            msg.ContentType := 'text/html';
            msg.Body.Text := body;

            if respoderPara <> '' then
            begin
              replyTo := msg.ReplyTo.Add;
              replyTo.Name := 'SYNS - Contato';
              replyTo.Address := respoderPara;
            end;

            if(Assigned(attachFiles)) then
            begin
                for i := 0 to attachFiles.Count - 1 do
                begin
                   if FileExists(attachFiles[i]) then
                   begin
                       IdAttachmentFile := TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]); //Incluindo o anexo na mensagem
                       IdAttachmentFile.ContentType := 'application/pdf;'; //Informando o tipo MIME do anexo. IMPORTANTE! Colocar o tipo MIME + ; (ponto-e-vírgula)
                       IdAttachmentFile.FileName := ExtractFileName(attachFiles[i]); //Nome do arquivo
                       //TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]);
                   end;
                end;
            end;

            smtp.Connect;
            smtp.Send(msg);
            smtp.Disconnect;

            result:=true;
        finally
            msg.Free;
            ssl.Free;
            smtp.Free;
        end;
    except
       result:=false;
    end;

end;

    
asked by anonymous 07.06.2016 / 13:32

1 answer

0

If resolved, I added a TIdText , I changed the ContentType from TIdMessage to 'multipart/mixed' and set ContentType from TIdText to 'text/html'

function SendEmail(sendTo, subject, body: string;
  attachFiles: TStringList; smtpHost: string; smtpPort: Integer; smtpUser,
  smtpPass: string; tls: TIdUseTLS; respoderPara: string): boolean;
var
    smtp: TIdSmtp;
    ssl: TIdSSLIOHandlerSocketOpenSSL;
    msg: TIdMessage;
    text: TIdText;
    i: Integer;
    replyTo: TIdEMailAddressItem;
    IdAttachmentFile  : TIdAttachmentFile;
begin
    smtp:=TIdSmtp.Create(nil);
    ssl:=TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    msg:=TIdMessage.Create(nil);
    try

        try
            smtp.Host := smtpHost;
            smtp.Port := smtpPort;
            smtp.Username := smtpUser;
            smtp.Password := smtpPass;    

            if not (tls=utNoTLSSupport) then
            begin
                ssl.Destination := smtpHost + ':' + IntToStr(smtpPort);
                ssl.Host := smtpHost;
                ssl.Port := smtpPort;
                ssl.SSLOptions.Method := sslvTLSv1;                      

                smtp.IOHandler := ssl;
                smtp.UseTLS := tls;
            end;

            msg.Recipients.EMailAddresses := sendTo;
            msg.Subject := subject;

            msg.MessageParts.Clear();
            msg.ContentType := 'multipart/mixed';

            text := TIdText.Create (msg.MessageParts, Nil);
            text.ContentType := 'text/html';
            text.CharSet := 'ISO-8859-1';
            text.Body.Text := body;

            if respoderPara <> '' then
            begin
              replyTo := msg.ReplyTo.Add;
              replyTo.Name := 'SYNS - Contato';
              replyTo.Address := respoderPara;
            end;

            if(Assigned(attachFiles)) then
            begin
                for i := 0 to attachFiles.Count - 1 do
                begin
                   if FileExists(attachFiles[i]) then
                   begin
                       IdAttachmentFile := TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]); //Incluindo o anexo na mensagem
                       IdAttachmentFile.ContentType := 'application/pdf;'; //Informando o tipo MIME do anexo. IMPORTANTE! Colocar o tipo MIME + ; (ponto-e-vírgula)
                       IdAttachmentFile.FileName := ExtractFileName(attachFiles[i]); //Nome do arquivo
                       IdAttachmentFile.ContentDisposition := 'inline';
                       IdAttachmentFile.ContentTransfer := 'base64';
                       //TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]);
                   end;
                end;
            end;

            smtp.Connect;
            smtp.Send(msg);
            smtp.Disconnect;

            result:=true;
        finally
            msg.Free;
            ssl.Free;
            smtp.Free;
        end;
    except
       result:=false;
    end;

end;
    
07.06.2016 / 14:49