Solved

Email seend as attachment

Posted on 2000-05-15
19
200 Views
Last Modified: 2010-04-04
I want to make a delphi (3.0 or 5.0) program that will start and open a new mailmessage in outlook express and attach a certain document as attachment and fill the mail with some standard text. (Like in MS-word File-send)

Is this easy tto setup myself or do  need a special component trhat can help me do this ?
0
Comment
Question by:RichardBorst
  • 5
  • 4
  • 4
  • +3
19 Comments
 
LVL 5

Expert Comment

by:TheNeil
ID: 2810541
You can do this using various methods. The following example uses MAPI:

uses mapi;

FUNCTION SendEMail(Handle : THandle; Mail : TStrings):Cardinal;
TYPE
  TAttachAccessArray = ARRAY [0..0] OF TMapiFileDesc;
  PAttachAccessArray = ^TAttachAccessArray;
VAR
  MapiMessage : TMapiMessage;
  Receip      : TMapiRecipDesc;
  Attachments : PAttachAccessArray;
  AttachCount : INTEGER;
  iCount      : INTEGER;
  FileName    : STRING;
BEGIN
  fillChar(MapiMessage, SizeOf(MapiMessage), #0);
  Attachments := NIL;
  fillChar(Receip,SizeOf(Receip), #0);
  IF Mail.Values['to'] <> ''
  THEN
  BEGIN
    Receip.ulReserved := 0;
    Receip.ulRecipClass := MAPI_TO;
    Receip.lpszName := StrNew(PChar(Mail.Values['to']));
    Receip.lpszAddress := StrNew(PChar('SMTP:' + Mail.Values['to']));
    Receip.ulEIDSize := 0;
    MapiMessage.nRecipCount := 1;
    MapiMessage.lpRecips := @Receip;
  END;

  AttachCount := 0;
  FOR iCount := 0 TO MaxInt
  DO
  BEGIN
    IF Mail.Values['attachment' + IntToStr(iCount)] = ''
    THEN
      BREAK;
    AttachCount := AttachCount + 1;
  END;
 
  IF AttachCount > 0
  THEN
  BEGIN
    GetMem(Attachments,SizeOf(TMapiFileDesc) * AttachCount);
    FOR iCount := 0 TO (AttachCount - 1)
    DO
    BEGIN
      FileName := Mail.Values['attachment' + IntToStr(iCount)];
      Attachments[iCount].ulReserved := 0;
      Attachments[iCount].flFlags := 0;
      Attachments[iCount].nPosition := ULONG($FFFFFFFF);
      Attachments[iCount].lpszPathName := StrNew(PChar(FileName));
      Attachments[iCount].lpszFileName := StrNew(PChar(ExtractFileName(FileName)));
      Attachments[iCount].lpFileType := NIL;
    END;
    MapiMessage.nFileCount := AttachCount;
    MapiMessage.lpFiles := @Attachments^;
  END;

  IF Mail.Values['subject'] <> ''
  THEN
    MapiMessage.lpszSubject := StrNew(PChar(Mail.Values['subject']));
  IF Mail.Values['body'] <> ''
  THEN
    MapiMessage.lpszNoteText := StrNew(PChar(Mail.Values['body']));

  Result := MapiSendMail(0, Handle, MapiMessage,MAPI_DIALOG*Ord(Handle <> 0) OR MAPI_LOGON_UI OR MAPI_NEW_SESSION, 0);

  FOR iCount := 0 TO (AttachCount - 1)
  DO
  BEGIN
    strDispose(Attachments[iCount].lpszPathName);
    strDispose(Attachments[iCount].lpszFileName);
  END;

  IF assigned(MapiMessage.lpszSubject)
  THEN
    strDispose(MapiMessage.lpszSubject);
  IF assigned(MapiMessage.lpszNoteText)
  THEN
    strDispose(MapiMessage.lpszNoteText);
  IF assigned(Receip.lpszAddress)
  THEN
    strDispose(Receip.lpszAddress);
  IF assigned(Receip.lpszName)
  THEN
    strDispose(Receip.lpszName);
END;

procedure TForm1.Button1Click(Sender: TObject);
VAR
  mail : TStringList
BEGIN
  mail := TStringList.Create;
  mail.values['to']      := 'Bloke@Geeza.com';
  mail.values['subject'] := 'A subject';
  mail.values['body']    := 'Some body text (line 1)';
  mail.values['body']    := 'Some more body text (line 2)';
  mail.values['attachment0'] := 'c:\winnt\winnt256.bmp';
  mail.values['attachment1'] := 'c:\winnt\winnt.bmp';
  sendEMail(Application.Handle, mail);
  mail.Free;
END;

The Neil =:)
0
 
LVL 15

Expert Comment

by:simonet
ID: 2811533
Richard,

I've written a sample application (w/ full source code) that illustrates the usage of S-MAPI on a Delphi project.

You can download the project "How to send e-mail an faxes from a Delphi app"  from my site:
htp://www.bhnet.com.br/~simonet/howtoprojs.htm

Yours,

alex
0
 
LVL 1

Expert Comment

by:xsoft
ID: 2812114
listening...
0
 
LVL 1

Expert Comment

by:sageryd
ID: 2824737
Hi all!

I think there is a much simpler way, I havn't tried it though. This is just a shot in the dark, but if you write a WinExec() proc and execute something like: "mailto:name@domain.com?Subject?Body?Attachment", it might work. I know what I just wrote is not the proper format, but if you can figure out how it's written, you might get it to work on one line of source! I know it can be done this way without attachments, I really don't know if it'll even work....

Good luck!

--johan
0
 

Accepted Solution

by:
Chawa earned 200 total points
ID: 2828437
 use this (without attach - I don't know how exactly):

  uses ShellAPI;
  ........
  ShellExecute(ValidParentForm(Self).Handle,
    'Open',
    PChar('mailto:username@somehost.com?CC=and he&BCC=and her&Subject=Text Subject&Body=Body of message',
    nil, nil, SW_SHOWNORMAL);

or Delphi components for mail
0
 
LVL 1

Expert Comment

by:sageryd
ID: 2828615
Chawa: No offense, but here at E-E we usually post comments instead of answers unless you are completely sure that there isn't somebody else that could possibly come up with a better answer than yours. So please, I think I speak for everyone when I say this, for everybody's sake, convert your answer to a comment!

Cheers! Don't take it personal!

--johan
0
 
LVL 15

Expert Comment

by:simonet
ID: 2828944
Chawa:

Read the question again. Here's some quote from it: "and attach a certain document as attachment"

How is the user supposed to attach a document using ShellExecute?


Richard,

In order to use S-MAPI with Outlook Express, you have to set it as your default MAPI application. This setting can be found in the Options dialog.

Alex
0
 

Expert Comment

by:Chawa
ID: 2828996
I don't exactly but &Attach or something else maybe!?!

 I new in expertsexhange and I send few comments, people maybe use this comments and don't send me any points.
0
 
LVL 15

Expert Comment

by:simonet
ID: 2829167
>&Attach or something else maybe
That doesn't exist.

>people maybe use this comments and don't send me any points

Get used to it. Who said life is fair? Not getting any points is much better than step in everybody's toes, isn't it?
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 1

Expert Comment

by:sageryd
ID: 2829170
Chawa: I E-E people are honest with each other, otherwise it wouldn't work. EEven if you propose an answer, the person who put up the question has to evaluate it, and if he/she doesn't want to give out any points for the answer he/she could just deny the answer and use it anyway. And if you post a comment the same could happen, but, you also allow other users to propose answers that may be better than yours. btw, The person who put up the Q can actually accept a comment as an answer if he/she wants to.

Simonet: I think what Chawa is trying to say is that he thinks it may be possible to send a path to a file on the HDD and let the mailprogram itself look up this file and attach it if it exists. I agree with you though on the quote, Chawa didn't exactly answer the question fully.


--johan
0
 

Expert Comment

by:Chawa
ID: 2829212
Ok. I dont think about you or anything in this question (about points). I send comments to another question, and man why ask question probably use the comment.

I am sorry about this. I only one programmer.

Thanks seqeryd.

comment to 'Who said life is fair?' is :
I from Yugoslavia and survive Bombing Attack.
0
 

Expert Comment

by:Chawa
ID: 2829233
Chawa changed the proposed answer to a comment
0
 
LVL 1

Expert Comment

by:sageryd
ID: 2829338
Well done Chawa! (Both the answer>comment, and the attack!)

:o)

--johan
0
 
LVL 15

Expert Comment

by:simonet
ID: 2843534
>RichardBorst

So, what about the e-mail stuff? Please give us some feedback on how it worked out.

Alex
0
 
LVL 15

Expert Comment

by:simonet
ID: 2925898
Alex has changed his comment from Monday, May 15 2000 - 02:16PM BRT to an answer.
0
 

Author Comment

by:RichardBorst
ID: 3276075
Comment accepted as answer
0
 

Author Comment

by:RichardBorst
ID: 3276076
I Think this coukld be the solution. I do not have working it yet but it must be a key for the solution
0
 

Author Comment

by:RichardBorst
ID: 3459487
I tried also the solution from TheNeil using MAPI.

The Neil,

Does this works with netscape's messenger.

Richard
0
 

Author Comment

by:RichardBorst
ID: 3459499
The solution from chawa workes, but not with the attachment. I tries to find the right syntax on the internet, but attachments are not supported in the "mailto"-statement.
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
IExtractImage Delphi 14 207
Delphi with SQL Natvie Client 15 86
Delphi cmd execution 6 56
control image tags in a string ? 12 111
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

19 Experts available now in Live!

Get 1:1 Help Now