Team LiB
Previous Section Next Section

Sending E-Mail

This section shows how to create e-mail using some Office and Exchange COM objects:

Each of these components is unique, but they have overlapping functionality. For instance, CDO is a COM wrapper to MAPI. You can review the following MSDN link for more information:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncdsys/html/cdo_roadmap.htm
Code Example: Sending E-mail Using CDOEx
Start example

using System;
using CDO;
using ADODB;

namespace Client.Chapter_19___Office_Integration
{
          class TestClass
          {
                 static void Main(string[] args)
                 {
                        Message MyMessage = new MessageClass();
                        Configuration MyConfig = MyMessage.Configuration;
                        Fields MyFields = MyConfig.Fields;
                        MyFields[@http://schemas.microsoft.com/cdo/"
                          + "configuration/sendusing"].Value = 2;
                        MyFields[@http://schemas.microsoft.com/cdo/
                          + "configuration/smtpserverport"].Value = 25;
                        MyFields[@http://schemas.microsoft.com/cdo/
                         + "configuration/smtpserver"].Value = "smarthost";
                        MyFields.Update();
                        MyMessage.Configuration = MyConfig;
                        MyMessage.TextBody = "This is a test message";
                        MyMessage.Subject = "Testing";
                        MyMessage.From = "gregmcb@microsoft.com";
                        MyMessage.To = "pmacbeth@comporium.com";
                        MyMessage.Send();
               }
          }
}
End example
Code Example: Sending E-mail Using CDO 1.21
Start example

using System;
using System.Reflection;

namespace Test
{
          class TestClass
          {
                [STAThread]
                static void Main(string[] args)
                {
                       Object vEmpty = Missing.Value;
                       string sReplyUserAlias = "ReplyTMyRecipient";
                       try
                       {
                            //Create MAPI session and logon
                             MAPI.Session MySession = new MAPI.Session();
                             MySession.Logon(vEmpty, vEmpty, true,
                                true, 0, true, vEmpty);

                            //Get the Outbox
                             MAPI.Folder MyFolder = (MAPI.Folder)MySession.Outbox;
                             Console.WriteLine("Folder: {0}", MyFolder.Name);
                            //Create a new message
                             MAPI.Messages MyMsgs = (MAPI.Messages)MyFolder.Messages;
                              MAPI.Message MyMsg = (MAPI.Message)MyMsgs.Add(
                               vEmpty, vEmpty, vEmpty, vEmpty);
                             MyMsg.Subject = "Send Using C#";
                             MyMsg.Text = "Hello World";
                            //Add a recipient
                             MAPI.Recipients MyRecips =
                                (MAPI.Recipients)MyMsg.Recipients;
                             MAPI.Recipient MyRecip = (MAPI.Recipient)MyRecips.Add(
                                 vEmpty, vEmpty, vEmpty, vEmpty);
                             MyRecip.Name = "TestRecipient";   //TODO: Set recipient
                             MyRecip.Resolve(false);
                            //Set the user to whom mail will be replied
                            //We can't create a new recipient object,
                            //so we add this recipient to the
                            //message just long enough to resolve it.
                             MAPI.Recipient MyTempRecip =
                               (MAPI.Recipient)MyRecips.Add(
                                   vEmpty, vEmpty, vEmpty, vEmpty);
                             MyTempRecip.Name = sReplyUserAlias;
                             MyTempRecip.Resolve(false);
                             //Hash the ID into a string that looks like a
                             //FLATENTRYLIST structure.
                             //Calculate the length of a string converted
                             //hex representation of the

                             //Alt Recip's EntryID.
                             //Divide this value by 2 to calculate
                             //how long this will appear to
                             //MAPI when it views it as binary
                             //rather than as a string.
                              int nID = MyTempRecip.ID.ToString().Length/2;
                             //Concatenate members of the FLATENTRY structure:
                             //A) Length of entire FLATENTRY structure
                             //B) Padding
                             //C) ENTRYID of Alt Recip
                              String mID = Convert.ToString(nID, 16) +
                                "000000" + MyTempRecip.ID;

                             //Now calculate the length of the
                             //entire FLATENTRY structure as a
                             //string and again divide by 2 to
                             //determine length when viewed as binary.
                              String StructureLength =
                                Convert.ToString(mID.Length/2, 16);
                             //Assemble the components of the
                             //FLATENTRYLIST structure:
                             //A) "01000000" ' defines how many
                             //FLATENTRY structures there are in
                             //the FLATENTRYLIST (plus padding) -
                             //There is only 1 in this sample.
                             //B) Length of the first (and only)
                             //FLATENTRY array member
                             //C) More padding
                             //D) EntryID of Alt Recip (as a string)
                              String sBlock = "01000000" +
                                StructureLength + "000000" + mID;
                              MAPI.Fields MyFlds;
                              MyFlds = (MAPI.Fields)MyMsg.Fields;
                             //Write the values to the fields
                              MyFlds.Add(
                               MAPI.CdoPropTags.CdoPR_REPLY_RECIPIENT_NAMES,
                               MyTempRecip.Name, vEmpty, vEmpty);
                              MyFlds.Add(
                                MAPI.CdoPropTags.CdoPR_REPLY_RECIPIENT_ENTRIES,
                                sBlock, vEmpty, vEmpty);
                            //Remove the temporary recipient
                             MyTempRecip.Delete();
                            //Send mail
                             MyMsg.Send(vEmpty, vEmpty, vEmpty);
                            //Logoff
                             MySession.Logoff();

                           //Clean up
                             MyRecip = null;
                             MyRecips = null;
                             MyMsgs = null;
                             MyMsg = null;
                             MyFolder = null;
                             MySession = null;
                       }
                       catch (Exception e)
                       {
                             Console.WriteLine("{0} Exception caught.", e);
                       }
                }
          }
}
End example
Code Example: Sending E-mail Using System.Web
Start example
using System;
using System.Web.Mail;

namespace Test
{
          class TestClass
          {
                [STAThread]
                static void Main(string[] args)
                {
                       MailMessage Message = new MailMessage();
                       Message.From = "sender@somewhere.com";
                       Message.To = "recipient@somewhere.com";
                       Message.Subject = "Send Using Web Mail";
                       Message.BodyFormat = MailFormat.Html;
                       Message.Body =
                         "<HTML><BODY><B>Hello World!</B></BODY></HTML>";
                       String sFile = @"C:\temp\Hello.txt";
                       MailAttachment Attach =
                          new MailAttachment(sFile, MailEncoding.Base64);
                       Message.Attachments.Add(Attach);
                       SmtpMail.SmtpServer = "MySMTPServer";
                       SmtpMail.Send(Message);
                }

          }
}
End example

Team LiB
Previous Section Next Section