Send mails with rtf format using smtp client
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Send mails with rtf format using smtp client

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    42

    Send mails with rtf format using smtp client

    Hi,
    I am developing a tool to send mail. I need that the user can send the mail with the body text in RTF format. Therefore the user can write and edit the body text and when he want push the send button and the mail is sended correctly.

    I have developed this tool using mapiex and works fine, the reciver get the mail and can see the body in RTF format.
    Now I need do the same using SMTP Client, System::Net::Mail (http://msdn.microsoft.com/en-us/libr...mtpclient.aspx), I send the mail but I don't know send mail with body in rtf format. The recivier always get the mail with the body text in text plain format. How I can do to send mails with body in RTF formats using SMTP Client?

    I have tried to add alternatvies view, for example:

    Code:
    MailMessage *mmsgMailMsg = new MailMessage();
    mmsgMailMsg->From = maAdressFrom;
    ...
    mmsgMailMsg->Body = sMessage.Trim();
    
    
    AlternateView *altViewRTF = AlternateView::CreateAlternateViewFromString(m_sBodyRTFText.Trim(),Encoding::ASCII,MediaTypeNames::Text::RichText);
    mmsgMailMsg->AlternateViews->Add(altViewRTF);
    ...
    I have checked the mail that recivie the outlook clients and always the body text is in plain text. If I check other clients I can see the same. In other clients I can see that the body in rtf format is attached to mail (I don't want this).

    Can any help me please?

    pd: I have searched a lot of webs to solve this issue but I don't get solve my issue, for example: http://social.msdn.microsoft.com/for...1-698f67a42321
    Last edited by molistok; May 25th, 2011 at 07:32 AM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: Send mails with rtf format using smtp client

    First, to the old syntax: I myself use the new syntax exclusively and this is also common here on the forum. (I think I can read the old syntax more or less, but I'm probably even better in reading C#). MS recommends to use the old syntax only for maintaining legacy code, but as you're talking of "we" in the other thread over there in the VC++ section you may be working at a company and doing just this.

    However, the bottom line is that I'm not going to learn the old syntax unless it's absolutely necessary and you'll only get samples in the new syntax from me. It's up to you to translate them, sorry.

    But now back to the topic...

    The code snippet you posted doesn't really reveal much, but what I see there looks correct.

    I have some experience with the SmtpClient class but not with alternate views. So I started to do some experiments based on the MS sample code from http://msdn.microsoft.com/en-us/libr...nateviews.aspx. One of the first things I noticed was that the MS code apparently is faulty: It creates a mail with two atlernate views both of content type text/plain, just one of them contains the HTML code.

    Following is my modified version of the sample code. Besides fixing the bug of wrongly defaulting to text/plain as the content type for the alternate view, it contains some other modifications, mostly for convenience while testing. It also creats a third view containing RTF text.

    Code:
    static void CreateMessageWithMultipleViews( String^ server, String^ recipients, ICredentialsByHost ^credentials )
    {
    
      // Create a message and set up the recipients.
      MailMessage^ message = gcnew MailMessage( L"me@arkhamasylum.org",recipients,L"This e-mail message has multiple views.",L"This is some plain text." );
    
      // Construct the alternate body as HTML.
      String^ body = L"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
      body = String::Concat( body, L"<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">" );
      body = String::Concat( body, L"</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text" );
      body = String::Concat( body, L"</FONT></DIV></BODY></HTML>" );
    
      // Add the alternate body to the message.
      AlternateView^ alternate = AlternateView::CreateAlternateViewFromString(body, nullptr, MediaTypeNames::Text::Html);
      message->AlternateViews->Add(alternate);
    
      // Construct another alternate body as RTF - the RTF is a bit bulky so I load it from file
      AlternateView ^alternateRTF = gcnew AlternateView("Test2a.rtf", MediaTypeNames::Text::RichText);
      message->AlternateViews->Add(alternateRTF);
    
      // Send the message.
      SmtpClient^ client = gcnew SmtpClient;
      if (!server) {  // Passing nullptr for server means local test
        client->PickupDirectoryLocation = Path::GetFullPath("..\\Pickup");
        client->DeliveryMethod = SmtpDeliveryMethod::SpecifiedPickupDirectory;
        client->Host = "localhost";
      } else client->Host = server;
      client->Credentials = credentials;
      client->Send( message );
    
      // Display the values in the ContentType for the attachment.
      ContentType^ c = alternate->ContentType;
      Console::WriteLine( L"Content type" );
      Console::WriteLine( c );
      Console::WriteLine( L"Boundary {0}", c->Boundary );
      Console::WriteLine( L"CharSet {0}", c->CharSet );
      Console::WriteLine( L"MediaType {0}", c->MediaType );
      Console::WriteLine( L"Name {0}", c->Name );
      Console::WriteLine( L"Parameters: {0}", c->Parameters->Count );
      IEnumerator^ myEnum = c->Parameters->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
        DictionaryEntry^ d = safe_cast<DictionaryEntry^>(myEnum->Current);
        Console::WriteLine( L"{0} = {1}", d->Key, d->Value );
      }
      Console::WriteLine();
    
      // Display the values in the ContentType for the OTHER attachment.
      c = alternateRTF->ContentType;
      Console::WriteLine( L"Content type" );
      Console::WriteLine( c );
      Console::WriteLine( L"Boundary {0}", c->Boundary );
      Console::WriteLine( L"CharSet {0}", c->CharSet );
      Console::WriteLine( L"MediaType {0}", c->MediaType );
      Console::WriteLine( L"Name {0}", c->Name );
      Console::WriteLine( L"Parameters: {0}", c->Parameters->Count );
      myEnum = c->Parameters->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
        DictionaryEntry^ d = safe_cast<DictionaryEntry^>(myEnum->Current);
        Console::WriteLine( L"{0} = {1}", d->Key, d->Value );
      }
      Console::WriteLine();
    
      delete client;
      delete alternate;
      delete alternateRTF;
    }
    Using the local test feature I added to the code it creates a mail with three views which IMO is correct. However, when I have the SmtpClient send the mail back to myself via my mail provider to check it in Outlook, I just get the HTML view (and no attachment).

    I somehow suspect the mail provider to filter out the RTF content for some obscure reason. If it's not the provider it's probably Outlook, and since it is an MS program IMO it has even less resons to do that. At any rate, while the size relation of the raw mail files created in the local test is 6 KB for the one with the three views (including RTF) vs. 1 KB for the one with two views, Outlook shows a size of 5 KB for both these versions of the mail.

    I think it's worth to notice that, while having used Office for many years now, I happened to start using Outlook just a few weeks ago, so I very well may not have found everything important in that program yet. In particular I didn't find a way to view the raw mail text in Outlook, just the header.

    Note that for the local test feature of the code above to work, the pickup directory that I hard-coded needs to exist (and maybe the hard-coded path adjusted). The path I used is handy when the code is run from the VC++ IDE or a command line window with the CWD being the debug or release output directory: It uses a common pickup directory for both the debug and release build which is located in the solution directory.

    BTW, the two links in your post are broken. They contain an actual ellipsis which apparently happened when copy-pasting from your post in the VC++ section. However, I've already clicked the link to that MS forum thread in the post over there, and yes, it's not really a big help.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Feb 2007
    Posts
    42

    Re: Send mails with rtf format using smtp client

    Firstly, thank you very much for your reply. Secondly yes, I copied first topic and pasted it to this topic and therefore my links were broken, now I have edited this topic and links works fine.

    I have tried to translate my code:

    Code:
    MailMessage ^mmsgMailMsg = gcnew MailMessage;
    // maAdressFrom is a MailAddress object and the value is my mail addres
    mmsgMailMsg->From = maAdressFrom;
    ...
    // sMessage is a String, and the value is the body message in plain text, for example the value is Hello
    mmsgMailMsg->Body = sMessage; 
    
    // I add the alternateView with the body content (m_sBodyRTFText) in RTF format, m_sBodyRTFText have this value: 
    
    /* {\rtf1\ansi\ansicpg1252\deff0\deflang1027{\fonttbl{\f0\fnil\fcharset0 Arial;}}
    \viewkind4\uc1\pard\qc\ul\b\fs24 Hello\ulnone\b0\par } */
    // The translation of this rtf code is the word Hello, aligned in center, bold and with underline.
    
    AlternateView ^altViewRTF = AlternateView::CreateAlternateViewFromString(m_sBodyRTFText,Encoding::ASCII,MediaTypeNames::Text::RichText);
    mmsgMailMsg->AlternateViews->Add(altViewRTF);
    ...
    If I add another alternate view with a body in html and with the type in html, when I recivied the mail I can read the mail in html. Therefore I think that this confirm that I add the alternate view fine.

    I suspect that outlook don't accept the text in rtf format (is possible that I need add some other chars in the body in rtf format??) and therefore discard this alternate view. Any people know if is possible send mails in rtf formats with smtp client library? Can any help me?

    Thanks.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: Send mails with rtf format using smtp client

    Hmmm... Your code looks ok.

    Quote Originally Posted by molistok View Post
    If I add another alternate view with a body in html and with the type in html, when I recivied the mail I can read the mail in html. Therefore I think that this confirm that I add the alternate view fine.
    You mean you tested with a mail containing exactly one text/plain and one text/richtext view and that didn't work either? In the meantime I got suspicious that it might simply be a matter of more than two available views not working but didn't test that yet. If you already did I can spare the effort.

    I suspect that outlook don't accept the text in rtf format [...] and therefore discard this alternate view.
    That looks like a likely cause for me too, though I'd find that really weird as RTF is an MS format, so why should even an MS program fail/refuse to handle it? OTOH I find it rather unlikely that we both use the same mail provider...

    I even tried to send myself an RTF mail created in outlook itself, set up to use Word as the RTF editor. Guess what? It arrived as HTML...

    (is possible that I need add some other chars in the body in rtf format??)
    I created my RTF with Word (which is why it's 3.5 KB for a single line with a few colored words), so I expect it to be "truly authoritative MS RTF". If that doesn't work, then what would?

    Any people know if is possible send mails in rtf formats with smtp client library? Can any help me?
    Though I don't have a practical need for that at the moment I'm really curious about that too.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Feb 2007
    Posts
    42

    Re: Send mails with rtf format using smtp client

    Is possible that is not working fine because outlook want recieve a rtf format uncompressed (or compressed), and I think that we are sending mails with RTF format compressed(or uncompressed)... I don't understand a lot, but I am thinking this at this time (I have read this in http://stackoverflow.com/questions/2...mat-to-outlook).

    Thank you very much Eri523.
    Last edited by molistok; May 25th, 2011 at 11:31 AM. Reason: spelling mistakes

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: Send mails with rtf format using smtp client

    Quote Originally Posted by molistok View Post
    Is possible that is not working fine because outlook want recieve a rtf format uncompressed (or compressed), and I think that we are sending mails with RTF format compressed(or uncompressed)...
    The mail file generated by the local test shows that SmtpClient encodes the RTF as base64. Not exactly a compression, rather some sort of bloat. I didn't decode the bas64 stream again but I think it contains the unmodified original RTF data.

    Using the registry tweak described in http://www.outlook-tips.net/2011/159...e-outlook-2003 (a link that I found in the stackoverflow thread you linked to) I could verify that the RTF view actually reaches my Outlook. (So there's no blame to put on the mail provider. ) Unfortunately Outlook just keeps ignoring it...

    I certainly will turn this option back off once this thread is over because it pretty much bloats the mails as Outlook stores them. The place Outlook accounted for the same three-view mail climbed from 5 KB to 16 KB after activating it. Keep in mind that Outlook will apply this bloat to all mails while the option is activated, including multi-MB ones. Looks like Outlook converts the mails into some internal format upon reception and then stores that, discarding the original raw mail. With that registry option turned on it will just store the raw mail (instead of just the header) in addition to what it already stores anyway, thereby duplicating the information.

    I don't understand a lot, but I am thinking this at this time (I have read this in http://stackoverflow.com/questions/2...mat-to-outlook).
    Now that thread is much more helpful, yet unfortunately not really promising. How much effort are you willing to take?

    The fact that I wasn't able to send myself an RTF mail with Outlook makes me wonder whether it's even possible at all to properly transport mails with TNEF content over non-Excange SMTP servers. However, as I understand dave wanta's post on stackoverflow and the Wikipedia article on TNEF, it should be possible. Chances are I made a mistake myself during that attempt...

    BTW, I immideately bookmarked that Outlook Tips site. Looks like it may turn out to be really helpful, especially since I'm new to Outlook.

    Thank you very much Eri523.
    Youre welcome.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Feb 2007
    Posts
    42

    Re: Send mails with rtf format using smtp client

    Hi,
    After to fight some hours more with rtf formats of outlook, we have changed our send mail tool and we will send a alternative view in html format because I think that is more easy and I think that will skip some other problems... Thank you very much!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center