Solved

How to expose a property in COM+

Posted on 2013-01-22
5
264 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:Michael74
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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