Solved

How to expose a property in COM+

Posted on 2013-01-22
5
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 38808746
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
ID: 38809370
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
ID: 38809738
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
ID: 38810492
>>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
ID: 38814107
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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