Recent

Author Topic: Writing an email with gmail  (Read 8958 times)

max1893

  • Newbie
  • Posts: 3
Writing an email with gmail
« on: June 12, 2013, 08:34:10 pm »
Hi :)

I have a few problems with sending an email. I tried different things: synapse, pop3...
I want to send a gmail, only a string, nothing special..
Here is my source code:

Code: [Select]
procedure TForm1.Button3Click(Sender: TObject);
var
  sSmtpHost, sSmtpPort, sSmtpUser, sSmtpPasswd, sFrom, sTo, sFileName:ansistring;
begin
  sSmtpHost:='smtp.gmail.com';
 sSmtpPort:='587';
 sSmtpUser:='me';
 sSmtpPasswd:='******';
 sFrom:='myemail@gmail.com ';
 sTo:='myemail@gmail.com';
 sFileName:='E:\teschd.txt';
Mailsend(sSmtpHost, sSmtpPort, sSmtpUser, sSmtpPasswd, sFrom, sTo, sFileName);


end;
procedure TForm1.AddToLog(const a: string);
begin
  Memo3.Lines.Add(a);
end;

procedure TForm1.MailSend(const sSmtpHost, sSmtpPort, sSmtpUser, sSmtpPasswd, sFrom, sTo, sFileName: AnsiString);
var
  smtp: TSMTPSend;
  msg_lines: TStringList;
begin
  msg_lines := TStringList.Create;
  smtp := TSMTPSend.Create;
  try
    msg_lines.LoadFromFile(sFileName);
    msg_lines.Insert(0, 'From: ' + sFrom);
    msg_lines.Insert(1, 'To: ' + sTo);

    smtp.UserName := sSmtpUser;
    smtp.Password := sSmtpPasswd;

    smtp.TargetHost := sSmtpHost;
    smtp.TargetPort := sSmtpPort;


 AddToLog('SMTP Login');
    if not smtp.Login() then
      raise ESMTP.Create('SMTP ERROR: Login:' + smtp.EnhCodeString);

    AddToLog('SMTP StartTLS');
    if not smtp.StartTLS() then
      raise ESMTP.Create('SMTP ERROR: StartTLS:' + smtp.EnhCodeString);

    AddToLog('SMTP Mail');
    if not smtp.MailFrom(sFrom, Length(sFrom)) then
      raise ESMTP.Create('SMTP ERROR: MailFrom:' + smtp.EnhCodeString);
    if not smtp.MailTo(sTo) then
      raise ESMTP.Create('SMTP ERROR: MailTo:' + smtp.EnhCodeString);
    if not smtp.MailData(msg_lines) then
      raise ESMTP.Create('SMTP ERROR: MailData:' + smtp.EnhCodeString);

    AddToLog('SMTP Logout');
    if not smtp.Logout() then
      raise ESMTP.Create('SMTP ERROR: Logout:' + smtp.EnhCodeString);

    AddToLog('OK!');
  finally
    msg_lines.Free;
    smtp.Free;
  end;
end;                             

Recent Packages are load and they work...
Thanks for your help :)
« Last Edit: June 12, 2013, 09:13:09 pm by max1893 »

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Writing an email with gmail
« Reply #1 on: June 12, 2013, 09:02:47 pm »
...
Code: [Select]
...
  sSmtpHost:='smtp.gamil.com';     ???????????????????????????????????????????
...                       

max1893

  • Newbie
  • Posts: 3
Re: Writing an email with gmail
« Reply #2 on: June 12, 2013, 09:07:55 pm »
oh.. thanks:)
i corrected it, but it doesnt work....
it says SMTP error Mailfrom:permanent Failure-Invalide command.

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Writing an email with gmail
« Reply #3 on: June 12, 2013, 09:22:53 pm »
This works for me with gmail using Synapse:

Code: [Select]
uses
  ..., smtpsend,ssl_openssl;

function SendMail(User, Password, MailFrom, MailTo, SMTPHost, SMTPPort: string; MailData: string
  ): Boolean;
var
  SMTP: TSMTPSend;
  sl:TStringList;
begin
  Result:=False;
  SMTP:=TSMTPSend.Create;
  sl:=TStringList.Create;
  try
    sl.text:=Maildata;
    SMTP.UserName:=User;
    SMTP.Password:=Password;
    SMTP.TargetHost:=SMTPHost;
    SMTP.TargetPort:=SMTPPort;
    SMTP.AutoTLS:=true;
    if SMTPPort<> '25' then
      SMTP.FullSSL:=true;
    if SMTP.Login then
    begin
      result:=SMTP.MailFrom(MailFrom, Length(MailData)) and
         SMTP.MailTo(MailTo) and
         SMTP.MailData(sl);
      SMTP.Logout;
    end;
  finally
    SMTP.Free;
    sl.Free;
  end;
end;

max1893

  • Newbie
  • Posts: 3
Re: Writing an email with gmail
« Reply #4 on: June 12, 2013, 10:25:37 pm »
Thanks :)

Silvio Clécio

  • Guest
Re: Writing an email with gmail
« Reply #5 on: June 12, 2013, 10:40:58 pm »
I use XMailer plugin (it uses Synapse framework): https://github.com/silvioprog/xmailer
Works fine with Gmail, Hotmail, Yahoo etc.! :)

 

TinyPortal © 2005-2018