views:

691

answers:

1

I have a Classic ASP page that creates a CDO.Message object to send email. The code works on Window Server 2003 but not 2008. On 2008 an "Access is denied" error gets thrown. Here is a simple test page I wrote to diagnose the problem. How can I get this to work on Windows Server 2008?


dim myMail
Set myMail=CreateObject("CDO.Message")
If Err.Number <> 0 Then
Response.Write ("Error Occurred: ")
Response.Write (Err.Description)
Else
Response.Write ("CDO.Message was created")
myMail.Subject="Sending email with CDO"
myMail.From="[email protected]"
myMail.To="[email protected]"
myMail.TextBody="This is a message."
myMail.Send
set myMail=nothing
End If

A: 

I never got the CDO.Message object to work on Windows Server 2008. However, I found a workaround. I wrote an email class that works on Windows Server 2008. Hope this helps someone else.

[ComVisible(true)]
public class Email
{
    public bool SendEmail(string strTo, string strFrom , string strSubject, string strBody)
    {
        bool result = false;

        try
        {
            MailMessage message = new MailMessage();
            SmtpClient client = new SmtpClient("smtp.mycompany.com");

            List<string> to = recipientList(strTo);
            foreach (string item in to)
            {
                message.To.Add(new MailAddress(item));
            }
            message.From = new MailAddress(strFrom);
            message.Subject = strSubject;
            message.Body = strBody;

            client.Send(message);

            result = true;
        }
        catch
        {
            result = false;
            throw;
        }
        return result;
    }

    private List<string> recipientList(string strTo)
    {
        List<string> result = new List<string>();
        string[] emailAddresses = strTo.Split(new Char[]{',',';'});
        foreach (string email in emailAddresses)
        {
            result.Add(email.Trim());
        }
        return result;
    }
}

related questions