• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3959
  • Last Modified:

Send Outlook Express emails in the background

Using Delphi7, I am attempting to send emails from my application. I know that there are many ways to do this easily, and in some parts of my app I am creating an email and having the new email form come up (so that the user can amend the email and then click "Send").

However, I need to perform some email tasks in the background and I do not want to use as smtp server due to it needing to be configured (host, username,etc...). I can get the email created and sent fine, without the new email form coming up BUT the Outlook Send/Receive form comes to the foreground and basically locks out my app until the emails are sent!

I have looked at locating the window handle and trying to minimise it, but this is not possible as the MAPI commands are outside of Delphi and the mapi.pas unit is only a wrapper for these functions.

Can anybody help???


Code snippet for current email sending is below:


procedure SendMailDialog(Recip, Subject, Body, Attach : string; ShowDialog : boolean);
var
  mapiMsg : TMapiMessage;
  smapiRecip : TMapiRecipDesc;
  mapiFileDetail : TMapiFileDesc;
  AFile : String;
begin

  FillChar(mapiMsg, SizeOf(mapiMsg), 0);
  FillChar(smapiRecip, SizeOf(smapiRecip), 0);
  FillChar(mapiFileDetail, SizeOf(mapiFileDetail), 0);

  smapiRecip.ulRecipClass := MAPI_TO;
  smapiRecip.lpszName := '';
  smapiRecip.lpszAddress := PChar(Recip);

  mapiFileDetail.lpszPathName := PChar(Attach);
  AFile := ExtractFilename(Attach);
  mapiFileDetail.lpszFileName := PChar(AFile);

  mapiMsg.lpszSubject := PChar(Subject);
  mapiMsg.lpszNoteText := PChar(Body);
  mapiMsg.nRecipCount := 1;
  mapiMsg.lpRecips := @smapiRecip;
  mapiMsg.nFileCount := 1;
  mapiMsg.lpFiles := @mapiFileDetail;

  if ShowDialog then
    MapiSendMail(0, 0, mapiMsg, MAPI_LOGON_UI+MAPI_DIALOG, 0)
  else
  begin
    MapiSendMail(0, 0, mapiMsg, 0, 0);
  end;
 
end;
0
JONNY7469
Asked:
JONNY7469
  • 9
  • 6
  • 3
  • +1
1 Solution
 
Eddie ShipmanAll-around developerCommented:
Did you try using Mike Shkolnik's MAPI Mail components?
http://www.scalabium.com/download/mapimail.zip
0
 
JONNY7469Author Commented:
Thanks for the link Eddie.

Will download and give it a go.....
0
 
developmentguruCommented:
I use Indy components (come with Delphi).  The TidSMTP component sends emails without bringing up a screen.  Here is a code snippet (idMessage1 is type TidMessage, IdSMTP1 is typ eTIdSMTP).  I had the Host property set to "mail.ABC.com" on the TIdSMTP component during design.

    IdMessage1.Clear;
    IdMessage1.From.Address := 'notify@ABC.com';
    IdMessage1.From.Name := 'programnotification@ABC.com';
    IdMessage1.Recipients.Add.Address := ReportQuery.Fields[5].AsString;
    IdMessage1.CCList.Add.Address := XXXXXX@ABC.com';
    IdMessage1.CCList.Add.Address := YYYYYY@DEF.com';
    IdMessage1.Subject := ReportQuery.Fields[7].AsString;
    IdMessage1.Body.Add(ReportQuery.Fields[8].AsString);

    try
      IdSMTP1.Connect;  //mail.complexitymanager.com
      try
        try
          IdSMTP1.Send(IdMessage1);
        except
          on e:Exception do
            begin
              MemoNotificationsActivity.Lines.Insert(0, '-----Error sending ' +
                'notification:' + e.Message + '-----');
              raise;
            end;
        end;
      finally
        IdSMTP1.Disconnect;
      end;

the email addresses and site names have been modified to protect the innocent.

I hope it helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Eddie ShipmanAll-around developerCommented:
developmentguru, he said he doesn't want to have to setup server info.

[quote]
...I do not want to use as smtp server due to it needing to be configured (host, username,etc...)....
[/quote]


0
 
JONNY7469Author Commented:
Thanks developmentguru, but Eddie is right - the smtp server needs to be configured - meaning that users of my software would need to enter their smtp info. Most users are not technical in anyway way - so this would not be practical.

As there any public smtp servers that can be set up and used? is that mail.ABC.com actually a real SMTP server?


Thanks. :-)
0
 
Eddie ShipmanAll-around developerCommented:
I can give you an alternative but it's ging to cost you a Hotmail-Plus account.
See my two articles on accessing Hotmail from Delphi here:
http://www.delphipages.com/news/detaildocs.cfm?ID=137
http://www.delphipages.com/news/detaildocs.cfm?ID=138

Source code is avail here:
http://www.delphipages.com/result.cfm?ID=4615

You can setup a HMPlus account and your app can use it to send email
instead of using MAPI or SMTP.
0
 
developmentguruCommented:
You can use free email services for this (there are a bunch of them out there).  I use our corporate account.  Either way, this alleviates the need for the users to set it up.
0
 
Eddie ShipmanAll-around developerCommented:
But accessing them from Delphi, that is the question.
0
 
JONNY7469Author Commented:
Thanks for all the help guys, unfortunately I think there is no easy way round this.

What I would ideally have wanted is to create an email in mapi and have it sent via the mapi client (in this case Outlook Express). In this way Outlook Express handles the mail ( can use standard signatures, keep in Sent Items, etc..) which makes life easier.

The problem with all of the solutions above is that they use the mapi SendMail function - and this causes Outlook Express to pop up the Send/Receive form - which can't be minimised or closed until all emails are sent!!!

I think I am going to have issues here. :-(
0
 
Eddie ShipmanAll-around developerCommented:
Did you not look at the Hotmail option?
0
 
JONNY7469Author Commented:
Hi Eddie.

The hotmail option does not give me the emails in the "Sent" folder, nor would it provide the auto-signature feature from Outlook. Also, with mapi - an internet connection would not be necesary- emails would simply be queued up until a connection was made available.

Don't we just love integrating with Microsoft software?!!? :-D

Thanks,


Jon.
0
 
developmentguruCommented:
Ok, now that I understand what you are looking for...  Check out this link.

http://www.outlookcode.com/article.aspx?ID=37

  Following the advice in here should give you easy access to all of the automation objects that Outlook supports.  Looking at the BDS 2006 components there is a TOutlookApplication components you can use (out of the box).  I do not see any such components in Delphi 2007, but normal OLE automation techniques will work without using prebuilt components too.

Let me know if you need more.
0
 
Eddie ShipmanAll-around developerCommented:
developmentguru, Outlook EXPRESS is NOT automatable using COM.

I will work on this sometime today using MAPI to see if I can get a solution.
0
 
JONNY7469Author Commented:
OK guys.

I have the system working ok with Outlook Express as it's quite easy to change security settings and the emails will go nicely. However, with Outlook there is a problem. Using the same method I get an error saying "none of your email accounts could send this message" - no idea why - the message appears in the outbox fine and does look ok!!

Anyway, I can use COM if the client is using outlook. BUT.... there is still the annoying warning messages that come up when my app tries to send the email - I can see no way of turning that off in Outlook - any ideas how to turn it off or bypass it?


Thanks.
0
 
Eddie ShipmanAll-around developerCommented:
Google for "Outlook Redemption" to handle the security issues with Outlook.
0
 
JONNY7469Author Commented:
Thanks Eddie, looks like that would do it for me.

An aside question - as it seems like you have top knowledge of Outlook objects -

How can I send an email "to" and "bcc" at the same time using MAPI? I have looked at the Delphi MAPI.pas file and it seems like the TMapiMessage object only has 1 TMapiRecipDesc object. The TMapiRecipDesc needs a type set for it (ulRecipClass=MAPI_To, MAPI_BCC, etc...) and so I can not see how both To and BCC can be added to a message by using the object structure.

Any help appreciated.... :-)


Thanks.
0
 
Eddie ShipmanAll-around developerCommented:

Recip := MailItem.Recipients.Add("some name or address");
Recip.Type := olBCC;

Open in new window

0
 
Eddie ShipmanAll-around developerCommented:
You can add more than 1...
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now