C# SMTP client using CDO

1 minute read

The following code utilize the CDO service of Windows OS to send an email. In my research CDO SMTP client can only support SSL/TLS connection type for using SSL. If you want to use START TLS connection type, C# SmtpClient class can support it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// <summary>
    /// Send an electronic message using the Collaboration Data Objects (CDO).
    /// </summary>
    /// <remarks>http://support.microsoft.com/kb/310212</remarks>
private void SendEmailWithCDO()
{
            try
            {
                var settings = ViewModel;
                CDO.Message message = new CDO.Message();
                CDO.IConfiguration configuration = message.Configuration;
                ADODB.Fields fields = configuration.Fields;
                // Set configuration.
                // sendusing:               cdoSendUsingPort, value 2, for sending the message using the network.
                // smtpauthenticate:     Specifies the mechanism used when authenticating to an SMTP service over the network.
                //                                  Possible values are:
                //                                  - cdoAnonymous, value 0. Do not authenticate.
                //                                  - cdoBasic, value 1. Use basic clear-text authentication. (Hint: This requires the use of "sendusername" and "sendpassword" fields)
                //                                  - cdoNTLM, value 2. The current process security context is used to authenticate with the service.
                ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
                field.Value = settings.SMTPServerAddress;
                field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
                field.Value = settings.PortNumber;
                field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
                field.Value = CDO.CdoSendUsing.cdoSendUsingPort;
                field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];
                field.Value = CDO.CdoProtocolsAuthentication.cdoBasic;
                field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"];
                field.Value = settings.Username;
                field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
                field.Value = settings.Password;
                field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
                field.Value = "true";
                fields.Update();
                message.From = settings.SenderEmailAddress;
                message.To = settings.SenderEmailAddress;
                message.Subject = "Test";
                message.TextBody = settings.Content;
                // Send message.
                message.Send();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(message);
                MessageBox.Show("Sent a message successfully");
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
                MessageBox.Show(ex.Message);
            }
        }
}