1

I wonder if it's possible with the .net framework or Microsoft.Office.Interop.Outlook to load an email message (*.msg), do a search and replace and send it from C#.

It's all happening on the server so Outlook cannot be installed.

What I've tried

  • the Redemption library but somehow it loses the images inlined in the template and can't figure out to remedy this

  • Using Microsoft.Office.Interop.Outlook

        Application objOutlook = new Application();
        objOutlook.CreateItemFromTemplate("c:\temp\..",)
    

But it expects as it second parameter an outlook folder, I can't give it a file path where it will save to

I'm thinking to switch to regular txt files instead of C# but maybe someone did this already

Update 1

This is the redemption code I tried. The problem is that the formatting and image (of a signature is not preserved)

using Interop.Redemption;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Replace(@"mailnonunicode.msg");
            Replace(@"mailunicode.msg");
            Replace(@"mailtemplate.oft");
        }

        static void Replace(string cTestharnessKmailMsg)
        {
            RDOSession rdoSession = new RDOSession();
            RDOMail messageFromMsgFile = rdoSession.GetMessageFromMsgFile(cTestharnessKmailMsg);

            messageFromMsgFile.Body = messageFromMsgFile.Body.Replace("abc",
                                                                      "xyz");
            messageFromMsgFile.Save();
        }
    }
}

Update 2 / Solution

If you want to preserve the formatting, you need to work with HTMLBody or RTFBody properties, not with the plain text Body.

buckley
  • 13,690
  • 3
  • 53
  • 61

1 Answers1

1

What is your existing Redemption code?

If the message needs to be sent, it must be created in one of the Outlook folders - a standalone MSG file cannot be sent.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I can't have outlook installed on the server so no outlook folders. I think that rules out redemption thus. :( – buckley Mar 08 '13 at 18:13
  • The RDO family of objects (http://www.dimastr.com/redemption/rdo_introduction.htm) does not need Outlook, only the MAPi system, which means you can have the standalone version of MAPI installed (http://www.microsoft.com/downloads/details.aspx?FamilyID=e17e7f31-079a-43a9-bff2-0a110307611e&DisplayLang=en). You can then call RDOSession.LogonExchangeMailbox to dynamically connect to a mailbox, retrieve the folder where the message will be created (e.g. RDOSession.GetDefaultFolder(olFolderDrafts), create the message (RDOFolder.Items.Add) import the MSG file (RDOMail.Import), then send it. – Dmitry Streblechenko Mar 08 '13 at 23:22
  • I've added my code to the question. Isn't this the way to make a replacement? I can send you the complete solution if that's helpful. – buckley Mar 12 '13 at 12:36
  • If you want to preserve the formatting, you need to work with HTMLBody or RTFBody properties, not with the plain text Body. – Dmitry Streblechenko Mar 12 '13 at 16:30
  • Thanks Dmitry, that was the solution – buckley Mar 13 '13 at 10:00