• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 698
  • Last Modified:

sending a mail with attachment

I wanna know what do I have to do to make my program send a mail and modify the subject, the body text and the attachement too. The program running in my computer has to send a file to an account but I dont even know how to send a mail and I guess its different if you wanna add an attachment. Please post full code, not just a part of it.
Thank you.
0
harry_haller
Asked:
harry_haller
  • 3
  • 3
  • 2
  • +3
1 Solution
 
Wim ten BrinkCommented:
I assume you want to do this in raw code? Else just use the Indy SMTP/POP3 components. The other question is if you want to use MAPI instead, which is basically a simpler technique based on ActiveX controls in Outlook or Outlook Express.
0
 
Ivanov_GCommented:
function TfrmMain.SendMail(FileToSend: String): String;
var
  MailMsg        : TidMessage;
  MailFile       : TidAttachment;
begin
  // send the mail with the attachment
  MailMsg := TidMessage.Create(idSMTP);
  // from
  MailMsg.From.Address := 'g_i_ivanov@mail.bg'; //GetOption('FromAddress');
  MailMsg.From.Name    := 'g_i_ivanov@mail.bg'; //Application.Title;
  // to
  MailMsg.Recipients.Add;
  MailMsg.Recipients.Items[0].Address := 'g_i_ivanov@mail.bg'; //GetOption('ToAddress');
  //
  MailMsg.Subject      := 'Screenshot created on ' +
                          FormatDateTime('dd.mm.yyyy hh:nn:ss', Now);
  // the attachment
  MailFile             := TidAttachment.Create(MailMsg.MessageParts, FileToSend);
  MailFile.DisplayName := MailMsg.Subject;

  // connect to server
  try
    idSMTP.Connect();
  except
    on E : Exception do
      Exit;
  end;

  // send
  try
    idSMTP.Send(MailMsg);
  except
    on E: Exception do
       Exit;
  end;

  // close connection and free memory
  idSMTP.Disconnect;
  MailFile.Free;
  MailMsg.Free;
end;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
harry_hallerAuthor Commented:
Ivanov_G:
Please, post all the code cause I dont want to have problems with the uses or other things.
Actually, it gives me some errors and I think that might be the reason.
Thanks.
0
 
calinutzCommented:
Now the code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Psock, NMsmtp, Jpeg, Clipbrd,
  Gauges;

type
  Temail1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Memo1: TMemo;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Label5: TLabel;
    Label6: TLabel;
    imeil: TNMSMTP;
    FlatGauge1: TGauge;
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure imeilSuccess(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  email1: Temail1;

implementation

{$R *.dfm}

procedure Temail1.BitBtn2Click(Sender: TObject);
begin
Close;
end;

procedure Temail1.BitBtn1Click(Sender: TObject);
var
         s:string;
  jpgImg: TJPEGImage;
begin
//xxxxxxxxxxxxxxxxxxxx
flatgauge1.Progress:=0;
Label6.Caption:='Verific dir. Grafice';
if directoryexists('C:\Grafice')=false then
createdir('C:\Grafice');

Label6.Caption:='Generez graficul';

s:='c:\test.txt';
Label6.Caption:='Salvez graficul jpg';
flatgauge1.Progress:=25;
//xxxxxxxxxxxxxxxxxxxx
Label6.Caption:='Pregatesc email-ul';

 imeil.ClearParameters;
 imeil.PostMessage.Subject:=Edit2.Text;
 imeil.PostMessage.ToAddress.Clear;
 imeil.PostMessage.ToAddress.Add(Edit1.Text);
 imeil.PostMessage.Attachments.Clear;
 imeil.PostMessage.Attachments.Add(s);
 imeil.PostMessage.Body.Clear;
 imeil.PostMessage.Body.AddStrings(Memo1.Lines);
//xxxxxxxxxxxxxxxxxxxx
flatgauge1.Progress:=45;
Label6.Caption:='Conecting to email';
 imeil.Connect;
flatgauge1.Progress:=60;
Label6.Caption:='Sending email...';
 imeil.SendMail;
flatgauge1.Progress:=100;
Label6.Caption:='Done';

end;

procedure Temail1.imeilSuccess(Sender: TObject);
begin
ShowMessage('Mesaje sent successfully ');
imeil.Disconnect;
Close;
end;

end.
0
 
Ivanov_GCommented:
1) drop a TIdSMTP component on your form (found on Indy Clients palette)
2) use the code below

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdMessageClient, IdSMTP, idMessage;

type
  TForm1 = class(TForm)
    IdSMTP1: TIdSMTP;
  private
    function SendMail(FileToSend: String): String;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.SendMail(FileToSend: String): String;
var
  MailMsg        : TidMessage;
  MailFile       : TidAttachment;
begin
  // send the mail with the attachment
  MailMsg := TidMessage.Create(idSMTP1);
  // from
  MailMsg.From.Address := 'g_i_ivanov@mail.bg'; //GetOption('FromAddress');
  MailMsg.From.Name    := 'g_i_ivanov@mail.bg'; //Application.Title;
  // to
  MailMsg.Recipients.Add;
  MailMsg.Recipients.Items[0].Address := 'g_i_ivanov@mail.bg'; //GetOption('ToAddress');
  //
  MailMsg.Subject      := 'Screenshot created on ' +
                          FormatDateTime('dd.mm.yyyy hh:nn:ss', Now);
  // the attachment
  MailFile             := TidAttachment.Create(MailMsg.MessageParts, FileToSend);
  MailFile.DisplayName := MailMsg.Subject;

  // connect to server
  try
    idSMTP1.Connect();
  except
    on E : Exception do
      Exit;
  end;

  // send
  try
    idSMTP1.Send(MailMsg);
  except
    on E: Exception do
       Exit;
  end;

  // close connection and free memory
  idSMTP1.Disconnect;
  MailFile.Free;
  MailMsg.Free;
end;

end.
0
 
calinutzCommented:
You must use your own:

host : www.email.com (or IP)
userid:  userid

from address: obvious
from name:    obvious

Ignore those I placed in they are only to fill the space so that you know where to input your data.

And by the way: you will need to have a file named test.txt on the root of your c drive (C:\test.txt)
0
 
Ivanov_GCommented:
I forgot to say that for IdSMTP1 you should adjust our SMTP server, for example the one you use as outgoing server in Outlook. If it requires authentification - set username and password too..
0
 
calinutzCommented:
You have the source code above. And afterwards you will need to change some properties to use your own email provider and your account.
So here's the form:


object email1: Temail1
  Left = 349
  Top = 117
  BorderStyle = bsSingle
  Caption = 'Email to:'
  ClientHeight = 293
  ClientWidth = 422
  Color = 16768407
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsStayOnTop
  OldCreateOrder = False
  Position = poDesktopCenter
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 2
    Top = 28
    Width = 84
    Height = 13
    Caption = 'Email destination:'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = 10485760
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label2: TLabel
    Left = 2
    Top = 60
    Width = 65
    Height = 13
    Caption = 'Email subject '
    Font.Charset = DEFAULT_CHARSET
    Font.Color = 10485760
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label3: TLabel
    Left = 2
    Top = 92
    Width = 37
    Height = 13
    Caption = 'Message'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = 10485760
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label5: TLabel
    Left = 48
    Top = 7
    Width = 36
    Height = 13
    Caption = 'Activity'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label6: TLabel
    Left = 268
    Top = 8
    Width = 38
    Height = 13
    Caption = 'waiting...'
  end
  object FlatGauge1: TGauge
    Left = 96
    Top = 4
    Width = 165
    Height = 17
    Progress = 0
  end
  object Edit1: TEdit
    Left = 2
    Top = 40
    Width = 417
    Height = 19
    Ctl3D = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentCtl3D = False
    ParentFont = False
    TabOrder = 0
    Text = 'destination@email.com'
  end
  object Edit2: TEdit
    Left = 2
    Top = 72
    Width = 417
    Height = 19
    Ctl3D = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentCtl3D = False
    ParentFont = False
    TabOrder = 1
    Text = 'Subject'
  end
  object Memo1: TMemo
    Left = 2
    Top = 104
    Width = 417
    Height = 137
    Ctl3D = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentCtl3D = False
    ParentFont = False
    TabOrder = 2
  end
  object BitBtn1: TBitBtn
    Left = 44
    Top = 244
    Width = 177
    Height = 45
    Caption = 'Send e-mail with attachment'
    TabOrder = 3
    OnClick = BitBtn1Click
  end
  object BitBtn2: TBitBtn
    Left = 224
    Top = 244
    Width = 113
    Height = 45
    Caption = 'Quit'
    TabOrder = 4
    OnClick = BitBtn2Click
  end
  object imeil: TNMSMTP
    Host = 'www.yourhost.com'
    Port = 25
    ReportLevel = 0
    UserID = 'yourUserID'
    PostMessage.FromAddress = 'YourEmailAddress@yourHost.com'
    PostMessage.FromName = 'Your Name'
    EncodeType = uuMime
    ClearParams = True
    SubType = mtPlain
    Charset = 'us-ascii'
    Left = 28
    Top = 244
  end
end
0
 
SaLzCommented:
for the host I surgest that you use your ISP SMTP Server. if you don't know your ISP SMTP, search it on google or visit there web site for POP & SMTP Servers.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdMessage, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP;

type
  TForm1 = class(TForm)
    IdSMTP1: TIdSMTP;
    IdMessage1: TIdMessage;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  IdMessage1.Clear;
  IdSMTP1.Host := 'mail.domain.com';
  IdSMTP1.Port := 25;
  IdMessage1.From.Address := 'from@domain.com';
  IdMessage1.Recipients.EMailAddresses := 'recipients@domain.com';
  IdMessage1.Subject := 'subject';
  IdMessage1.Body.Text := 'body';
  TIdAttachment.Create(IdMessage1.MessageParts, 'C:\1.txt');
try
      IdSMTP1.Connect(1000);
      IdSMTP1.Send(IdMessage1);
finally
    if IdSMTP1.Connected then IdSMTP1.Disconnect;
end;


end;

end.
=================
object Form1: TForm1
  Left = 575
  Top = 279
  Width = 123
  Height = 97
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 0
    Top = 32
    Width = 75
    Height = 25
    Caption = 'Send Mail'
    TabOrder = 0
    OnClick = Button1Click
  end
  object IdSMTP1: TIdSMTP
    MaxLineAction = maException
    ReadTimeout = 0
    Port = 25
    AuthenticationType = atNone
  end
  object IdMessage1: TIdMessage
    AttachmentEncoding = 'MIME'
    BccList = <>
    CCList = <>
    Encoding = meMIME
    Recipients = <>
    ReplyTo = <>
    Left = 32
  end
end
0
 
SaLzCommented:
how ironic, admin accepted a link to "How to send a mail with an attachment with outlook from Delphi 7" when the user wanted to send a email with his program, i.e. SMTP. now admin thinks its better to use outlook than his own program.

when Ivanov_G and the rest of us have give him SMTP code and he wanted that in full code, it gives email and body and attachments within your program,

admins must have been tired with all that mouse clicking.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now