Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Indy, Email, D10


This question is not answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Jul 31, 2017 9:44 AM Last Post By: Remy Lebeau (Te...
Jim Sawyer

Posts: 214
Registered: 1/3/10
Indy, Email, D10  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 21, 2017 6:59 AM
In the following code why would the

smtp.Send( msg )

be an invalid command???

procedure TfrmEmailTest.SendEmailIndy(
      const SMTPServer: String;
      const FromName, FromAddress: String;
      const ToAddresses: String; // comma "," separated list of email addresses
      const CCAddresses: String;  // comma "," separated list of email addresses
      const BCCAddresses: String;  // comma "," separated list of email addresses
      const Subject: String;
      const EmailBody: String;
      const IsBodyHtml: Boolean;
      const Attachments: TStrings );
var
  smtp: TIdSMTP;  // IdSmtp.Pas
  msg: TIdMessage;  // IdMessage.Pas
  builder: TIdCustomMessageBuilder;  // IdMessageBuilder.Pas
  s: String;
  emailAddress: String;
begin
  msg := TIdMessage.Create( nil );
  try
    if IsBodyHtml then
    begin
      builder := TIdMessageBuilderHtml.Create;
      TIdMessageBuilderHtml( Builder ).Html.Text := Emailbody;
    end
    else
    begin
      builder := TIdMessageBuilderPlain.Create;
    end;
    try
      if Attachments <> NIL then
      begin
        for s in Attachments do
          Builder.Attachments.Add(s);
      end;
      builder.FillMessage( msg );
    finally
      builder.Free;
    end;
 
    msg.From.Name := FromName;
    msg.From.Address := FromAddress;
    msg.Subject := Subject;
 
    // if the message is plaintext then we must fill the body outside of the plaintext email builder.
    // (the plainTextBuilder is unable to build plaintext email)
    if not IsBodyHtml then
      msg.Body.Text := EmailBody;
 
    for s in ToAddresses.Split( [','] ) do
    begin
      emailAddress := Trim( s );
      if emailAddress <> '' then
      begin
        with msg.Recipients.Add do
        begin
          // Name := <Name of recipient>
          Address := emailAddress;
        end;
      end;
    end;
 
    for s in CCAddresses.Split( [','] ) do
    begin
      emailAddress := Trim( s );
      if emailAddress <> '' then
        msg.CCList.Add.Address := emailAddress;
    end;
 
    for s in BCCAddresses.Split( [','] ) do
    begin
      emailAddress := Trim( s );
      if emailAddress <> '' then
        msg.BccList.Add.Address := emailAddress
    end;
 
    smtp := TIdSMTP.Create( NIL );
    try
      smtp.Host := SMTPServer;
      smtp.Port := 25;
 
      // Indy (and C# SmtpClient class) already defaults to the computer name
      // smtp.HeloName :=
      smtp.Connect;
      try
        smtp.Send( msg );
      finally
        smtp.Disconnect();
      end;
    finally
      smtp.Free;
    end;
  finally
    msg.Free;
  end;
end;
 
procedure TfrmEmailTest.btnCloseClick(Sender: TObject);
begin
  Close;
end;
 
procedure TfrmEmailTest.btnSendEmailClick(Sender: TObject);
begin
  SendEmailIndy(
    'smtp.verizon.net',
    'jim.sawyer', 'jim.sawyer@verizon.net',
    'jimsawyer222@gmail.com',
    '',
    '',
    'Testing',
    '<!DOCTYPE html><html><body>Now is the time...</body></html>',
    true,
    nil );
end;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Indy, Email, D10
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 24, 2017 12:33 PM   in response to: Jim Sawyer in response to: Jim Sawyer
Jim Sawyer wrote:

In the following code why would the

smtp.Send( msg )

be an invalid command???

What makes you think it is invalid? Please be more specific.

Also, your assertion that "if the message is plaintext then we must
fill the body outside of the plaintext email builder. (the
plainTextBuilder is unable to build plaintext email)" is completely
wrong. The PlainText builder is specifically designed for creating
plain-text emails, hence its name. Even the HTML builder is capable of
creating plain-text emails, too. So, you don't really need to use
TIdMessageBuildlerPlain in this situation at all.

Try something more like this:

uses
  ..., IdSmtp, IdMessage, IdMessageBuilder;
 
...
 
procedure TfrmEmailTest.SendEmailIndy(
  const SMTPServer: String;
  const FromName, FromAddress: String;
  const ToAddresses: String; // comma separated list of email addresses
  const CCAddresses: String;  // comma separated list of email addresses
  const BCCAddresses: String; // comma separated list of email addresses
  const Subject: String;
  const EmailBody: String;
  const IsBodyHtml: Boolean;
  const Attachments: TStrings );
var
  smtp: TIdSMTP;
  msg: TIdMessage;
  builder: TIdMessageBuilderHtml;
begin
  msg := TIdMessage.Create( nil );
  try
    builder := TIdMessageBuilderHtml.Create;
    try
      if IsBodyHtml then
        builder.Html.Text := Emailbody
      else
        builder.PlainText.Text := Emailbody;
 
      if Attachments <> nil then
      begin
        for s in Attachments do
          builder.Attachments.Add(s);
      end;
 
      builder.FillMessage( msg );
    finally
      builder.Free;
    end;
 
    msg.From.Name := FromName;
    msg.From.Address := FromAddress;
    msg.Subject := Subject;
    msg.Recipients.EmailAddresses := ToAddresses;
    msg.CCList.EmailAddresses := CCAddresses;
    msg.BccList.EmailAddresses := BCCAddresses;
 
    smtp := TIdSMTP.Create( NIL );
    try
      smtp.Host := SMTPServer;
      smtp.Port := 25;
 
      // Indy (and C# SmtpClient class) already defaults to the
computer name
      // smtp.HeloName := ...;
      smtp.Connect;
      try
        smtp.Send( msg );
      finally
        smtp.Disconnect;
      end;
    finally
      smtp.Free;
    end;
  finally
    msg.Free;
  end;
end;


--
Remy Lebeau (TeamB)
Jim Sawyer

Posts: 214
Registered: 1/3/10
Re: Indy, Email, D10  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2017 7:03 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks for the code. However after utilizing it on both the 32 bit and 64 bit application,
I still get the error (as presented by EurekaLog) as follows:

EIdSocketError
Socket Error #10060
Connection timed out
ID 97EA3FE4

I'm trying to use a mail.airmail.net account that I use with a Delphi 7
routine I developed with the help of the MarshallSoft wrapper. So I
know it works. I'm using Delphi 10.2 and Windows 10 pro in
a high end Dell computer.

I welcome any ideas on what the problem might be and on
anything else I can try.

Thanks for your assistance above and beyond the call of duty.
Jim Sawyer


Remy Lebeau (TeamB) wrote:
Jim Sawyer wrote:

In the following code why would the

smtp.Send( msg )

be an invalid command???

What makes you think it is invalid? Please be more specific.

Also, your assertion that "if the message is plaintext then we must
fill the body outside of the plaintext email builder. (the
plainTextBuilder is unable to build plaintext email)" is completely
wrong. The PlainText builder is specifically designed for creating
plain-text emails, hence its name. Even the HTML builder is capable of
creating plain-text emails, too. So, you don't really need to use
TIdMessageBuildlerPlain in this situation at all.

Try something more like this:

uses
  ..., IdSmtp, IdMessage, IdMessageBuilder;
 
...
 
procedure TfrmEmailTest.SendEmailIndy(
  const SMTPServer: String;
  const FromName, FromAddress: String;
  const ToAddresses: String; // comma separated list of email addresses
  const CCAddresses: String;  // comma separated list of email addresses
  const BCCAddresses: String; // comma separated list of email addresses
  const Subject: String;
  const EmailBody: String;
  const IsBodyHtml: Boolean;
  const Attachments: TStrings );
var
  smtp: TIdSMTP;
  msg: TIdMessage;
  builder: TIdMessageBuilderHtml;
begin
  msg := TIdMessage.Create( nil );
  try
    builder := TIdMessageBuilderHtml.Create;
    try
      if IsBodyHtml then
        builder.Html.Text := Emailbody
      else
        builder.PlainText.Text := Emailbody;
 
      if Attachments <> nil then
      begin
        for s in Attachments do
          builder.Attachments.Add(s);
      end;
 
      builder.FillMessage( msg );
    finally
      builder.Free;
    end;
 
    msg.From.Name := FromName;
    msg.From.Address := FromAddress;
    msg.Subject := Subject;
    msg.Recipients.EmailAddresses := ToAddresses;
    msg.CCList.EmailAddresses := CCAddresses;
    msg.BccList.EmailAddresses := BCCAddresses;
 
    smtp := TIdSMTP.Create( NIL );
    try
      smtp.Host := SMTPServer;
      smtp.Port := 25;
 
      // Indy (and C# SmtpClient class) already defaults to the
computer name
      // smtp.HeloName := ...;
      smtp.Connect;
      try
        smtp.Send( msg );
      finally
        smtp.Disconnect;
      end;
    finally
      smtp.Free;
    end;
  finally
    msg.Free;
  end;
end;


--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Indy, Email, D10  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 31, 2017 9:44 AM   in response to: Jim Sawyer in response to: Jim Sawyer
Jim Sawyer wrote:

I'm trying to use a mail.airmail.net account

The SMTP server for Airmail is smtp.airmail.net. mail.airmail.net is
the webmail server instead.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02