Solved

How to send a mail with an attachment with outlook from Delphi 7.

Posted on 2004-10-01
6
443 Views
Last Modified: 2010-04-05
Hi. Here is my first question:

I want to send a mail from Delphi7 throught Outlook.
I think is possible with TOutlookApplication object, but i can't find any documentation.
I only need to open a "new mail" outlook form, with an attachment, and some text on Subject.
Can i do that?.


0
Comment
Question by:Colorker
  • 3
  • 3
6 Comments
 
LVL 6

Accepted Solution

by:
vadim_ti earned 125 total points
ID: 12198405
you need to include unit Mapi.


function SendMail(const Subject, Body, FileName,
                  SenderName, SenderEMail,
                  RecipientName, RecipientEMail: string): Integer;
var
  msg: TMapiMessage;
  lpSender, lpRecipient: TMapiRecipDesc;
  FileAttach: TMapiFileDesc;

  SM: TFNMapiSendMail;
  MAPIModule: HModule;
begin
  FillChar(msg, SizeOf(msg), 0);
  with msg do
  begin
    if (Subject <> '') then
      lpszSubject := PChar(Subject);

    if (Body <> '') then
      lpszNoteText := PChar(Body);

    if (SenderEmail <> '') then
    begin
      lpSender.ulRecipClass := MAPI_ORIG;
      if (SenderName = '') then
        lpSender.lpszName := PChar(SenderEMail)
      else
        lpSender.lpszName := PChar(SenderName);
      lpSender.lpszAddress := PChar(SenderEmail);
      lpSender.ulReserved := 0;
      lpSender.ulEIDSize := 0;
      lpSender.lpEntryID := nil;
      lpOriginator := @lpSender;
    end;

    if (RecipientEmail <> '') then
    begin
      lpRecipient.ulRecipClass := MAPI_TO;
      if (RecipientName = '') then
        lpRecipient.lpszName := PChar(RecipientEMail)
      else
        lpRecipient.lpszName := PChar(RecipientName);
      lpRecipient.lpszAddress := PChar(RecipientEmail);
      lpRecipient.ulReserved := 0;
      lpRecipient.ulEIDSize := 0;
      lpRecipient.lpEntryID := nil;
      nRecipCount := 1;
      lpRecips := @lpRecipient;
    end
    else
      lpRecips := nil;

    if (FileName = '') then
    begin
      nFileCount := 0;
      lpFiles := nil;
    end
    else
    begin
      FillChar(FileAttach, SizeOf(FileAttach), 0);
      FileAttach.nPosition := Cardinal($FFFFFFFF);
      FileAttach.lpszPathName := PChar(FileName);

      nFileCount := 1;
      lpFiles := @FileAttach;
    end;
  end;

  MAPIModule := LoadLibrary(PChar(MAPIDLL));
  if MAPIModule = 0 then
    Result := -1
  else
    try
      @SM := GetProcAddress(MAPIModule, 'MAPISendMail');
      if @SM <> nil then
      begin
        Result := SM(0, Application.Handle, msg, MAPI_DIALOG or MAPI_LOGON_UI, 0);
      end
      else
        Result := 1;
    finally
      FreeLibrary(MAPIModule);
    end;
end;

0
 

Author Comment

by:Colorker
ID: 12198527
It works fine!!!.

Only one question:

I leave in blank Sendername, SenderEmail, recipientName and recipientEmail:

sendmail('Report Sales mailing','Your sales report is here.','c:\report001.pdf','','','','');

The recipient must be filled by the user in the Outlook form.
But... what about Sendername and Sendermail? can this fail in some cases?.

Thanks and sorry for my English.

 




0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12198534
i think it will not fail, but some providers will block such e-mails as spam
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.

 

Author Comment

by:Colorker
ID: 12198560
ups, only one more thing.

If in a future y process a large number of mail's to be send, can i do the process automatically filling all fields , (not outlook form pop-up)?
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12198578
I think if you will not set
MAPI_DIALOG
flag, e-mail client interface will not be running, test it
0
 

Author Comment

by:Colorker
ID: 12198662
oK.Thanks.
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

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

13 Experts available now in Live!

Get 1:1 Help Now