Send SMTP email with Office365 account

Download Source Code

This example connects to the smtp.office365.com host via the SMTP protocol, authorizes using the STARTTLS command, composes a new message, and sends it.

procedure TForm1.btnSendClick(Sender: TObject);
begin
  if (clSmtp1.Active) then Exit;
 
  clSmtp1.Server := 'smtp.office365.com';
  clSmtp1.Port := 587;
 
  clSmtp1.UserName := edtUser.Text;
  clSmtp1.Password := edtPassword.Text;
 
  clSmtp1.UseTLS := ctExplicit;
 
  clSmtp1.Open();
  try
    clMailMessage1.BuildMessage(memBody.Text, '');
    clMailMessage1.From.FullAddress := edtUser.Text;
    clMailMessage1.ToList.EmailAddresses := edtTo.Text;
    clMailMessage1.Subject := edtSubject.Text;
 
    clSmtp1.Send(clMailMessage1);
 
    ShowMessage('The message was sent successfully.');
  finally
    clSmtp1.Close();
  end;
end;

Confirm server credentials:

procedure TForm1.clSmtp1VerifyServer(Sender: TObject; ACertificate: TclCertificate; const AStatusText: string;
  AStatusCode: Integer; var AVerified: Boolean);
begin
  if not AVerified and (MessageDlg(AStatusText + #13#10' Do you wish to proceed ?',
    mtWarning, [mbYes, mbNo], 0) = mrYes) then
  begin
    AVerified := True;
  end;
end;

 

Add Feedback