当前位置: 技术问答>java相关
关于javamail的中文问题
来源: 互联网 发布时间:2017-03-27
本文导语: 我想获得邮件的正文的编码方式 如果用MimeMessage.getContentType()方法,这是javamail api doc的相关说明: Returns the value of the RFC 822 "Content-Type" header field. This represents the content-type of the content of this message. This...
我想获得邮件的正文的编码方式
如果用MimeMessage.getContentType()方法,这是javamail api doc的相关说明:
Returns the value of the RFC 822 "Content-Type" header field. This represents the content-type of the content of this message. This value must not be null. If this field is unavailable, "text/plain" should be returned. This implementation uses the getHeader method to obtain the requisite header field.
我查阅了rfc 822,没有找到相关说明。
请问这个方法可能返回什么内容?我怎么去根据不同的编码方式编码?
谁有相关经验,请赐教!
如果用MimeMessage.getContentType()方法,这是javamail api doc的相关说明:
Returns the value of the RFC 822 "Content-Type" header field. This represents the content-type of the content of this message. This value must not be null. If this field is unavailable, "text/plain" should be returned. This implementation uses the getHeader method to obtain the requisite header field.
我查阅了rfc 822,没有找到相关说明。
请问这个方法可能返回什么内容?我怎么去根据不同的编码方式编码?
谁有相关经验,请赐教!
|
这个方法返回的是邮件内容的Content-Type中的信息。比如:Content-Type: text/html;charset="GB2312"
/**
* 得到消息中的内容信息
* @param path 附件保存的路径
* @param path2 当前URL
* @param eventtype 如果eventtype=1,不显示附件
* @return 返回内容信息
*/
//调用此方法前,要调用MailAccept.getMessageArray()方法得到message
public String getMailContent(String path,String path2,int eventtype) {
String s = "";
String sct = "";
this.path = path; //得到附件保存的路径
this.path2 = strdata.indexStr(path2,"/",8); //得到当前URL
this.eventtype = eventtype;
try {
Object o = message.getContent(); //返回所有消息内容
sct = message.getContentType(); //返回ContentType信息
if (message.isMimeType("text/plain")||message.isMimeType("text/html")||sct==null) {
//判断ContentType中是否为GB2312或GBK编码
if (strdata.regionStr(sct,"GB2312")||strdata.regionStr(sct,"gb2312")||strdata.regionStr(sct,"GBK")||strdata.regionStr(sct,"GB2312_CHARSET")) {
s = "
coding = "GB2312";
}
//判断ContentType中是否为big5编码
else if (strdata.regionStr(sct,"big5")||strdata.regionStr(sct,"Big5")) {
s = "
coding = "Big5";
}
//如果没有就调用str类中的getstr进行编码转换
else
s = "
return s;
}
//判断消息中是否有multipart/*头,如果有就进入Multipart处理
if (message.isMimeType("multipart/*")) {
Multipart mp = (Multipart)o;
int cnt = mp.getCount();
for (int j = 0; j
/**
* 得到消息中的内容信息
* @param path 附件保存的路径
* @param path2 当前URL
* @param eventtype 如果eventtype=1,不显示附件
* @return 返回内容信息
*/
//调用此方法前,要调用MailAccept.getMessageArray()方法得到message
public String getMailContent(String path,String path2,int eventtype) {
String s = "";
String sct = "";
this.path = path; //得到附件保存的路径
this.path2 = strdata.indexStr(path2,"/",8); //得到当前URL
this.eventtype = eventtype;
try {
Object o = message.getContent(); //返回所有消息内容
sct = message.getContentType(); //返回ContentType信息
if (message.isMimeType("text/plain")||message.isMimeType("text/html")||sct==null) {
//判断ContentType中是否为GB2312或GBK编码
if (strdata.regionStr(sct,"GB2312")||strdata.regionStr(sct,"gb2312")||strdata.regionStr(sct,"GBK")||strdata.regionStr(sct,"GB2312_CHARSET")) {
s = "
"+(String)o+"";
coding = "GB2312";
}
//判断ContentType中是否为big5编码
else if (strdata.regionStr(sct,"big5")||strdata.regionStr(sct,"Big5")) {
s = "
"+strdata.getstrTobig((String)o)+"";
coding = "Big5";
}
//如果没有就调用str类中的getstr进行编码转换
else
s = "
"+strdata.getstr((String)o)+"";
return s;
}
//判断消息中是否有multipart/*头,如果有就进入Multipart处理
if (message.isMimeType("multipart/*")) {
Multipart mp = (Multipart)o;
int cnt = mp.getCount();
for (int j = 0; j