Solved

sending a mail with attachment

Posted on 2004-10-10
15
682 Views
Last Modified: 2010-05-18
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
Comment
Question by:harry_haller
  • 3
  • 3
  • 2
  • +3
15 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
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
 
LVL 6

Accepted Solution

by:
vadim_ti earned 300 total points
Comment Utility
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
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
 

Author Comment

by:harry_haller
Comment Utility
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
 
LVL 11

Expert Comment

by:calinutz
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
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
 
LVL 11

Expert Comment

by:calinutz
Comment Utility
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
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
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
 
LVL 11

Expert Comment

by:calinutz
Comment Utility
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
 
LVL 2

Expert Comment

by:SaLz
Comment Utility
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
 
LVL 2

Expert Comment

by:SaLz
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now