Change sender when sending email with TIdSmtp

1.9k views Asked by At

I'm making an email system for digital marketing and I have a problem to change the sender's name using Indy10, it is always taking the standard name registered in the account. I would like to change this name.

var
  IdBody: TIdText;

begin
 with IDSMTP do
  begin
   if Connected then
    Disconnect();
   Host := edtSMTP.Text;
   Password := edtSenha.Text;
   Port := sePorta.Value;
   Username := edtRemetente.Text;
   with IdSSLIOHandlerSocketOpenSSL.SSLOptions do
    begin
     SSLVersions := [];
     if cbxSSLV2.Checked then
      SSLVersions := [sslvSSLv2];
     if cbxSSLV23.Checked then
      SSLVersions := SSLVersions + [sslvSSLv23];
     if cbxSSLV3.Checked then
      SSLVersions := SSLVersions + [sslvSSLv3];
     if cbxTLSV1.Checked then
      SSLVersions := SSLVersions + [sslvTLSv1];
     if cbxTLSV11.Checked then
      SSLVersions := SSLVersions + [sslvTLSv1_1];
     if cbxTLSV12.Checked then
      SSLVersions := SSLVersions + [sslvTLSv1_2];
    end;

   case cbxAutenticacao.ItemIndex of
     1:  UseTLS := utNoTLSSupport;
     2:  UseTLS := utUseImplicitTLS;
     3:  UseTLS := utUseRequireTLS;
     4:  UseTLS := utUseExplicitTLS;
   end;

   Connect;
   Authenticate;
   with IdMessage do
    begin
     MessageParts.Clear;
     Subject := edtAssunto.Text;
     MessageParts.Clear;
     IdBody := TIdText.Create(MessageParts);
     try
      IdBody.ContentType := 'text/html';
      IdBody.Body.Text := mEngenharia.Text;
      if edtAnexo.Text <> '' then
       begin
        if FileExists(edtAnexo.Text) then
         TIdAttachmentFile.Create(MessageParts, TFileName(edtAnexo.Text));
       end;
      From.Address := edtDestinatario.Text;
      Recipients.EMailAddresses := edtDestinatario.Text;
      BccList.EMailAddresses := edtDestinatario.Text;
      Send(IdMessage);
      ShowMessage('Email enviado com sucesso!');
     finally
      FreeAndNil(IdBody);
     end;
    end;
  end;
end;
1

There are 1 answers

0
Remy Lebeau On

The main problem I see with this code is that you are assigning the same value (edtDestinatario.Text) to the TIdMessage.From.Address, TIdMessage.Recipients.EMailAddresses, and TIdMessage.BccList.EMailAddresses properties. You should not be doing that (except when sending test emails to yourself). You need to set the TIdMessage.From property to the name/address of the person who is sending the email, and there is no point in having TIdMessage.Recipients and TIdMessage.BccList contain the same list of addresses as those recipients will then receive duplicate emails.

If the address of the person who is sending the email is not the same as the owner of the SMTP account you are using to send the email with, you need to set the TIdMessage.Sender.Address property, or the optional AFrom parameter of the TIdSMTP.Send() method, to the account owner's real address. When TIdSMTP sends a MAIL FROM command to the server, it has to specify the actual sender of the email, which the SMTP server then validates to make sure it is allowed to send email (even if you had already authenticated with the server beforehand). In that command, TIdSMTP specifies the AFrom address if provided, otherwise the TIdMessage.Sender.Address if provided, otherwise the TIdMessage.From.Address address.

The sender that is specified in the MAIL FROM command is treated differently than the From and Sender email headers that TIdMessage generates. In fact, the actual email data that TIdMessage generates is sent as-is to each recipient, it is just arbitrary data as far as the SMTP server is concerned. The server is not supposed to mess around with it, other than to insert additional headers for tracking and routing purposes. So whatever Name you assign to the TIdMessage.From and TIdMessage.Sender properties, that is the name that recipients will see.

with IdMessage do
begin
  Clear;
  try
    Subject := edtAssunto.Text;
    if FileExists(edtAnexo.Text) then
    begin
      ContextType := 'multipart/mixed';
      IdBody := TIdText.Create(MessageParts);
      IdBody.ContentType := 'text/html';
      IdBody.Body.Text := mEngenharia.Text;
      TIdAttachmentFile.Create(MessageParts, edtAnexo.Text);
    end else
    begin
      ContentType := 'text/html';
      Body.Text := mEngenharia.Text;
    end;
    From.Address := ...;
    From.Name := ...;
    Sender.Address := ...; // if different than From.Address
    Recipients.EMailAddresses := edtDestinatario.Text;
    Send(IdMessage);
  finally
    Clear;
  end;
  ShowMessage('Email enviado com sucesso!');
end;