I have an ASP.NET application that needs to send emails. At first I used System.Net.Mail and everything was good. My application could send html messages and people received them just fine.
But then we added a new web server and that web server needed SSL to send the email using a different smtp server. So I had to use cdo messages instead of System.Net.Mail. See How can I send emails through SSL SMTP with the .NET Framework?
And now something very strange happens. The original server is sending emails in plain text, while the second server is sending emails in html. Both servers are using the exact same code (at the end of this post). The only difference is that the original server doesn't need username and password while the second one does.
Another difference is the first server is running Windows Server 2008 R2 and the second server is running Windows Server 2012. But I don't think cdo has changed between those two releases, right?
I don't know how to troubleshoot this. The problem is not in the code because the second server works fine, and it is not in the smtp server that the first server is using because it was working fine with System.Net.Mail. Any idea??
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Net.Configuration;
using System.Net.Mail;
using System.Runtime.InteropServices;
namespace Kernel
{
public class cdoMessage
{
public String Host;
public String Username;
public String Password;
public Int32 Port;
public Boolean EnableSsl;
public String From;
public MailAddressCollection To;
public Boolean IsBodyHtml;
public String Subject;
public String Body;
public MailAddressCollection Bcc;
public cdoMessage()
{
SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
Host = settings.Network.Host;
Username = settings.Network.UserName;
Password = settings.Network.Password;
Port = settings.Network.Port;
EnableSsl = settings.Network.EnableSsl;
From = settings.From;
To = new MailAddressCollection();
IsBodyHtml = false;
Bcc = new MailAddressCollection();
}
String s(String e)
{
return "http://schemas.microsoft.com/cdo/configuration/" + e;
}
public void Send()
{
CDO.Message message = new CDO.Message();
try
{
message.Configuration.Fields[s("smtpserver")].Value = Host;
message.Configuration.Fields[s("smtpserverport")].Value = Port;
message.Configuration.Fields[s("sendusing")].Value = CDO.CdoSendUsing.cdoSendUsingPort;
if (Username == null && Password == null)
{
message.Configuration.Fields[s("smtpauthenticate")].Value = CDO.CdoProtocolsAuthentication.cdoAnonymous;
}
else
{
message.Configuration.Fields[s("smtpauthenticate")].Value = CDO.CdoProtocolsAuthentication.cdoBasic;
message.Configuration.Fields[s("sendusername")].Value = Username;
message.Configuration.Fields[s("sendpassword")].Value = Password;
}
if (EnableSsl)
{
message.Configuration.Fields[s("smtpusessl")].Value = "true";
}
message.Configuration.Fields.Update();
message.From = From;
// ToString works just fine.
message.To = To.ToString();
message.BCC = Bcc.ToString();
message.Subject = Subject;
if (IsBodyHtml)
{
message.HTMLBody = Body;
}
else
{
message.TextBody = Body;
}
message.Send();
}
finally
{
Marshal.ReleaseComObject(message);
}
}
}
}