Solved

Email seend as attachment

Posted on 2000-05-15
19
199 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
Comment Utility
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
Comment Utility
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
Comment Utility
listening...
0
 
LVL 1

Expert Comment

by:sageryd
Comment Utility
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
Comment Utility
 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>&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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Expert Comment

by:sageryd
Comment Utility
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
Comment Utility
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
Comment Utility
Chawa changed the proposed answer to a comment
0
 
LVL 1

Expert Comment

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

:o)

--johan
0
 
LVL 15

Expert Comment

by:simonet
Comment Utility
>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
Comment Utility
Alex has changed his comment from Monday, May 15 2000 - 02:16PM BRT to an answer.
0
 

Author Comment

by:RichardBorst
Comment Utility
Comment accepted as answer
0
 

Author Comment

by:RichardBorst
Comment Utility
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
Comment Utility
I tried also the solution from TheNeil using MAPI.

The Neil,

Does this works with netscape's messenger.

Richard
0
 

Author Comment

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

9 Experts available now in Live!

Get 1:1 Help Now