How to send a letter using Delphi and Synapse library?

Synapse library in Delphi
image_pdfimage_print

Dear readers! On this website there was a post how to get values of all elements of Edit of the dialogue form using Delphi. Two posts described how to get IP address using Delphi here and here. Let’s start by downloading Synapse library from here: synapse.ararat.cz/doku.php.

Unpack the library into a temporary catalogue. Create catalogue D:\Delphi\Synapse\. It would be better if catalogues names contain Latin letters and no spaces. Copy the catalogue content source\lib from the unpacked archive into catalogue D:\Delphi\Synapse\. Now install Synapse library.

If you have Delphi 5-7, then:

  • File -> Close All.
  • Tools -> Environment Options. In the dialogue appeared find bookmark Library.
  • Find words Browsing path. Clock the button […] on the right.
  • In the dialogue find button […]. Click it; there will appear a window to select the catalogue, find our catalogue D:\Delphi\Synapse\, select it and press button OK.
  • Add selected catalogue by clicking Add and OK to close the window.
  • Now words Library Path.
  • Find words Library Path. Clock the button […] on the right.
  • In the dialogue find button […]. Click it; there will appear a window to select the catalogue, find our catalogue D:\Delphi\Synapse\, select it and press button OK.
  • Add selected catalogue by clicking Add and OK to close the window.

For Delphi 2006-2007 this is done as follows:

  • File -> Close All.
  • Tools -> Options. In the dialogue appeared, in a tree on the left
    find Environment options -> Delphi Options -> Library – Win32.
  • Find words Browsing path. Clock the button […] on the right.
  • In the dialogue find button […]. Click it, there will appear a window to select the catalogue, find our catalogue D:\Delphi\Synapse\, select it and press button OK.
  • Add selected catalogue by clicking Add and OK to close the window.
  • Now words Library Path.
  • Find words Library Path. Clock the button […] on the right.
  • In the dialogue find button […]. Click it, there will appear a window to select the catalogue, find our catalogue D:\Delphi\Synapse\, select it and press button OK.
  • Add selected catalogue by clicking Add and OK to close the window.

Now let’s write a code to send e-mail. Create new project. In order to send a letter it is necessary to use smtp protocol. In the Synapse library there are smtpsend, mimemess, mimepart modules needed for this. Add these libraries:

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogues,
  StdCtrls, httpsend, mimemess, mimepart, smtpsend;

Write the following procedure:

Procedure SendMail (Host, Subject, pTo, From , TextBody, 
				HTMLBody, login,password : string);
var Msg : TMimeMess; // message
    StringList : TStringList; // letter contents
    MIMEPart : TMimePart; // parts of the message (for future)
begin
  Msg := TMimeMess.Create; // create new message
  StringList := TStringList.Create;
  try
	// Add headings
    Msg.Header.Subject := Subject;// Subject of the message 
    Msg.Header.From := From; // name and address of a sender 
    Msg.Header.ToList.Add(pTo); // name and address of a receiver 
	// create root element 
    MIMEPart := Msg.AddPartMultipart('alternative', nil);
    if length(TextBody)=0 then 
	// If format is HTML
      begin 
        StringList.Text := HTMLBody;
        Msg.AddPartHTML(StringList, MIMEPart);
      end
    else
	// if it is a text format 
      begin 
        StringList.Text := TextBody;
        Msg.AddPartText(StringList, MIMEPart);
      end;
	// Encode and send 
    Msg.EncodeMessage;
	// Send.
    if smtpsend.SendToRaw(From,pTo,Host,Msg.Lines,login,password) then 
        ShowMessage('Your letter was sent')
    else
        ShowMessage('Your letter was not sent'); 
 finally
   Msg.Free;
   StringList.Free;
 end;
end;

Put a button on the form and write in the event handler the following:

procedure TForm1.Button1Click(Sender: TObject);
begin
    SendMail('smtp.yandex.ru',
        'Subject of the text letter',
        '"' + 'letter_to' + '" <' + [email protected]' + '>',
        '"' + 'letter_from' + '" <' + [email protected]' + '>',
        'Content of the letter in a text format',
        '',
        'login_smtp', 'password_smtp') 
end;
3 replies
  1. Brent
    Brent says:

    Is there a reason why you are using small gray text on a white background? It is very hard to read. Please change to black text if you are using a gray/white background. My eyes will thank your for it. 🙂

    Reply
  2. Canada Goose USA Free Shipping
    Canada Goose USA Free Shipping says:

    I’m not positive the place you’re getting your information, however good topic. I must spend some time learning more or understanding more. Thank you for excellent information I used to be in search of this information for my mission.

    Reply
  3. Mens Moncler Coat
    Mens Moncler Coat says:

    I have been browsing on-line greater than three hours lately, but I never found any fascinating article like yours. It is pretty worth enough for me. In my view, if all web owners and bloggers made excellent content material as you probably did, the web will probably be much more useful than ever before.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *