none
Send RTF formated email using C# and SMTP RRS feed

  • Question

  • How can I send RTF formated email using C# and SMTP?

    I did try with SMTPClient but it didnt worked. When I tried to set email body with below RTF formated text;

     

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fqrq2\fcharset0
    Verdana;}}
    {\colortbl ;\red0\green0\blue0;\red128\green0\blue0;\red0\green0\blue255;}
    \cf0\f0\fs20 Dear :\line\par
    \b THIS IS A TEST RTF BODY FORMAT\b0\par
    THIS IS A TEST RTF BODY FORMAT\keepn
    THIS IS A TEST RTF BODY FORMAT\line\par
    \b THIS IS A TEST RTF BODY FORMAT\par\b0
    THIS IS A TEST RTF BODY FORMAT\keepn
    THIS IS A TEST RTF BODY FORMAT\keepn
    THIS IS A TEST RTF BODY FORMAT\line\par
    \b THIS IS A TEST RTF BODY FORMAT\par\b0
    THIS IS A TEST RTF BODY FORMAT:\line\par\li360
    \bullet  THIS IS A TEST RTF BODY FORMAT\par
    \bullet  THIS IS A TEST RTF BODY FORMAT\par
    \bullet  THIS IS A TEST RTF BODY FORMAT\par
    \bullet  THIS IS A TEST RTF BODY FORMAT\line\par
    \pard\b THIS IS A TEST RTF BODY FORMAT?\par\b0
    THIS IS A TEST RTF BODY FORMAT\line\par
    \b THIS IS A TEST RTF BODY FORMAT\par\b0
    THIS IS A TEST RTF BODY FORMAT \'ae (XXXXX\'ae)
     THIS IS A TEST RTF BODY FORMAT\keepn
    THIS IS A TEST RTF BODY FORMAT.\line\par
    Sincerely,\line\par
    XXXXXXXXXX\line\line\line\par
    \pard\widctlpar\qc\ulnone\b0 THIS IS A TEST RTF BODY FORMAT\line\par
    ABC \'a9 0000 ABC ABC,\line
    LINE1 \line
    \pard\qc{{\field{\*\fldinst{HYPERLINK ""}
    }{\fldrslt{\cf3\ul LINE2}}}}\f0  \~ {{
    \field{\*\fldinst{HYPERLINK ""}}
    {\fldrslt{\cf3\ul LINE3}}}}\f0\line
    \pard\qc ABC ABC ABC\par }

     

    It appears as it is (with  RTF tags) in the email body.

    Thanks

    Thursday, March 28, 2013 2:04 PM

Answers

  • You will need to force Tnef by setting a special MAPI property:

    mailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8582000B", true);


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, March 29, 2013 1:20 PM
  • Of course, if Outlook is not installed, how can you use the Outlook Object Model?

    Is your code running in a service by any chance? Keep in mind that Office apps (Outlook included) cannot be used in a service.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, April 1, 2013 1:46 PM
  • MAPI_E_NOT_FOUND from IMsgServiceAdmin::CreateMsgService means MAPI cannot find the specified service in the mapisvc.inf file.

    This most likely happens if you have multiple instances of the mapisvc.inf file on your system: Outlook does not list its default services (MSEMS and MSPST MS), but if you then install the standalone version of MAPI, it might pick up the wrong mapisvc.inf file which does not have MSEMS service.

    Can you check if you have multiple instances of mapisvc.inf?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Tuesday, April 9, 2013 6:18 AM
  • Redemption will help in this case if the messages are sent through an Exchange server and you can install the standalone version of MAPI.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, April 5, 2013 2:00 PM

All replies

  • RTF is Microsoft specific, it is only sent if you send in the tnef format (the infamous winmail.dat attachment). In theory, the TNEF format is documented, so you can create a winmail.dat attachment.

    Is there a particular reason why you need to send in the RTF format through SMTP?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, March 28, 2013 2:42 PM
  • Why do you need to send RTF, and why use SMTPClient? The only mail programs that know how to deal with RTF are Outlook and (if it even still exists) Eudora. A more universal format to use would be HTML.
     
    If you do need to send RTF for some reason, why not use the Outlook object model and Outlook automation? WIth a VSTO addin (this is a VSTO forum) you have to have Office installed anyway, just use Outlook to send RTF if for some odd reason you really need RTF.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "KMP296" <=?utf-8?B?S01QMjk2?=> wrote in message news:a934cd4b-f234-45d8-89cb-b3528df43134...

    How can I send RTF formated email using C# and SMTP?

    I did try with SMTPClient but it didnt worked. When I tried to set email body with below RTF formated text;

     

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fqrq2\fcharset0
    Verdana;}}
    {\colortbl ;\red0\green0\blue0;\red128\green0\blue0;\red0\green0\blue255;}
    \cf0\f0\fs20 Dear :\line\par
    \b THIS IS A TEST RTF BODY FORMAT\b0\par
    THIS IS A TEST RTF BODY FORMAT\keepn
    THIS IS A TEST RTF BODY FORMAT\line\par
    \b THIS IS A TEST RTF BODY FORMAT\par\b0
    THIS IS A TEST RTF BODY FORMAT\keepn
    THIS IS A TEST RTF BODY FORMAT\keepn
    THIS IS A TEST RTF BODY FORMAT\line\par
    \b THIS IS A TEST RTF BODY FORMAT\par\b0
    THIS IS A TEST RTF BODY FORMAT:\line\par\li360
    \bullet  THIS IS A TEST RTF BODY FORMAT\par
    \bullet  THIS IS A TEST RTF BODY FORMAT\par
    \bullet  THIS IS A TEST RTF BODY FORMAT\par
    \bullet  THIS IS A TEST RTF BODY FORMAT\line\par
    \pard\b THIS IS A TEST RTF BODY FORMAT?\par\b0
    THIS IS A TEST RTF BODY FORMAT\line\par
    \b THIS IS A TEST RTF BODY FORMAT\par\b0
    THIS IS A TEST RTF BODY FORMAT \'ae (XXXXX\'ae)
     THIS IS A TEST RTF BODY FORMAT\keepn
    THIS IS A TEST RTF BODY FORMAT.\line\par
    Sincerely,\line\par
    XXXXXXXXXX\line\line\line\par
    \pard\widctlpar\qc\ulnone\b0 THIS IS A TEST RTF BODY FORMAT\line\par
    ABC \'a9 0000 ABC ABC,\line
    LINE1 \line
    \pard\qc{{\field{\*\fldinst{HYPERLINK ""}
    }{\fldrslt{\cf3\ul LINE2}}}}\f0  \~ {{
    \field{\*\fldinst{HYPERLINK ""}}
    {\fldrslt{\cf3\ul LINE3}}}}\f0\line
    \pard\qc ABC ABC ABC\par }

     

    It appears as it is (with  RTF tags) in the email body.

    Thanks


    Ken Slovak MVP - Outlook
    Thursday, March 28, 2013 2:51 PM
  • Thanks for reply.

    Sending RTF formated mail is customer requirement.

    I think, sending RTF mail as 'winmail.dat' attachment is not a good option. Is there any other way to send RTF mail like "Outlook Object Model + Redemption"?

    I tried outlook object model to set RTF body with following code,

    Microsoft.Office.Interop.Outlook.Application app = new Application();
    MailItem mailItem = (MailItem)app.CreateItem(OlItemType.olMailItem);
    mailItem.BodyFormat = OlBodyFormat.olFormatRichText;
    mailItem.RTFBody = bodyText;

    But this also didn't worked.

    Thanks


    • Edited by KMP296 Friday, March 29, 2013 10:09 AM
    Friday, March 29, 2013 9:59 AM
  • You will need to force Tnef by setting a special MAPI property:

    mailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8582000B", true);


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, March 29, 2013 1:20 PM
  • I tried to run my application using outlook object model on server where Office/Outlook client is not installed and I got below error;

    Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154

    Do I need to install outlook client on server?

    Thanks



    • Edited by KMP296 Monday, April 1, 2013 6:23 AM
    Monday, April 1, 2013 5:56 AM
  • Of course, if Outlook is not installed, how can you use the Outlook Object Model?

    Is your code running in a service by any chance? Keep in mind that Office apps (Outlook included) cannot be used in a service.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, April 1, 2013 1:46 PM
  • Thanks for the reply Dmitry.

    Now the situation is, cause of some restrictions we can't install Outlook on server where my application will execute. So how can I send/save mail without Outlook installed? Will Redemption help me in this case? How? I appriciate if you could post the code for the same.

    Thanks


    • Edited by KMP296 Friday, April 5, 2013 10:49 AM
    Friday, April 5, 2013 8:26 AM
  • Redemption will help in this case if the messages are sent through an Exchange server and you can install the standalone version of MAPI.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, April 5, 2013 2:00 PM
  • Thanks for the reply Dmitry.

    I tried this option in my code which is giving me below error;

    Retrieving the COM class factory for component with CLSID {29AB7A12-B531-450E-8F7A-EA94C2F3C05F} failed due to the following error: 80040154.

    I am getting above error in RDOSession initialization.

    I tried following ways to solve this error;

    1. Built application with X86 configuration ---- Not Worked

    2. Use of RedemptionLoader --- This gives me a message 'There is no Email program associated to perform requested action. Please install an email program or if one is already installed,create an association in Defaults Program control Panel '

    After that I downloaded 'Standalone Version of MAPI' and installed it. Now RDOSession instantiate properly but I am getting below error on rdoSession.LogonExchangeMailbox method;

    IMsgServiceAdmin::CreateMsgService: MAPI_E_NOT_FOUND

    How can I solve this?

    appriciate your help.

    Thanks


    • Edited by KMP296 Monday, April 8, 2013 12:38 PM
    Monday, April 8, 2013 11:33 AM
  • MAPI_E_NOT_FOUND from IMsgServiceAdmin::CreateMsgService means MAPI cannot find the specified service in the mapisvc.inf file.

    This most likely happens if you have multiple instances of the mapisvc.inf file on your system: Outlook does not list its default services (MSEMS and MSPST MS), but if you then install the standalone version of MAPI, it might pick up the wrong mapisvc.inf file which does not have MSEMS service.

    Can you check if you have multiple instances of mapisvc.inf?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Tuesday, April 9, 2013 6:18 AM
  • I found multiple instances of mapisvc.inf.

    To solve this error I modified mapisvc.inf file under C:\windows\sysWOW64 by referencing this link,

    http://tech.groups.yahoo.com/group/Outlook-Redemption/message/8745

    Now If I try to create and save RDOMail object, it works fine with RedemptionLoader.

    But when I register 'Redemption' and 'Redemption64' dlls using regsvr32 and try to create mail (without using RedemptionLoader) by assigning To, body content. But after saving the message/mail, I am getting blank mail.

    Also how can I set the 'From' field in RDOMail object?

    Thanks  

    Tuesday, April 9, 2013 7:57 AM
  • What is the relevant snippet of your code?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Wednesday, April 10, 2013 5:21 AM
  • Here is the code;

                 //RedemptionLoader.DllLocation64Bit = @"C:\Redemption\redemption64.dll";
                //RedemptionLoader.DllLocation32Bit = @"C:\Redemption\redemption.dll";

                //Create a Redemption object and use it
                //RDOSession rdoSession = RedemptionLoader.new_RDOSession();
               
                RDOSessionClass rdoSession = new RDOSessionClass();
               
                rdoSession.LogonExchangeMailbox("UserID", "Exchange Server");
                Object map = rdoSession.MAPIOBJECT;
                var address = rdoSession.CurrentUser;
                RDOMailClass m = (RDOMailClass)rdoSession.GetSharedDefaultFolder(address, rdoDefaultFolders.olFolderInbox).Items.Add("IPM.Note");
               
                m.Recipients.Add("User Email Address");
                m.Recipients.ResolveAll(null, null);
                m.Subject = "My Test";
                m.RTFBody = content;
                                       
                m.SaveAs(@"C:\Redemption\TestAttachment.msg",Type.Missing);

    This will create a blank message with no body. If I use RedemptionLoader, it will work perfectly.

    Wednesday, April 10, 2013 7:04 AM
  • Try to call m.Save before calling SaveAs - MAPI syncs all 3 body flavors (plain, HTML, RTF) when the message is saved.

    Why are you creating a message in a delegate Inbox folder but never save it?

    If all you need is a standalone MSG file, use RDOSession.CreateMessageFromMsgFile. It won't even need an active session, much less an Exchange mailbox.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Wednesday, April 10, 2013 8:17 PM
  • Thanks for replay Dmitry.

    I tried calling Save before SaveAs which set body content of mail properly but the 'To', 'From', 'Subject' part was blank in this case.

    Also I tried createing mail using RDOSession.CreateMessageFromMsgFile,which created mail with proper body content but still the header part 'To', 'From', 'Subject', 'CC', 'BCC' remains blank.

    Code Snippet;

    RDOSession rdoSession = new RDOSession();
                rdoSession.LogonExchangeMailbox("[UserID]", "[Exchange Server ID ]");
                Object map = rdoSession.MAPIOBJECT;

                //Create Message From MsgFile
                RDOMail m = (RDOMail)rdoSession.CreateMessageFromMsgFile (@"C:\Redemption\TempMsg.msg", Type.Missing, Type.Missing);
               
                //set 'To'
                m.Recipients.Add("abc@xyz.com");
                m.Recipients.ResolveAll(null, null);

                //Set 'From'
                var from = rdoSession.AddressBook.ResolveName("pqr@xyz.com", Type.Missing, Type.Missing);
                m.SentOnBehalfOf = from;
               
                //Set 'Subject'
                m.Subject = "My Test Mail";
               
                //Set 'RTF Body'
                m.RTFBody = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1027{\fonttbl{\f0\fnil\fcharset0 Arial;}}\viewkind4\uc1\pard\qc\ul\b\fs24 Hello\ulnone\b0\par }"; //content;
               
                //m.Save();
                m.SaveAs(@"C:\Redemption\NewMessage\MP.msg",Type.Missing);

    I also called m.Save before m.SaveAs, which couldn't set header part.

    How can I set header part? Which properties do I need to set?

    Help appriciated..!!

    Thanks.



    • Edited by KMP296 Monday, April 15, 2013 9:06 AM
    Monday, April 15, 2013 8:26 AM
  • The Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets to send or receive data over the Internet. SMTP protocol is using for sending email from C#. C# use System.Net.Mail namespace for sending email. To send e-mail you need to configure SMTP server. If you don’t have any SMTP, you can use free SMTP server. You can also use your gmail account.

    http://cybarlab.blogspot.com/2013/03/send-e-mail-using-c-sharp.html shows how to send an email using SMTP server.

    Hope it will help you

    Tuesday, April 16, 2013 5:56 AM
  • I am not sure I understand - do you mean Subject property will be saved empty even if you explicitly set it before?

    Can you send the resulting MSG file (zipped) to Dmitry at dimastr dot com?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Tuesday, April 16, 2013 5:57 AM
  • do you mean Subject property will be saved empty even if you explicitly set it before?

    --- Yes, along with Subject, the 'To', 'CC', 'From', 'Bcc' properties which I set explicitly are empty in resulting MSG file.

    I sent resulting MSG file to Dmitry@dimastr.com

    Thanks 

    Tuesday, April 16, 2013 7:02 AM