C#开发彩信帮助利器(MMS、SDP)
作者:PaulLeder 日期:2010-07-07
开发彩信(MM7或基于MM7的SDP)均采用带附件的SOAP方式传输。问题难就难在传输的SOAP还要带附件。
在网上看到很多人把SOAP报文搞得很神秘,现在想回去,这些人应该是没有拿到移动运营商的协议资料才这样的。
但是即使你知道了带附件的SOAP也未必能下手处理彩信分包。这是个最关键的问题。
之前我用过SharpMIMETools,是一个不错的工具,但是因为他并不支持内嵌式的分块,所以我选择了放弃,也许它本身是支持的,只是我没有发现而已。
后来我找到了A C# Implementation of Mime De/encode解决了这样的问题,个人觉得A C# Implementation of Mime De/encode更加方便使用,当然,需要做一些简单的扩展。
比如我的例子,此例子是在原DEMO的基础上修改的。需要修改DEMO里面的MIME类。
引用内容
例子中需要一张命名为00.jpg的图片,不然会报错的,需要扩展Content_ID等。在原DEMO上是会出错的。
最后的结果是:
引用内容
在网上看到很多人把SOAP报文搞得很神秘,现在想回去,这些人应该是没有拿到移动运营商的协议资料才这样的。
但是即使你知道了带附件的SOAP也未必能下手处理彩信分包。这是个最关键的问题。
之前我用过SharpMIMETools,是一个不错的工具,但是因为他并不支持内嵌式的分块,所以我选择了放弃,也许它本身是支持的,只是我没有发现而已。
后来我找到了A C# Implementation of Mime De/encode解决了这样的问题,个人觉得A C# Implementation of Mime De/encode更加方便使用,当然,需要做一些简单的扩展。
比如我的例子,此例子是在原DEMO的基础上修改的。需要修改DEMO里面的MIME类。
引用内容MimeMessage mail = new MimeMessage();
mail.Setversion();
mail.SetContentType("multipart/related; type=\"text/xml\"; start=\"<rootpart@soapui.org>\"");
mail.SetBoundary(null);
// Add a text body part
// default Content-Type is "text/plain"
// default Content-Transfer-Encoding is "7bit"
MimeBody mBody = mail.CreatePart();
//mBody.SetContentType("text/xml; charset=UTF-8");
mBody.SetTransferEncoding("8bit");
mBody.SetContentID("rootpart@soapui.org");
System.Text.StringBuilder sb1 = new StringBuilder();
sb1.AppendLine("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:loc=\"http://www.csapi.org/schema/parlayx/multimedia_messaging/send/v2_4/local\">");
sb1.AppendLine("<soapenv:Header/>");
sb1.AppendLine(" <soapenv:Body>");
sb1.AppendLine(" </soapenv:Body>");
sb1.AppendLine("</soapenv:Envelope>");
mBody.SetText(sb1.ToString());
mBody = mail.CreatePart();
mBody.SetTransferEncoding("base64");
mBody.SetContentType("images/jpg");
mBody.ReadFromFile("00.jpg");
mBody.SetContentID("001.jpg");
//Add an embeded multipart
mBody = mail.CreatePart();
mBody.SetContentType("multipart/mixed");
mBody.SetBoundary(null);
MimeBody mBodyChild = mBody.CreatePart();
mBodyChild.SetContentID("Part 1.txt");
mBodyChild.ReadFromFile("a.txt");
mBodyChild = mBody.CreatePart();
mBodyChild.SetContentID("Part 2.txt");
mBodyChild.SetText("Content of Part 2\r\n");
//store content to a string buffer
StringBuilder sb = new StringBuilder();
mail.StoreBody(sb);
StreamWriter sw = new StreamWriter("aaa2.txt");
sw.Write(sb.ToString());
sw.Close();
StreamReader sr = new StreamReader("aaa2.txt");
string message = sr.ReadToEnd();
MimeMessage aMimeMessage = new MimeMessage();
aMimeMessage.LoadBody(message);
ArrayList bodylist = new ArrayList();
aMimeMessage.GetBodyPartList(bodylist);
for (int i = 0; i < bodylist.Count; i++)
{
MimeBody ab = (MimeBody)bodylist[i];
if (ab.IsText())
{
string m = ab.GetText();
System.Windows.Forms.MessageBox.Show(m);
}
else if (ab.IsAttachment())
{
ab.WriteToFile("new" + ab.GetContentID());
}
}
mail.Setversion();
mail.SetContentType("multipart/related; type=\"text/xml\"; start=\"<rootpart@soapui.org>\"");
mail.SetBoundary(null);
// Add a text body part
// default Content-Type is "text/plain"
// default Content-Transfer-Encoding is "7bit"
MimeBody mBody = mail.CreatePart();
//mBody.SetContentType("text/xml; charset=UTF-8");
mBody.SetTransferEncoding("8bit");
mBody.SetContentID("rootpart@soapui.org");
System.Text.StringBuilder sb1 = new StringBuilder();
sb1.AppendLine("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:loc=\"http://www.csapi.org/schema/parlayx/multimedia_messaging/send/v2_4/local\">");
sb1.AppendLine("<soapenv:Header/>");
sb1.AppendLine(" <soapenv:Body>");
sb1.AppendLine(" </soapenv:Body>");
sb1.AppendLine("</soapenv:Envelope>");
mBody.SetText(sb1.ToString());
mBody = mail.CreatePart();
mBody.SetTransferEncoding("base64");
mBody.SetContentType("images/jpg");
mBody.ReadFromFile("00.jpg");
mBody.SetContentID("001.jpg");
//Add an embeded multipart
mBody = mail.CreatePart();
mBody.SetContentType("multipart/mixed");
mBody.SetBoundary(null);
MimeBody mBodyChild = mBody.CreatePart();
mBodyChild.SetContentID("Part 1.txt");
mBodyChild.ReadFromFile("a.txt");
mBodyChild = mBody.CreatePart();
mBodyChild.SetContentID("Part 2.txt");
mBodyChild.SetText("Content of Part 2\r\n");
//store content to a string buffer
StringBuilder sb = new StringBuilder();
mail.StoreBody(sb);
StreamWriter sw = new StreamWriter("aaa2.txt");
sw.Write(sb.ToString());
sw.Close();
StreamReader sr = new StreamReader("aaa2.txt");
string message = sr.ReadToEnd();
MimeMessage aMimeMessage = new MimeMessage();
aMimeMessage.LoadBody(message);
ArrayList bodylist = new ArrayList();
aMimeMessage.GetBodyPartList(bodylist);
for (int i = 0; i < bodylist.Count; i++)
{
MimeBody ab = (MimeBody)bodylist[i];
if (ab.IsText())
{
string m = ab.GetText();
System.Windows.Forms.MessageBox.Show(m);
}
else if (ab.IsAttachment())
{
ab.WriteToFile("new" + ab.GetContentID());
}
}
例子中需要一张命名为00.jpg的图片,不然会报错的,需要扩展Content_ID等。在原DEMO上是会出错的。
最后的结果是:
引用内容MIME-Version: 1.0
Content-Type: multipart/related;
type="text/xml";
start="<rootpart@soapui.org>";
boundary="__=_Part_Boundary_930842489_308062783"
--__=_Part_Boundary_930842489_308062783
Content-Transfer-Encoding: 8bit
Content-ID: <rootpart@soapui.org>
Content-Type: text/plain;
charset="gb2312"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="http://www.csapi.org/schema/parlayx/multimedia_messaging/send/v2_4/local">
<soapenv:Header/>
<soapenv:Body>
</soapenv:Body>
</soapenv:Envelope>
--__=_Part_Boundary_930842489_308062783
Content-Transfer-Encoding: base64
Content-Type: images/jpg
Content-ID: <001.jpg>
图片的BASE64编码
--__=_Part_Boundary_930842489_308062783
Content-Type: multipart/mixed;
boundary="__=_Part_Boundary_1945173545_95082901"
--__=_Part_Boundary_1945173545_95082901
Content-ID: <Part 1.txt>
Content-Transfer-Encoding: base64
ZmRhc2ZhZHNmYQ0KZmRzYWYNCmRzZmRzYWZkc7eiyfq087f5tdi3vbCitPTJs7eiw7vBy7XEt6LJ
1bX6DQq3osn6tPO3+WRzZmRhc2bKv7Tzt/K12Le9
--__=_Part_Boundary_1945173545_95082901
Content-ID: <Part 2.txt>
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset="gb2312"
Content of Part 2
--__=_Part_Boundary_1945173545_95082901--
--__=_Part_Boundary_930842489_308062783--
Content-Type: multipart/related;
type="text/xml";
start="<rootpart@soapui.org>";
boundary="__=_Part_Boundary_930842489_308062783"
--__=_Part_Boundary_930842489_308062783
Content-Transfer-Encoding: 8bit
Content-ID: <rootpart@soapui.org>
Content-Type: text/plain;
charset="gb2312"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="http://www.csapi.org/schema/parlayx/multimedia_messaging/send/v2_4/local">
<soapenv:Header/>
<soapenv:Body>
</soapenv:Body>
</soapenv:Envelope>
--__=_Part_Boundary_930842489_308062783
Content-Transfer-Encoding: base64
Content-Type: images/jpg
Content-ID: <001.jpg>
图片的BASE64编码
--__=_Part_Boundary_930842489_308062783
Content-Type: multipart/mixed;
boundary="__=_Part_Boundary_1945173545_95082901"
--__=_Part_Boundary_1945173545_95082901
Content-ID: <Part 1.txt>
Content-Transfer-Encoding: base64
ZmRhc2ZhZHNmYQ0KZmRzYWYNCmRzZmRzYWZkc7eiyfq087f5tdi3vbCitPTJs7eiw7vBy7XEt6LJ
1bX6DQq3osn6tPO3+WRzZmRhc2bKv7Tzt/K12Le9
--__=_Part_Boundary_1945173545_95082901
Content-ID: <Part 2.txt>
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset="gb2312"
Content of Part 2
--__=_Part_Boundary_1945173545_95082901--
--__=_Part_Boundary_930842489_308062783--
评论: 0 | 引用: 0 | 查看次数: 138
发表评论
上一篇
下一篇

文章来自:
Tags:
相关日志:






