一,MMSCOMP命令行方式
MMSCOMP.exe需要安装nowSms后才有,可在网上搜索下载(此文不再作详细介绍):
1> nowSms的安装目录下将会有mmscomp.exe文件,该应用程序能通过所配置的头文件生成彩信.
2> 首先准备制作彩信所需要的资源文件.
SMIL文件:
--------------------1.smil-----------------
<smil xmlns='http://www.w3.org/2000/SMIL20/CR/Language'>
<head>
<meta name='title' content='create mms' />
<meta name='author' content='aking(lmx88@163.com)' />
<layout>
<root-layout width='122px' height='1424px' background-color='#FFFFFF' />
<region id='Text' width='122px' height='1296px' top='148px' left='0px' fit='hidden' />
<region id='Image' width='122px' height='128px' top='20px' left='0px' fit='hidden' />
</layout>
</head>
<body>
<par>
<img src='1.gif' region='Image' />
<text src='1.txt' region='Text' ></text>
</par>
</body>
</smil>
SMIL文件中所指定的图片文件:1.gif
SMIL文件中所指定的文本文件:1.txt
3> 头描述文件1.hdr
X-Mms-Message-Type: m-retrieve-conf
X-Mms-Transaction-Id: 0123456789
From:03005/TYPE=PLMN
Subject:MMS MSG 下发
头描述文件选项如下:
1,表示该内容为彩信类型.
X-Mms-Message-Type: m-retrieve-conf (required)
2,事物ID,可以是一数字串.
X-Mms-Transaction-Id: text-string
3,版本
X-Mms-Version: 1.0
4,消息ID,不重复的数字串.
Message-Id: text-string (usually x@x format)
5,数据表单
Date: HTTP-date-format
6,彩信发送者号码.可自己填写
From: address@domain or +InternationalPhoneNumber/TYPE=PLMN (Address-present-token is assumed)
7,彩信接收者号码
To: address@domain or +InternationalPhoneNumber/TYPE=PLMN (use multiple headers for multiple recipients)
8,抄送方.
Cc: (same format as To)
9,密送.
Bcc: (same format as To)
10,彩信标题
Subject: text-string
11,类别,缺省为标准Personal
X-Mms-Message-Class: Personal, Advertisement, Informational or Auto (default is Personal)
12,优先级别,缺省为普通
X-Mms-Priority: Low, Normal or High (default is Normal)
13,状态报告,默认为不需要.
X-Mms-Delivery-Report: Yes or No (default is No)
14,已读报告
X-Mms-Read-Reply: Yes or No (default is No)
15,内容类型,MMS的MIME-TYPE为application/vnd.wap.multipart.related.应用服务器如IIS,TOMCAT,RESIN等设置:application/vnd.wap.multipart.related
Content-type: MIME-Type (default is application/vnd.wap.multipart.related, override default with caution!)
16,内容列表,分号隔开文件名与内容类型.
X-NowMMS-Content-Location: filename;content-type (optional, use multiple headers for multiple files)
X-Mms-Message-Type为头文件必需提供的选项,其它参数可选;但建议保留发送方与标题
4> 上面已经准备好了MMS制作的相关文件,现在只需要在DOS下运行命令: mmscomp 1.hdr 1.smil
注意要将所有的资源文件均保存在相同路径下.
出错时将会有扬声器的警报提醒,运行成功后生成1.mms文件.该文件即可用于彩信的发送(关于彩信发送将在以后的文章中介绍.)
二,JAVA编程方式
使用JAVA程序生成MMS文件需要到诺基亚论坛下载NMSS SDK(但我下载时好像已不提供该SDK工具了,若哪位有该工具包希望能发份我),
由于NMSS SDK没有下到,在网上只下到mms java library的JAR文件.该包生成的MMS好像只适合NOKIA系列手机,具体我没作测试.
1> 将MMSLibrary.jar设置到classpath中.
2> 创建JAVA:
-----------------------------------SampleMMSCreation.java----------------------------------
import java.io.*;
import java.util.*;
import java.net.*;
import com.nokia.mms.*;
public class SampleMMSCreation {
public SampleMMSCreation() {
MMMessage mms = new MMMessage();
// 1)Set headers
SetHeaders(mms);
// 2)Add various content parts
AddContents(mms);
MMEncoder encoder = new MMEncoder();
encoder.setMessage(mms);
try {
// 3)Encode the message
encoder.encodeMessage();
byte[] out = encoder.getMessage();
// 4)Print the encoded message to a file
createMmsFile(out, "1.mms");
} catch (Exception e) {
System.out.println("ex="+e.getMessage());
}
}
private void SetHeaders(MMMessage m) {
// Just going to set the mandatories, and the subject...
// Type,TransID,Version are all mandatory, and must be the first
// headers, in this order!
m.setMessageType(IMMConstants.MESSAGE_TYPE_M_SEND_REQ);
m.setTransactionId("0123456789");
m.setVersion(IMMConstants.MMS_VERSION_10);
m.setFrom("+03005/TYPE=PLMN");
m.addToAddress("+13728672084/TYPE=PLMN"); // at least one To/CC/BCC is
// mandatory
m.setSubject("My first test message!"); // subject is optional
// ContentType is mandatory, and must be last header! These last 3 lines
// set the ContentType to
// application/vnd.wml.multipart.related;type="application/smil";start="<0000>"
// In case of multipart.mixed, only the first line is needed (and change
// the constant)
// Any string will do for the Content-ID, but it must match that used
// for the presentation part,
// and it must be Content-ID, it cannot be Content-Location.
m.setPresentationId("<1.smil>"); // presentation part has
m.setContentType(IMMConstants.CT_APPLICATION_MULTIPART_RELATED);
m.setMultipartRelatedType(IMMConstants.CT_APPLICATION_SMIL); // presentation
// part
// is
// written
// in
// SMIL
// Content-ID=<0000>
}
private void AddContents(MMMessage m) {
// This is where the majority of the work is done. Note that here we are
// adding the parts of the
// message in the order we want them to appear. Actually the
// presentation part specifies that,
// but in terminals which cannot understand the presentation part, the
// order may be significant,
// so there seems to be no reason to use random order.
// Note also, that the current version (1.1) of the library encodes the
// message in some sort
// of random order, so developers must either fix that problem using the
// source code, or
// be prepared for random order output.
// Path where contents are stored assumed to be same as this class...
String path = getPath();
//System.out.println("path="+path);
// Add SMIL content
MMContent smil_part = new MMContent();
byte[] b1 = readFile(path + "1.smil");
smil_part.setContent(b1, 0, b1.length);
smil_part.setContentId("<1.smil>"); // If "<>" are used with this
// method, the result is Content-ID
smil_part.setType(IMMConstants.CT_APPLICATION_SMIL);
m.addContent(smil_part);
// Add slide1 text
MMContent s1_text = new MMContent();
byte[] b2 = readFile(path + "77691_1.txt");
s1_text.setContent(b2, 0, b2.length);
s1_text.setContentId("<77691_1.txt>"); // If "<>" are not used with
// this method, the result is
// Content-Location
s1_text.setType(IMMConstants.CT_TEXT_PLAIN);
m.addContent(s1_text);
MMContent s1_image = new MMContent();
byte[] b3 = readFile(path + "76444.gif");
s1_image.setContent(b3, 0, b3.length);
s1_image.setContentId("<76444.gif>");
s1_image.setType(IMMConstants.CT_IMAGE_GIF);
m.addContent(s1_image);
}
private byte[] readFile(String filename) {
int fileSize = 0;
RandomAccessFile fileH = null;
// Opens the file for reading.
try {
fileH = new RandomAccessFile(filename, "r");
fileSize = (int) fileH.length();
} catch (IOException ioErr) {
System.err.println("Cannot find " + filename);
System.err.println(ioErr);
System.exit(200);
}
// allocates the buffer large enough to hold entire file
byte[] buf = new byte[fileSize];
// reads all bytes of file
int i = 0;
try {
while (true) {
try {
buf[i++] = fileH.readByte();
} catch (EOFException e) {
break;
}
}
} catch (IOException ioErr) {
System.out.println("ERROR in reading of file" + filename);
}
return buf;
}
private String getPath() {
URL url = getClass().getResource(getClass().getName() + ".class");
String classPath = url.getHost() + url.getFile();
int pos = classPath.lastIndexOf("/");
return classPath.substring(0, pos + 1);
}
public void createMmsFile(byte[] output, String filename) {
try {
String path = getPath();
File f = new File(path + filename);
FileOutputStream out = new FileOutputStream(f);
out.write(output);
out.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
SampleMMSCreation oa = new SampleMMSCreation();
}
}
--------------------------------------------------------------------------
3> 编译运行JAVA程序即可生成MMS文件,需要注意淘汰文件的路径与CLASS文件一致.
三,总结
虽然两种方式都能生成MMS文件,但下发到手机手第一种方式的能正常打开,第二种方式则提示破损信息.
编程方式实现的MMS创建需要注意很多的细节,比如setContentId是否需要加上<>,还有包里面一个类需要修改成UTF-8编码的问题.
因为第二种方式只有JAR,反编译后能看到JAVA源代码,但未再作更深的研究,希望研究透了的网友能将程序发一份到我的邮箱:lmx88@163.com
四,参考资料
Sending MMS Notifications and Content (http://www.ihub.com/MMS%20Messages.htm)
How To Create MMS Services v4_0
GettingStartedWithMMSv1_1
open mobile alliance (开发移动联盟)