Solved

sending a mail with attachment

Posted on 2004-10-10
15
683 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
ID: 12271822
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
ID: 12271881
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12272735
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
ID: 12273864
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
ID: 12274193
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12274197
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
ID: 12274203
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
ID: 12274204
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
ID: 12287769
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
ID: 12708931
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
ID: 13458198
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi 10 Seattle Dataset Actions 5 78
Machine not responding during CopyFile() 3 88
Convert Jpg /PNG To GIF 5 118
Create a path if not exists 7 68
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

9 Experts available now in Live!

Get 1:1 Help Now