Menu

Let's Us Share What We Know

  • HOME
  • BLOG
  • Calling a COM component directly from C#

Calling a COM component directly from C#

15,April,2011

This post title could be using SMTPsvg.Mailer component in c# code too:-). Recently, we had to call a COM component from C# code. The COM component in question was SMTPsvg.Mailer. Sample code in classic ASP was available. The easiest way to do this method is using Interop. But we were not in a situation to generate interop libraries. We also learned this could have been easier had we used vb.net instead of c#. On further investigation we found two links that helped us to solve the issue:

  Sample code showing usage of SMTPsvg.Mailer in c# (you might need to change values in BOLD in the code below). object objMailer = null; System.Type m_MailerType = System.Type.GetTypeFromProgID("SMTPsvg.Mailer"); objMailer=System.Activator.CreateInstance(m_MailerType); m_MailerType.InvokeMember("FromName", System.Reflection.BindingFlags.SetProperty, null, objMailer, new object[] {"FROM_NAME"}); m_MailerType.InvokeMember("FromAddress", System.Reflection.BindingFlags.SetProperty, null, objMailer, new object[] {"FROM_EMAIL"}); m_MailerType.InvokeMember("RemoteHost", System.Reflection.BindingFlags.SetProperty, null, objMailer, new object[] {"REMOTE_HOST_NAME"}); m_MailerType.InvokeMember("Subject", System.Reflection.BindingFlags.SetProperty, null, objMailer, new object[] {“YOUR_SUBJECT”}); m_MailerType.InvokeMember("BodyText", System.Reflection.BindingFlags.SetProperty, null, objMailer, new object[] {“MAIL_BODY”}); m_MailerType.InvokeMember("ContentType", System.Reflection.BindingFlags.SetProperty, null, objMailer, new object[] {" Text/HTML "});//Content type, set to Text/HTML for HTML mail need not set this for text mails m_MailerType.InvokeMember("AddRecipient", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, objMailer, new object[] {“RECEPIENT_NAME”,”RECEPIENT_ADDRESS”); // name value pairs bool bReturn = (bool)m_MailerType.InvokeMember("SendMail", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, objMailer, null); Be aware that the above method is likely to be slow, but still might be useful in some cases.

Leave a Reply

Your email address will not be published. Required fields are marked *