Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

How to expose a property in COM+

I have a DLL that was created under ASP.NET that I have made Visible to COM.  I am able to access the methods in this DLL but can't access a property in the DLL.  Right now when I call the SendMailMessage method, it is returning false.  I have a property, ErrorMsg, that will tell me the error returned but I get an error when I try and access the property - Object doesn't support this property or method: 'mEmail.ErrorMsg.

Here is the DLL Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Net.Mail;
using System.Net;
using System.Runtime.InteropServices;

namespace IntranetSupport_Mail
{
    public interface IMailHelper
    {
        bool SendMailMessage(string from, string to, string bcc, string cc, string subject, string body, string attachment);
    }

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class MailHelper : IMailHelper
    {
        // Network Credentials
        private string _Domain;
        public string Domain
        {
            get
            {
                return this._Domain;
            }
            set
            {
                this._Domain = value;
            }
        }

        private string _UserName;
        public string UserName
        {
            get
            {
                return this._UserName;
            }
            set
            {
                this._UserName = value;
            }
        }

        private string _Password;
        public string Password
        {
            get
            {
                return this._Password;
            }
            set
            {
                this._Password = value;
            }
        }

        private string _smtpHost;
        public string smtpHost
        {
            get
            {
                return this._smtpHost;
            }
            set
            {
                this._smtpHost = value;
            }
        }

        // ErrorMsg
        private string _ErrorMsg;
        [ComVisible(true)]
        public string ErrorMsg
        {
            get
            {
                return this._ErrorMsg;
            }
            set
            {
                this._ErrorMsg = value;
            }
        }

        // Instance Constructor
        public MailHelper()
        {
            ErrorMsg = "";
        }

        // Sends a mail message
        // Parameters:
        //      from        Sender address
        //      to          Recipient address;  multiple addresses are separated with a ;
        //      bcc         Bcc recipient address;  multiple addresses are separated wih a ;
        //      cc          CC recipient address;  multiple addresses are separated with a ;
        //      subject     Subject of mail message
        //      body        Body of mail message
        //      attachment  Filename of attachment;  must include full path;  multiple attachments are separated with a ;
        // Return:
        //      Returns true if no errors encountered sending the message.  Does not mean message was successfully
        //      received by recipient's mail server.
        //      Returns false if any errors encountered trying to send the message.
        [ComVisible(true)]
        public bool SendMailMessage(string from, string to, string bcc, string cc, string subject, string body,
                                    string attachment)
        {
            // Instantiate a new instance of MailMessage
            MailMessage mMailMessage = new MailMessage();
            // Instantiate a new instance of SmtpClient
            SmtpClient mSmtpClient = new SmtpClient();

            try
            {
                // Set the Sender address
                if (!String.IsNullOrEmpty(from))
                {
                    mMailMessage.From = new MailAddress(from);
                }
                if (String.IsNullOrEmpty(mMailMessage.From.ToString()))
                {
                    ErrorMsg = "Missing Sender Email.";
                    return false;
                }

                // Set the To recipients
                char[] separator = new char[] { ';' };
                if (!String.IsNullOrEmpty(to))
                {
                    String[] ToRecipients = to.Split(separator);
                    foreach (string recipient in ToRecipients)
                    {
                        mMailMessage.To.Add(new MailAddress(recipient));
                    }
                }
                else
                {
                    ErrorMsg = "Missing Recipient Email.";
                    return false;
                }

                // Set the Bcc recipients
                if (!String.IsNullOrEmpty(bcc))
                {
                    String[] BccRecipients = bcc.Split(separator);
                    foreach (string recipient in BccRecipients)
                    {
                        mMailMessage.Bcc.Add(new MailAddress(recipient));
                    }
                }

                // Set the CC recipients
                if (!String.IsNullOrEmpty(cc))
                {
                    String[] CcRecipients = cc.Split(separator);
                    foreach (string recipient in CcRecipients)
                    {
                        mMailMessage.CC.Add(new MailAddress(recipient));
                    }
                }

                // Set the subject
                if (!String.IsNullOrEmpty(subject))
                {
                    mMailMessage.Subject = subject;
                }
                else
                {
                    ErrorMsg = "Missing Subject.";
                    return false;
                }

                // Set the body of the message
                if (!String.IsNullOrEmpty(body))
                {
                    mMailMessage.Body = body;
                    mMailMessage.IsBodyHtml = true;
                }

                // Add Attachments
                if (!String.IsNullOrEmpty(attachment))
                {
                    String[] attachments = attachment.Split(separator);
                    foreach (string filename in attachments)
                    {
                        Attachment attach = new Attachment(filename);
                        mMailMessage.Attachments.Add(attach);
                    }
                }

                // Set the priority of the mail message to normal
                mMailMessage.Priority = MailPriority.Normal;

                // Send the mail message
                mSmtpClient.Send(mMailMessage);
            }
            catch (Exception ex)
            {
                ErrorMsg = ex.Message + "<br />" + ex.InnerException + "<br />";
                return false;
            }
            finally
            {
                mMailMessage.Dispose();
                mSmtpClient.Dispose();
            }
            return true;
        }
    }
}

Open in new window


Here is the classic ASP that I'm using to test it:
if Request.Form("Email") <> "" then
	Set mEmail = Server.CreateObject("IntranetSupport_Mail.MailHelper")
	if (mEmail.SendMailMessage("USW_21st_CVL@21st.com", Request.Form("EmailAddr"), "deborah.yarosh@21st.com", null, "Test Email", "<p>This is a test email from classic asp</p>", null)) then
		Results = "Email sent successfully."
	else
		Results = "Email not sent successfully.  " & Request.Form("EmailAddr") & "   " & mEmail.ErrorMsg
	end if
	mEmail = null
end if

Open in new window


Any help with this is greatly appreciated!
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Not sure what the problem is here. Have you tried looking at the object browser to see what is visible
http://msdn.microsoft.com/en-us/library/aa975608(v=vs.71).aspx

Michael
According to the interface definition in the lines after "public interface IMailHelper" there is only one item in the interface, namely SendMailMessage. If you want to access ErrorMsg you must add its definition in this interface section.
    public interface IMailHelper
    {
        bool SendMailMessage(string from, string to, string bcc, string cc, string subject, string body, string attachment);

     string ErrorMsg;
   }
Avatar of dyarosh
dyarosh

ASKER

BigRat - I tried adding string ErrorMsg to the interface and receive the following error: Interfaces cannot contain fields.

Michael74 - I can access the ErrorMsg property when accessing the DLL from an ASP.NET application.  It is when I try and access it using classic ASP that I can't access the ErrorMsg property.
ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh

ASKER

I wish I could have just exposed the property but I did as you suggested and created a method to return the ErrorMsg.  I now can access the property in classic ASP.  Thanks.