Using CDO to send mail. Mail not being sent until program exits or Outlook Express opened/closed

Using CDO to send mail. Mail not being sent until program exits or Outlook Express opened/closed

Post by Andrew Walk » Wed, 12 Feb 2003 15:54:47



Hi,

I have written a simple C# program to send out an email in both html
and plain
text format.  I am using CDO (v1.21) interop because it is my
understanding that the .NET classes will not support sending both html
and plain text formats at the same time.  I have everything working,
but there is one curiously annoying
"undocumented feature".  The mail is not actually sent until I exit
the program.
That is, it does not get sent after msg.Send();  I've been trying to
find a
"Flush" or "Purge" command with no luck.  I did see some newgroup
postings suggesting there is a bug with CDO that requires one to
reference Outlook and fake a button press of 'Send/Receive'.  Is this
really the case?  Can anyone say how you would do this if you are
using Outlook Express as your mail program?  If you do need to
use the Outlook object model, could someone post an example and the
references to include in the project?  I could not get the example
code I found working.  Hopefully, there is a more elegant solution.

Incidentally, beside simply exiting the dialog application,
opening/closing Outlook Express will also cause the message to be
sent.

Any ideas or pointers would be most appreciated. I am a beginner to
CDO/MAPI, so suggestions of tutorial/references would also be much
appreciated.

Thank you,

Andrew

I've included the relevant code:

private void cmdSend_Click(object sender, System.EventArgs e)
{
        CDO.Message msg = new CDO.MessageClass();

        // configure the message, smtp server etc.
        CDO.Configuration iConfg;
        ADODB.Fields oFields;
        ADODB.Field oField;
        iConfg = msg.Configuration;            
        oFields = iConfg.Fields;                
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
        oField.Value = 2 ; // use CDO.CdoConfiguration.cdoSMTPServer
        oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
        oField.Value = "mail.sbcglobal.net";//"smarthost"  ;
        oFields.Update();
        msg.Configuration = iConfg;

        // put in stuff from the GUI and html file
        msg.From        =        txtSenderEmail.Text;
        msg.To          =       txtTo.Text;

        String strTest = "";
        strTest = sw.ReadToEnd();
        sw.Close();
        // we will have both html and plain text for 'simple' mail
programs
        msg.HTMLBody = strTest;        
        msg.TextBody = txtBody.Text;

        msg.Subject = txtSubject.Text;

        msg.Send();

Quote:}