<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Wiz/dumb : CDOSYS</title><link>http://blogs.msdn.com/pcreehan/archive/tags/CDOSYS/default.aspx</link><description>Tags: CDOSYS</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Fixing the Charset on MIME Messages When Using ADODB.Stream.ReadText</title><link>http://blogs.msdn.com/pcreehan/archive/2009/10/29/fixing-the-charset-on-mime-messages-when-using-adodb-stream-readtext.aspx</link><pubDate>Thu, 29 Oct 2009 20:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9914370</guid><dc:creator>Patrick Creehan</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/pcreehan/comments/9914370.aspx</comments><wfw:commentRss>http://blogs.msdn.com/pcreehan/commentrss.aspx?PostID=9914370</wfw:commentRss><description>&lt;p&gt;We had an issue recently where the customer had written an application to process MIME messages that had body parts encoded with the &lt;a href="http://www.ietf.org/rfc/rfc1489.txt?number=1489"&gt;koi8-r charset&lt;/a&gt;…or at least that’s what it said:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;------_=_NextPart_001_01C9F067.D9742103
Content-Type: text/plain;
    charset=&amp;quot;koi8-r&amp;quot;
Content-Transfer-Encoding: quoted-printable&lt;/pre&gt;

&lt;p&gt;If you use code similar to the following, the resulting characters returned were not correct.&lt;/p&gt;

&lt;div style="padding-bottom: 5px; padding-left: 5px; width: 519px; padding-right: 5px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 5px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e0cbcb2d-a678-4b8e-a139-0da1ba0ab375" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;
&lt;p&gt;

&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; ReadBodyPart()&lt;br&gt;
{&lt;br&gt;
    CDO.&lt;span style="color:#2b91af"&gt;IDataSource&lt;/span&gt; oIDsrc;&lt;br&gt;
    ADODB.&lt;span style="color:#2b91af"&gt;Stream&lt;/span&gt; objBpStream = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; ADODB.&lt;span style="color:#2b91af"&gt;Stream&lt;/span&gt;();&lt;br&gt;
    CDO.&lt;span style="color:#2b91af"&gt;Message&lt;/span&gt; objCDOMsg = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; CDO.&lt;span style="color:#2b91af"&gt;Message&lt;/span&gt;();&lt;br&gt;
    CDO.&lt;span style="color:#2b91af"&gt;IBodyPart&lt;/span&gt; oIBodyPart;&lt;br&gt;
    &lt;br&gt;
    objBpStream.Open(vtMissing, &lt;br&gt;
        ADODB.&lt;span style="color:#2b91af"&gt;ConnectModeEnum&lt;/span&gt;.adModeUnknown,&lt;br&gt;
        ADODB.&lt;span style="color:#2b91af"&gt;StreamOpenOptionsEnum&lt;/span&gt;.adOpenStreamUnspecified, &lt;br&gt;
        &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty, &lt;br&gt;
        &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty);&lt;br&gt;
&lt;br&gt;
    objBpStream.LoadFromFile(EML_FILE_PATH);           &lt;br&gt;
    &lt;br&gt;
    oIDsrc = (CDO.&lt;span style="color:#2b91af"&gt;IDataSource&lt;/span&gt;)objCDOMsg;&lt;br&gt;
    &lt;br&gt;
    oIDsrc.OpenObject(objBpStream, &lt;span style="color:#a31515"&gt;"_Stream"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
    objBpStream.Close();&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
    oIBodyPart = (CDO.&lt;span style="color:#2b91af"&gt;IBodyPart&lt;/span&gt;)objCDOMsg.TextBodyPart;&lt;br&gt;
    objBpStream = (ADODB.&lt;span style="color:#2b91af"&gt;Stream&lt;/span&gt;)oIBodyPart.GetDecodedContentStream();&lt;br&gt;
    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; objBpStream.ReadText(-1);&lt;br&gt;
&lt;br&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The problem is that koi8-r is the &lt;a href="http://msdn.microsoft.com/en-us/library/system.text.encoding.bodyname(VS.85).aspx"&gt;body name&lt;/a&gt; for both “windows-1251” (&lt;a href="http://msdn.microsoft.com/en-us/goglobal/cc305144.aspx"&gt;codepage 1251&lt;/a&gt;) and “koi8-r” (&lt;a href="http://www.ietf.org/rfc/rfc1489.txt?number=1489"&gt;codepage 20866&lt;/a&gt;). I took a look at what the &lt;a href="http://msdn.microsoft.com/en-us/library/ms526935(EXCHG.10).aspx"&gt;TextBody&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms527537(EXCHG.10).aspx"&gt;HtmlBody&lt;/a&gt; properties were doing because they can return the characters correctly. Turns out, when the message is loaded, we make an API call to &lt;a href="http://msdn.microsoft.com/en-us/library/aa741015(VS.85).aspx"&gt;GetCharsetInfo&lt;/a&gt; and pass in the value defined in the header. This gives us back a structure that contains the &lt;a href="http://msdn.microsoft.com/en-us/library/aa767878(VS.85).aspx"&gt;uiCodePage&lt;/a&gt; used by &lt;a href="http://msdn.microsoft.com/en-us/library/ms526935(EXCHG.10).aspx"&gt;TextBody&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms527537(EXCHG.10).aspx"&gt;HtmlBody&lt;/a&gt; later.&lt;/p&gt;

&lt;p&gt;Fortunately, the .net framework provides methods to do this as well. I simply leverage them to correct the charset I’m given.&lt;/p&gt;

&lt;div style="padding-bottom: 5px; padding-left: 5px; width: 519px; padding-right: 5px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 5px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c2b19660-5aa6-4101-9a3a-ba8cc4b6ae56" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;
&lt;p&gt;

&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; GetCharSetName(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; bodyName)&lt;br&gt;
{&lt;br&gt;
    &lt;span style="color:#2b91af"&gt;Encoding&lt;/span&gt; encoding = &lt;span style="color:#2b91af"&gt;Encoding&lt;/span&gt;.GetEncoding(bodyName);&lt;br&gt;
    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (encoding != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;br&gt;
    {&lt;br&gt;
        &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; iCP = encoding.WindowsCodePage;&lt;br&gt;
        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Encoding&lt;/span&gt;.GetEncoding(iCP).WebName;&lt;br&gt;
    }&lt;br&gt;
    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; bodyName;&lt;br&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Then I just insert a call to that function above my call to ReadText and we’re good.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;
  &lt;div style="padding-bottom: 5px; padding-left: 5px; width: 519px; padding-right: 5px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 5px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f7dac8f9-8241-483c-a229-a0021993acaa" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;
&lt;p&gt;

&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; ReadBodyPart()&lt;br&gt;
{&lt;br&gt;
    CDO.&lt;span style="color:#2b91af"&gt;IDataSource&lt;/span&gt; oIDsrc;&lt;br&gt;
    ADODB.&lt;span style="color:#2b91af"&gt;Stream&lt;/span&gt; objBpStream = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; ADODB.&lt;span style="color:#2b91af"&gt;Stream&lt;/span&gt;();&lt;br&gt;
    CDO.&lt;span style="color:#2b91af"&gt;Message&lt;/span&gt; objCDOMsg = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; CDO.&lt;span style="color:#2b91af"&gt;Message&lt;/span&gt;();&lt;br&gt;
    CDO.&lt;span style="color:#2b91af"&gt;IBodyPart&lt;/span&gt; oIBodyPart;&lt;br&gt;
    &lt;br&gt;
    objBpStream.Open(vtMissing, &lt;br&gt;
        ADODB.&lt;span style="color:#2b91af"&gt;ConnectModeEnum&lt;/span&gt;.adModeUnknown,&lt;br&gt;
        ADODB.&lt;span style="color:#2b91af"&gt;StreamOpenOptionsEnum&lt;/span&gt;.adOpenStreamUnspecified, &lt;br&gt;
        &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty, &lt;br&gt;
        &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty);&lt;br&gt;
&lt;br&gt;
    objBpStream.LoadFromFile(EML_FILE_PATH);           &lt;br&gt;
    &lt;br&gt;
    oIDsrc = (CDO.&lt;span style="color:#2b91af"&gt;IDataSource&lt;/span&gt;)objCDOMsg;&lt;br&gt;
    &lt;br&gt;
    oIDsrc.OpenObject(objBpStream, &lt;span style="color:#a31515"&gt;"_Stream"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
    objBpStream.Close();&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
    oIBodyPart = (CDO.&lt;span style="color:#2b91af"&gt;IBodyPart&lt;/span&gt;)objCDOMsg.TextBodyPart;&lt;br&gt;
    objBpStream = (ADODB.&lt;span style="color:#2b91af"&gt;Stream&lt;/span&gt;)oIBodyPart.GetDecodedContentStream();&lt;br&gt;
    objBpStream.Charset = GetCharSetName(objBpStream.Charset);&lt;br&gt;
    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; objBpStream.ReadText(-1);&lt;br&gt;
&lt;br&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9914370" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/pcreehan/archive/tags/MIME/default.aspx">MIME</category><category domain="http://blogs.msdn.com/pcreehan/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/pcreehan/archive/tags/DevMsgTeam/default.aspx">DevMsgTeam</category><category domain="http://blogs.msdn.com/pcreehan/archive/tags/CDOSYS/default.aspx">CDOSYS</category><category domain="http://blogs.msdn.com/pcreehan/archive/tags/CDOSYS_2F00_CDOEX/default.aspx">CDOSYS/CDOEX</category></item><item><title>Why Pickups are the Best. More Robust, More Reliable, etc.</title><link>http://blogs.msdn.com/pcreehan/archive/2008/08/08/why-pickups-are-the-best-more-robust-more-reliable-etc.aspx</link><pubDate>Fri, 08 Aug 2008 15:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8843455</guid><dc:creator>Patrick Creehan</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/pcreehan/comments/8843455.aspx</comments><wfw:commentRss>http://blogs.msdn.com/pcreehan/commentrss.aspx?PostID=8843455</wfw:commentRss><description>&lt;H4&gt;&lt;/H4&gt;
&lt;H4&gt;&lt;/H4&gt;
&lt;P mce_keep="true"&gt;We get calls about this all the time. You have a CDOSYS application that…what? No, I’m not talking about automobiles, why did you…oh, the title. Well, it’s true. Read on.&lt;/P&gt;
&lt;H4&gt;The Dreaded Transport Error&lt;/H4&gt;
&lt;P&gt;The problem we see more than any other is when a customer is trying to send a lot of e-mail using an SMTP API such as CDOEX, CDOSYS, or System.Web.Mail, they will start to get &lt;A href="http://msdn.microsoft.com/en-us/library/ms526287(EXCHG.10).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms526287(EXCHG.10).aspx"&gt;CDO_E_FAILED_TO_CONNECT&lt;/A&gt; errors (0x80040213) (-2147220973) which is “The transport could not connect to the server.”&lt;/P&gt;
&lt;H4&gt;The Resolution&lt;/H4&gt;
&lt;P&gt;We see it over and over again. This error is because you are sending such a high volume of email over one port, typically the default port&amp;nbsp;(25), that you flood the port with traffic until your app can no longer talk to the server. If your application is sending such a large volume of mail so quickly that it generates this error, it’s time to install a local SMTP service (if one is not already installed) and start sending via port.&lt;/P&gt;
&lt;P&gt;&lt;A title="Setting the Send and Post Method" href="http://msdn.microsoft.com/en-us/library/ms527014(EXCHG.10).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms527014(EXCHG.10).aspx"&gt;Setting the Send and Post Method&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A title="Sending or Posting using the Pickup Directory" href="http://msdn.microsoft.com/en-us/library/ms526208(EXCHG.10).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms526208(EXCHG.10).aspx"&gt;Sending or Posting using the Pickup Directory&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A title="How To Send HTML Formatted Mail Using CDO for Windows 2000 and the" href="http://support.microsoft.com/kb/286430" mce_href="http://support.microsoft.com/kb/286430"&gt;How To Send HTML Formatted Mail Using CDO for Windows 2000 and the Local Pickup Directory&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;You can employ the same strategy in the article above to your System.Web.Mail emails:&lt;/P&gt;
&lt;P&gt;&lt;A title="How to send e-mail messages by using CDOSYS and the local SMTP directory with VB.NET" href="http://support.microsoft.com/kb/313773" mce_href="http://support.microsoft.com/kb/313773"&gt;How to send e-mail messages by using CDOSYS and the local SMTP pickup directory with VB.NET&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A title=http://support.microsoft.com/kb/310212/en-us href="http://support.microsoft.com/kb/310212/en-us" mce_href="http://support.microsoft.com/kb/310212/en-us"&gt;How to use the Cdosys.dll library to send an e-mail message with attachments by using Visual C#&lt;/A&gt; (this also shows how to send using pickup)&lt;/P&gt;
&lt;H4&gt;Benefits of using the pickup folder&lt;/H4&gt;
&lt;H5&gt;Performance &lt;/H5&gt;
&lt;P&gt;It is very easy to generate a text file on the file system. When your CDOSYS configuration is set to send via pickup, all CDOSYS is doing in your application is creating an .eml file in a specific directory. You’ve just removed the entire networking stack from your application. You can whip through email generation in a fraction of the time it takes to communicate over a TCP port.&lt;/P&gt;
&lt;H5&gt;Reliability&lt;/H5&gt;
&lt;P&gt;When you let the SMTP service do the sending of your emails, you are working on a far more stable architecture than for you to handle error conditions yourself in your application. IIS’s SMTP service makes it easy when there is a problem as well by just moving your .eml file to the BadMail folder. If the service runs into a problem where transport is too busy to take new connections, it just tries again later. Email is asynchronous by nature, so this shouldn’t be a big deal. &lt;/P&gt;
&lt;H5&gt;Resiliency&lt;/H5&gt;
&lt;P mce_keep="true"&gt;The SMTP service will just continually monitor the Pickup folder for any files in it and will try to send them. If there’s a power failure for example, or your machine crashes, when it reboots, the files are still there so the service will once again work on sending them. &lt;/P&gt;
&lt;H4&gt;When You Shouldn’t Use Pickup&lt;/H4&gt;
&lt;P&gt;If it is impossible for you to install a local SMTP service for whatever reason, sending via port may be your only option. For example, if you are writing an application to run on a desktop running Windows XP/Vista, it doesn’t make sense to install an SMTP service on a desktop platform. Additionally, if your application only sends an email occasionally, it will probably be fine to send using port instead of pickup – you’ll probably never run into this error. If you application does need to know immediately whether or not the email is going to succeed in each command in the protocol (MAIL FROM, RCPTTO, etc) then you may need to send via port as well. In those cases, if you will be sending a large volume of email very quickly, you should throttle your application to allow some time between each mail so that you don’t clog the pipes and end up with the dreaded transport error. This is, of course, assuming that you can’t install SMTP services locally.&lt;/P&gt;
&lt;P&gt;Another reason you may not code your application to use a pickup folder is that you don’t know the environment your application will be deployed on. For example, you’ve written a web application that sends email notifications when some data has changed and you’re going to sell this application for companies to deploy in their own environments. You don’t know, nor can you count on an SMTP service running locally on the box. Your first instinct may be to code your application to send via port. Certainly, they’ll have an SMTP service &lt;EM&gt;somewhere&lt;/EM&gt; in the organization and it’s easy for them to just type in a setting somewhere “mail.company.com” – folks are used to it by now. My plea to you is to account for both possibilities. Provide a mechanism for sending via pickup as well – you won’t regret it. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8843455" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/pcreehan/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/pcreehan/archive/tags/DevMsgTeam/default.aspx">DevMsgTeam</category><category domain="http://blogs.msdn.com/pcreehan/archive/tags/CDOSYS/default.aspx">CDOSYS</category><category domain="http://blogs.msdn.com/pcreehan/archive/tags/CDOSYS_2F00_CDOEX/default.aspx">CDOSYS/CDOEX</category></item></channel></rss>