Jump to content

Sending email


Abaksoft

Recommended Posts

Dear Unigueers,

There are many possibilities to send email with Delphi.

The easiest way a found, is via gmail Server

Although I found a few portions of code on the forum, no one has detailed the important steps to make it work.

So, here is a sample where you will for sure understand :

 

//==================================
// Sending email with gmail Server :
//==================================
// This is a compilation of code providing from Web and Unigui Forum.
// So thx to all of them :)


// Step by Step :
//1. if you have not a gmail account, Create it.
//2. Important :

// -  Enter in your Personnal Setting (gmail) and look for "Security"
// -  Activate something like : "Allow less secure apps "
//3.  Download (and Copy on the same executable folder ...\debug\) the two open SSL Libraries :  libeay32.dll  and   ssleay32.dll.

(you can find them in C:\...FmSoft\..\SSL)

Important: 

- if your unigui app is deployed on iis ( ISAPI), then copy them on  system32 or syswow64 .

- if it is deployed as Service / hyperserver, then keep them behind your exe file (same directory)


//4. Connect internet and launch this program
 

Have Fun :)

 

Sending_Email.zip

 

Edit :

On Unit Cls_email :  Modify  this with your emal adress :

    IdSMTP.Username        :='MyAdress@gmail.com';   //   (1)
    IdSMTP.Password        :='Mypw';
    IdMessage.From.Address :='MyAdress@gmail.com';   // the same in (1)

 

Personnal Setting (gmail)  " Allow less secure apps "  :

1.png

 

2.png

 

3.png

  • Like 4
  • Thanks 1
  • Upvote 2
Link to comment
Share on other sites

  • 7 months later...

Sample Application Sending a gmail with:

  • Multiple Address
  • HTML Body
  • Attach File

 

image.png.d8184e6aa1b10f065fadff1f558ad9bf.png

 

Many thanks to Abaksoftfor his detailed post and sample.

// Use sample:

  Gmail := TGmail.Create('YourAccount@gmail.com', 'App password', 'From you/company');
  try
    try
      Gmail.Connect;
      Gmail.Send(['useremail@gmail.com'], 'Subject', 'PlainBody', 'htmlBody', 'AttachmentFile');
      Gmail.Send(...);
      Gmail.Send(...);
    except
      on E: Exception do
        ShowMessage(E.Message);
    end;
  finally
    GEmail.Free;
  end;

Atthached file:

Sending_gmail.7z

  • Like 4
Link to comment
Share on other sites

Hi,

Be aware that when you send a lot of mail VIA gmail (bulk mail), you will soon be blocked because of spam.
Much is then a relative concept because mail is monitored on the ip address / domain from which it is sent,
number per minute, hour, etc., sender and replay address, etc.

If you really want to use it for business to send mail from your application, you can better:
1. send mail from your own domain, so via the domain where your application is started.hosted
2. use an smtp / relay server
3. send mail via an account of the customer

Option 1: send from your own domain
You can then create an email address for each customer. Suppose your application domain name is "myapp.com". You have 2 customers who use your application. These are the companies:
- company-X (have their own website / mail)
- company-Z (have their own website / mail

You now create an e-mail address for every customer, namely:
- company-X@myapp.com
- company-Y@myapp.com

When the company uses the e-mail functionality within your application, they use the above e-mail address.
Mail will now be sent FROM your own domain name WITH the same domain e-mail address, namely @ myapp.com

The next step is to ensure that when a reply is given to this e-mail, it also reaches the relevant company.
After all, one answers to Company-X@myapp.com and the mail ends up in your mail environment.

You now put a forward line on the mailbox of company-X@myapp.com whereby the mail is forwarded to an email address
of the company, for example info@company-X.com.

Now the mail is forwarded to the company and the communication takes place directly between company and customer
who has replyd on that mail. Company-X must, however, submit an spf record in their web environment take from myapp.com.
This indicates that company-X trusts myapp.com when sending e-mail.

Option 2: use a relay server
This is the most reliable way of sending mail. A smtp provider has everything set up to send mail optimally, continuously adapting the changing
mail rules of eg gmail, hotmail, outlook, etc. All you have to do is create accounts per customer, etc. Examples of smtp / relay server providers
are for example (there are in every country):
- https://www.sendinblue.com/
- https://www.mailchimp.com/
- https://www.serversmtp.com/
- https://sendgrid.com/
- https://www.flowmail.com

You then pay an x amount for an x number of mails to be sent per month, usually it starts at 15.000-20,000 per month.

Option 3: use mail server client
This option is also applied, but may result in mail being seen as a spam.
You ask your customer to create an email address with a password. What you actually do is use this data to log in to the mail server
of the customer and send mail via that mail server.

All 3 options have advantages and disadvantages. Sending mail from a different mail address than your domain (where your application is located)
is often seen as spam. Especially when you are going to send multiple mails to the same e-mail domain within a certain time.
This specific time is different for each provider (gmail, hotmail, outlook).

Do not forget to include the correct header information in your mail, especially when you send mail using options 1 and 2.
Header information is important because the domain is often mentioned there and then you run the risk that app domain and
mail domain are different to be.

You also have a number of other settings such as Dkim, Dmarc, Tls that affect the security of mail.

Regards Peter

 

  • Like 1
  • Upvote 2
Link to comment
Share on other sites

Hi Mohammed,

Looks good and neat. But beware if you are going to send this on behalf of rivierea-tours from YOUR gmail account WITH THE SENDER read@riviera-tours.net.
Then it will be seen as spam. Also when the recipient gives a replay, the mail will not arrive at read@riviera-tours.net if nothing arranges. 
Maybe it is an example and in practice it is arranged differently

A number of guidelines apply to (bulk) mail:
- link to domains, not IP addresses in email html body.
- do not include JavaScript in email html body.
- do not include embedded Object (ActiveX / Flash) in email html body.
- don't include HTML forms in email html body.

Providers such as hotmail, gmail, yahoo impose many restrictions on sending e-mail. You do not have this when you send mail
from your own domain or take one of the options from my previous message

Some guidelines
https://support.google.com/mail/answer/81126
https://support.google.com/mail/thread/9912589?hl=en
https://help.yahoo.com/kb/emails-arent-received-yahoo-mail-sln28074.html

Regards Peter

 

Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...
On 2/6/2020 at 10:58 PM, Abaksoft said:

Personnal Setting (gmail)  " Allow less secure apps "  :

3.png

Hello everyone,

From May 30, 2022 Google had disabled this feature (allow less secure apps). The sample app will display a message that "Username and Password not accepted".

Do you have any workaround for using GMail?

Thank you

Link to comment
Share on other sites

Hello DFong,

Thank you for your answer.

Could you please explain further, where to use the app password?

I followed the link you gave, and now I have the 16 digit password.

But I don't know where to use the password:

  • If I use the password to login to my Google account in FireFox, and send email with the sample from Abaksoft from another tab in FireFox, it doesn't work
  • If I use the password as SMTP password for smtp.google.com, it doesn't work.

Thanks again.

Link to comment
Share on other sites

Hello,

I solved my problem by deleting the app password, and create another one.

I really don't know what is wrong with the first app password. But the second one worked!

I use the app password as the SMTP password in Abaksoft's sample successfuly.

 

So if anyone failed to use the app password, create a new one 😃

1 hour ago, jrp said:

Hello DFong,

Thank you for your answer.

Could you please explain further, where to use the app password?

I followed the link you gave, and now I have the 16 digit password.

But I don't know where to use the password:

  • If I use the password to login to my Google account in FireFox, and send email with the sample from Abaksoft from another tab in FireFox, it doesn't work
  • If I use the password as SMTP password for smtp.google.com, it doesn't work.

Thanks again.

 

Link to comment
Share on other sites

Yeah, it's been awhile but I somehow recall having the same behavior when I first tried it months ago as I also needed to create a password twice.  At the time, I thought it may have had something to do with the app password being machine specific, but I was never able to reproduce it as I'm successfully using the latest app password from 2 different machines.

Glad to hear it's working for you!

  • Thanks 1
Link to comment
Share on other sites

  • 6 months later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...