Solved

How to expose a property in COM+

Posted on 2013-01-22
5
259 Views
Last Modified: 2013-01-24
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!
0
Comment
Question by:dyarosh
  • 2
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Michael74
Comment Utility
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
0
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
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;
   }
0
 

Author Comment

by:dyarosh
Comment Utility
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.
0
 
LVL 27

Accepted Solution

by:
BigRat earned 500 total points
Comment Utility
>>Interfaces cannot contain fields.

Beats me why not. I don't use C# but write in C++. The field must be turned into a property function. Try adding a function "string getErrorMsg()" to the interface and add a public function getErrorMsg() to the class.
0
 

Author Closing Comment

by:dyarosh
Comment Utility
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now