Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

Email seend as attachment

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
RichardBorst
Asked:
RichardBorst
  • 5
  • 4
  • 4
  • +3
1 Solution
 
TheNeilCommented:
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
 
simonetCommented:
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
 
xsoftCommented:
listening...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sagerydCommented:
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
 
ChawaCommented:
 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
 
sagerydCommented:
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
 
simonetCommented:
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
 
ChawaCommented:
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
 
simonetCommented:
>&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
 
sagerydCommented:
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
 
ChawaCommented:
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
 
ChawaCommented:
Chawa changed the proposed answer to a comment
0
 
sagerydCommented:
Well done Chawa! (Both the answer>comment, and the attack!)

:o)

--johan
0
 
simonetCommented:
>RichardBorst

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

Alex
0
 
simonetCommented:
Alex has changed his comment from Monday, May 15 2000 - 02:16PM BRT to an answer.
0
 
RichardBorstAuthor Commented:
Comment accepted as answer
0
 
RichardBorstAuthor Commented:
I Think this coukld be the solution. I do not have working it yet but it must be a key for the solution
0
 
RichardBorstAuthor Commented:
I tried also the solution from TheNeil using MAPI.

The Neil,

Does this works with netscape's messenger.

Richard
0
 
RichardBorstAuthor Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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