Monday, 19 August 2013

Email Service With Differently Encoded Attachments

Email Service With Differently Encoded Attachments

I'm trying to make an emailer service that sends out attachments that has
been processed elsewhere in our system. The requirements for this is that
the client is expecting a specific encoding specified in the settings for
a job. In a test app, I'm trying to send out emails with attachments in
different encodings, but when I view in IE or notepad++ I get a bunch of
question marks, instead of a correctly encoded file. What am I not doing
right?
var current_month = @"<?xml version=""1.0"" encoding=""utf-8""?>
<test>
ABCDEFGHIJKLMNOPQRSTUVWXYZ /0123456789
abcdefghijklmnopqrstuvwxyz £©µÀÆÖÞßéöÿ
–—'""„†•…‰™œŠŸž€
&#913;&#914;&#915;&#916;&#937;&#945;&#946;&#947;&#948;&#969;
&#1040;&#1041;&#1042;&#1043;&#1044;&#1072;&#1073;&#1074;&#1075;&#1076;
&#8704;&#8706;&#8712;&#8477;&#8743;&#8746;&#8801;&#8734;
&#8593;&#8599;&#8616;&#8635;&#8675;
&#9488;&#9532;&#9556;&#9560;&#9617;&#9658;&#9786;&#9792;
&#64257;&#65533;&#9280;&#8322;&#7968;&#7682;&#1253;&#7812;&#592;&#720;&#9038;&#1488;&#1329;&#4304;
&#927;&#8016;&#967;&#8054;
&#964;&#945;&#8016;&#964;&#8048;
&#960;&#945;&#961;&#8055;&#963;&#964;&#945;&#964;&#945;&#8055;
&#956;&#959;&#953;
&#947;&#953;&#947;&#957;&#8061;&#963;&#954;&#949;&#953;&#957;,
&#8038;
&#7940;&#957;&#948;&#961;&#949;&#962;
&#8127;&#913;&#952;&#951;&#957;&#945;&#8150;&#959;&#953;,
&#8005;&#964;&#945;&#957; &#964;&#8127;
&#949;&#7984;&#962; &#964;&#8048;
&#960;&#961;&#8049;&#947;&#956;&#945;&#964;&#945;
&#7936;&#960;&#959;&#946;&#955;&#8051;&#968;&#969;
&#954;&#945;&#8054;
&#8005;&#964;&#945;&#957;
&#960;&#961;&#8056;&#962;
&#964;&#959;&#8058;&#962;
&#955;&#8057;&#947;&#959;&#965;&#962;
&#959;&#8019;&#962;
&#7936;&#954;&#959;&#8059;&#969;&#903;
&#964;&#959;&#8058;&#962;
&#956;&#8050;&#957; &#947;&#8048;&#961;
&#955;&#8057;&#947;&#959;&#965;&#962;
&#960;&#949;&#961;&#8054;
&#964;&#959;&#8166;
&#964;&#953;&#956;&#969;&#961;&#8053;&#963;&#945;&#963;&#952;&#945;&#953;
&#934;&#8055;&#955;&#953;&#960;&#960;&#959;&#957;
&#8001;&#961;&#8182;
&#947;&#953;&#947;&#957;&#959;&#956;&#8051;&#957;&#959;&#965;&#962;,
&#964;&#8048; &#948;&#8050;
&#960;&#961;&#8049;&#947;&#956;&#945;&#964;&#8127;
&#949;&#7984;&#962;
&#964;&#959;&#8166;&#964;&#959;
&#960;&#961;&#959;&#8053;&#954;&#959;&#957;&#964;&#945;,
&#8037;&#963;&#952;&#8127;
&#8005;&#960;&#969;&#962; &#956;&#8052;
&#960;&#949;&#953;&#963;&#8057;&#956;&#949;&#952;&#8127;
&#945;&#8016;&#964;&#959;&#8054;
&#960;&#961;&#8057;&#964;&#949;&#961;&#959;&#957;
&#954;&#945;&#954;&#8182;&#962;
&#963;&#954;&#8051;&#968;&#945;&#963;&#952;&#945;&#953;
&#948;&#8051;&#959;&#957;.
&#959;&#8016;&#948;&#8051;&#957;
&#959;&#8022;&#957;
&#7940;&#955;&#955;&#959;
&#956;&#959;&#953;
&#948;&#959;&#954;&#959;&#8166;&#963;&#953;&#957;
&#959;&#7985; &#964;&#8048;
&#964;&#959;&#953;&#945;&#8166;&#964;&#945;
&#955;&#8051;&#947;&#959;&#957;&#964;&#949;&#962;
&#7970; &#964;&#8052;&#957;
&#8017;&#960;&#8057;&#952;&#949;&#963;&#953;&#957;,
&#960;&#949;&#961;&#8054; &#7975;&#962;
&#946;&#959;&#965;&#955;&#949;&#8059;&#949;&#963;&#952;&#945;&#953;,
&#959;&#8016;&#967;&#8054;
&#964;&#8052;&#957;
&#959;&#8022;&#963;&#945;&#957;
&#960;&#945;&#961;&#953;&#963;&#964;&#8049;&#957;&#964;&#949;&#962;
&#8017;&#956;&#8150;&#957;
&#7937;&#956;&#945;&#961;&#964;&#8049;&#957;&#949;&#953;&#957;.
&#7952;&#947;&#8060; &#948;&#8051;,
&#8005;&#964;&#953; &#956;&#8051;&#957;
&#960;&#959;&#964;&#8127;
&#7952;&#958;&#8134;&#957; &#964;&#8135;
&#960;&#8057;&#955;&#949;&#953;
&#954;&#945;&#8054; &#964;&#8048;
&#945;&#8017;&#964;&#8134;&#962;
&#7956;&#967;&#949;&#953;&#957;
&#7936;&#963;&#966;&#945;&#955;&#8182;&#962;
&#954;&#945;&#8054;
&#934;&#8055;&#955;&#953;&#960;&#960;&#959;&#957;
&#964;&#953;&#956;&#969;&#961;&#8053;&#963;&#945;&#963;&#952;&#945;&#953;,
&#954;&#945;&#8054;
&#956;&#8049;&#955;&#8127;
&#7936;&#954;&#961;&#953;&#946;&#8182;&#962;
&#959;&#7990;&#948;&#945;&#903;
&#7952;&#960;&#8127;
&#7952;&#956;&#959;&#8166;
&#947;&#8049;&#961;, &#959;&#8016;
&#960;&#8049;&#955;&#945;&#953;
&#947;&#8051;&#947;&#959;&#957;&#949;&#957;
&#964;&#945;&#8166;&#964;&#8127;
&#7936;&#956;&#966;&#8057;&#964;&#949;&#961;&#945;&#903;
&#957;&#8166;&#957;
&#956;&#8051;&#957;&#964;&#959;&#953;
&#960;&#8051;&#960;&#949;&#953;&#963;&#956;&#945;&#953;
&#964;&#959;&#8166;&#952;&#8127;
&#7985;&#954;&#945;&#957;&#8056;&#957;
&#960;&#961;&#959;&#955;&#945;&#946;&#949;&#8150;&#957;
&#7969;&#956;&#8150;&#957;
&#949;&#7990;&#957;&#945;&#953;
&#964;&#8052;&#957;
&#960;&#961;&#8061;&#964;&#951;&#957;,
&#8005;&#960;&#969;&#962;
&#964;&#959;&#8058;&#962;
&#963;&#965;&#956;&#956;&#8049;&#967;&#959;&#965;&#962;
&#963;&#8061;&#963;&#959;&#956;&#949;&#957;.
&#7952;&#8048;&#957; &#947;&#8048;&#961;
&#964;&#959;&#8166;&#964;&#959;
&#946;&#949;&#946;&#945;&#8055;&#969;&#962;
&#8017;&#960;&#8049;&#961;&#958;&#8131;,
&#964;&#8057;&#964;&#949;
&#954;&#945;&#8054;
&#960;&#949;&#961;&#8054;
&#964;&#959;&#8166;
&#964;&#8055;&#957;&#945;
&#964;&#953;&#956;&#969;&#961;&#8053;&#963;&#949;&#964;&#945;&#8055;
&#964;&#953;&#962; &#954;&#945;&#8054;
&#8003;&#957;
&#964;&#961;&#8057;&#960;&#959;&#957;
&#7952;&#958;&#8051;&#963;&#964;&#945;&#953;
&#963;&#954;&#959;&#960;&#949;&#8150;&#957;&#903;
&#960;&#961;&#8054;&#957; &#948;&#8050;
&#964;&#8052;&#957;
&#7936;&#961;&#967;&#8052;&#957;
&#8000;&#961;&#952;&#8182;&#962;
&#8017;&#960;&#959;&#952;&#8051;&#963;&#952;&#945;&#953;,
&#956;&#8049;&#964;&#945;&#953;&#959;&#957;
&#7969;&#947;&#959;&#8166;&#956;&#945;&#953;
&#960;&#949;&#961;&#8054;
&#964;&#8134;&#962;
&#964;&#949;&#955;&#949;&#965;&#964;&#8134;&#962;
&#8001;&#957;&#964;&#953;&#957;&#959;&#8166;&#957;
&#960;&#959;&#953;&#949;&#8150;&#963;&#952;&#945;&#953;
&#955;&#8057;&#947;&#959;&#957;.
</test>";
var newEncoding = Encoding.UTF8;
var bytes = Encoding.Default.GetBytes(current_month);
var newBytes = Encoding.Convert(Encoding.Default, newEncoding,
bytes);
var msCurrent = new MemoryStream(newBytes);
var attachment = new Attachment(msCurrent, "testattachment3.xml",
MediaTypeNames.Text.Xml);
attachment.ContentType = new ContentType("text/xml");
var message = new MailMessage("them", "me");
message.BodyEncoding = newEncoding;
message.Attachments.Add(attachment);
new SmtpClient("192.168.6.25")
{
UseDefaultCredentials = true
}.Send(message);

No comments:

Post a Comment