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;
The main problem I see with this code is that you are assigning the same value (
edtDestinatario.Text
) to theTIdMessage.From.Address
,TIdMessage.Recipients.EMailAddresses
, andTIdMessage.BccList.EMailAddresses
properties. You should not be doing that (except when sending test emails to yourself). You need to set theTIdMessage.From
property to the name/address of the person who is sending the email, and there is no point in havingTIdMessage.Recipients
andTIdMessage.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 optionalAFrom
parameter of theTIdSMTP.Send()
method, to the account owner's real address. WhenTIdSMTP
sends aMAIL 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 theAFrom
address if provided, otherwise theTIdMessage.Sender.Address
if provided, otherwise theTIdMessage.From.Address
address.The sender that is specified in the
MAIL FROM
command is treated differently than theFrom
andSender
email headers thatTIdMessage
generates. In fact, the actual email data thatTIdMessage
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 whateverName
you assign to theTIdMessage.From
andTIdMessage.Sender
properties, that is the name that recipients will see.