BCartolo
BCartolo

Reputation: 719

Users receive cdo messages as plain text

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);
            }
        }
    }
}

Upvotes: 2

Views: 1935

Answers (3)

drankin2112
drankin2112

Reputation: 4804

Here is a free library that has SSL\TLS support for POP3, SMTP, IMAP in explicit and implicit modes. Obviously I haven't used it but it appears fairly sophisticated for what it does. It's definately worth a try for the price.

http://visualstudiogallery.msdn.microsoft.com/28b96cd4-b755-48a0-b686-9abb7d5607a8

Upvotes: 1

drankin2112
drankin2112

Reputation: 4804

Try setting both .HTMLBody and .TextBody to the values that you want. Ideally different versions for plain text and html. Usually, if one is absent it is the html. The Html is considered the "alternative" display text in most readers, at least it was back in the day. Perhaps the different SMTP server handles missing email fields differently.

If that doesn't fix the problem, send plain text in both fields and see if both servers treat it the same.

If that doesn't work, forward the email from server B through server A and vice verse and see what happens. Even if you just do it manually from your email reader. It may very well be a setting on the second SMTP server that is sending as plain text by default.

Look at the MIME headers in the messages that are received from both servers. Look for differences so that you know what is actually different.

You should eventually be able to nail down what's happening and where.

UPDATE

Put in your credentials and give this a try. It sent mail to my gmail address over SSL. Try sending to your old server.

    public bool SendMail() {
        var client = new SmtpClient("smtp.gmail.com", 587) {
            Credentials = new NetworkCredential("your email", "password"),
            EnableSsl = true
        };

        MailMessage message = new MailMessage(
            new MailAddress("from email address"),
            new MailAddress("to email address")) {
                Body = "This is a test e-mail message sent using SSL ",
                Subject = "Test email with SSL and Credentials"
            };

        try {
            client.Send(message);
        } catch (Exception ex) {
            Console.WriteLine("Exception is:" + ex.ToString());
        }

        return false;
    }

Upvotes: 0

fan711
fan711

Reputation: 716

I recall this happening with weird reason:

If lines in the body are too long SMTP service will send the mail as plain text even we're sending it as HTML. Try breaking the mail body down to shorter lines by inserting \n line breaks. Though I can't say how long the lines may be.

Upvotes: 0

Related Questions