Link to home
Start Free TrialLog in
Avatar of AndrewNixon
AndrewNixon

asked on

Sending Emails silently from Delphi

Hi,

I am trying to send an email from within delphi, silently, without using SMTP. I am currently using MAPI calls which work well except that when sending to a client using Outlook Express and a Linux mail server, the attachments come through as garbage or 'winmail.dat' and a wierd MIME type header.
All of the examples I have looked at pass through NIL as the filetype for the attachments sent to MAPI. Does anyone have any alternatives to using MAPI or know how to solve the problem with the attachments.

Any help would be much appreciated.

Thanks
Avatar of calinutz
calinutz
Flag of Romania image

Using SMTP you can have it like this:
// the listBox1 contains paths to the files you want to attach to the email
// the Memo1 contains the body of your message
// imeil is the tSMTP component
// I did not understand why not use SMTP?

begin
if imeil.Connected=true then
begin
 imeil.Disconnect;
 imeil.ClearParameters;

 imeil.Host:='yourhost';
 imeil.UserID:='yourUserID'
 imeil.PostMessage.FromAddress:='your from address';
 imeil.PostMessage.FromName:='your name';

 imeil.PostMessage.Subject:='your subject';
 imeil.PostMessage.ToAddress.Clear;
 imeil.PostMessage.ToAddress.Add('your To address');
 imeil.PostMessage.Attachments.Clear;

 imeil.PostMessage.Attachments.AddStrings(ListBox1.Items);
 imeil.PostMessage.Body.Clear;
 imeil.PostMessage.Body.AddStrings(Memo1.Lines);
end
  else
  begin
 imeil.ClearParameters;


 imeil.Host:='yourhost';
 imeil.UserID:='yourUserID'
 imeil.PostMessage.FromAddress:='your from address';
 imeil.PostMessage.FromName:='your name';

 imeil.PostMessage.Subject:='your subject';
 imeil.PostMessage.ToAddress.Clear;
 imeil.PostMessage.ToAddress.Add('your To Address');
 imeil.PostMessage.Attachments.Clear;
 imeil.PostMessage.Attachments.AddStrings(ListBox1.Items);
 imeil.PostMessage.Body.Clear;
 imeil.PostMessage.Body.AddStrings(Memo1.Lines);
 imeil.Connect;
  end;
 imeil.SendMail;
Close;
end;
ASKER CERTIFIED SOLUTION
Avatar of calinutz
calinutz
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AndrewNixon
AndrewNixon

ASKER

Thanks for the reply, but SMTP does not help us.  We can already send using either SMTP or MAPI, the problem is that our client wants to have the emails in their 'Sent items' folder on the local machine doing the sending, thus, MAPI must be used unless there is a way to get messages sent via SMTP into 'sent items' ?

Thanks
calinutz - Thanks for the link, while there is lot of info there, it all involves sending mails by bypassing the local mail program.  I want to use the local mail program (hence the use of MAPI) in order to get it into the client's sent items folder.

Thanks
Avatar of Eddie Shipman
If you want to send it silently, why would you also want it in the client's sent folder? LOL!!

I wrote the article on Hotmail and I am working on doing attachments, too.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> If you want to send it silently, why would you also want it in the client's sent folder? LOL!!

I'm with u on that one Eddie! lol
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ashok111 - Thanks for your code, it looks almost identical to my own (excpet that we can't use the MAPI_DIALOG flag), and works fine for normal situations, just not with the Linux mail server and Outlook express client.  I suspect it has something to do with the 'lpFileType' property of the TMapiFileDesc, but I don't know how to set it's properties correctly and all of the examples I have seen just set it to NIL or ignore it completely.
I found this from Microsoft site.  It is in VB.
It has a way to send e-mail to the outbox.
Also, the code is very well commented.
Unfortunately it is not in Delphi.

Sub MapiSendMail()
   Dim objSession As Object
   Dim objMessage As Object
   Dim objRecipient As Object
   Dim sProfile As String
   Dim sSubjPrmpt As String
   Dim sTextPrmpt As String
   Dim sEmailPrmpt As String
   Dim sMsgTitle As String
   ' Leaving sProfile equal to Null will
   ' force the user to select which Mapi
   ' profile to use. To keep from being
   ' prompted, you must supply a valid
   ' user profile.
   sProfile = ""
   sEmailPrmpt = "Enter valid Email Name of message recipient:"
   sSubjPrmpt = "Enter the subject line for this message:"
   sTextPrmpt = "Enter the text for this message:"
   sMsgTitle = "Mapi Macro Example"
   ' Create the Session Object.
   Set objSession = CreateObject("mapi.session")
   ' Log on using the session object.
   ' Specify a valid profile name if you want to
   ' avoid the logon dialog box.
   objSession.Logon profileName:=sProfile
   ' Add a new message object to the OutBox.
   Set objMessage = objSession.Outbox.Messages.Add
   ' Set the properties of the message object.
   objMessage.Subject = InputBox(sSubjPrmpt, sMsgTitle)
   objMessage.Text = InputBox(sTextPrmpt, sMsgTitle)
   ' Add a recipient object to the objMessage.Recipients collection.
   Set objRecipient = objMessage.Recipients.Add
   ' Set the properties of the recipient object.
   objRecipient.Name = InputBox(sEmailPrmpt, sMsgTitle)
   objRecipient.Resolve
   ' Send the message. Setting showDialog to False
   ' sends the message without displaying the message
   ' or requiring user intervention. A setting of True
   ' displays the message and the user must choose
   ' to Send from within the message dialog.
   objMessage.Send showDialog:=False
   MsgBox "Message sent successfully!"
   ' Log off using the session object.
   objSession.Logoff
End Sub

' Add an attachment (assumes "myfile.doc" exist in c:\)
 
 objMessage.Attachments.Add("MyFile.doc", 0, 1, "c:\myfile.doc")

was missing from above example
Thanks,  I'll try and do this in Delphi.
Why not just use Mike Shkolnik's free SimpleMAPI control:
http://www.scalabium.com
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial