message.setSubject(subject);
message.setHeader("X-Mailer",mailer);
message.setHeader("X-Priority",priority);
message.setSentDate(new Date());
message.setText(text+writenametext+mytext);
Transport.send(message);
message.setHeader("X-Mailer",mailer);
message.setHeader("X-Priority",priority);
message.setSentDate(new Date());
message.setText(text+writenametext+mytext);
Transport.send(message);
public String getPart(Part part, int partNum,int msgNum,int eventtype) throws MessagingException,IOException {
String s="";
String s2="";
String s5="";
String sct = part.getContentType();//返回multipart/*头下的所有内容
boolean blnnum = true;
if (sct == null) {
s="无效";
return s;
}
ContentType ct = new ContentType(sct); //进行头处理
if (ct.match("text/plain") || ct.match("text/html")) {
//判断ContentType中是否为GB2312或GBK编码
if (strdata.regionStr(sct,"GB2312")||strdata.regionStr(sct,"gb2312")||strdata.regionStr(sct,"GBK")||strdata.regionStr(sct,"iso-gb2312")) {
s1 = "<pre>"+(String)part.getContent()+"</pre>";
coding = "GB2312";
}
//判断ContentType中是否为big5编码
else if (strdata.regionStr(sct,"big5")||strdata.regionStr(sct,"Big5")) {
s1 = "<pre>"+strdata.getstrTobig((String)part.getContent())+"</pre>";
coding = "Big5";
}
//如果没有就调用str类中的getstr进行编码转换
else s1 = "<pre>"+strdata.getstr((String)part.getContent())+"</pre>";
}
//如果multipart/*头中还有multipart/alternative头就进行处理
if(part.isMimeType("multipart/*")) {
String s6="";
Multipart mp = (Multipart)part.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
if(mp.getBodyPart(i).isMimeType("text/plain") || mp.getBodyPart(i).isMimeType("text/html")||mp.getBodyPart(i).isMimeType("multipart/*"))
s5=getPart(mp.getBodyPart(i),i,3,eventtype);
}
return s5;
}
//对附件进行处理
if (blnAttachment(part)) {
if (eventtype==1) return s1; //不显示附件
s3 = "附件:" + getMailAttachment(part) + "<br>" + s3; //得到附件名和下载链接
}
s = s1+s3;
return s;
}/**
* 得到消息中的内容信息
* @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 = "<pre>"+(String)o+"</pre>";
coding = "GB2312";
}
//判断ContentType中是否为big5编码
else if (strdata.regionStr(sct,"big5")||strdata.regionStr(sct,"Big5")) {
s = "<pre>"+strdata.getstrTobig((String)o)+"</pre>";
coding = "Big5";
}
//如果没有就调用str类中的getstr进行编码转换
else
s = "<pre>"+strdata.getstr((String)o)+"</pre>";
return s;
}
//判断消息中是否有multipart/*头,如果有就进入Multipart处理
if (message.isMimeType("multipart/*")) {
Multipart mp = (Multipart)o;
int cnt = mp.getCount();
for (int j = 0; j < cnt; j++) {
s="<br>"+getPart(mp.getBodyPart(j),j,2,eventtype);
}
}
}
catch(MessagingException e ) {
System.err.println("MailContent.getMailContent(): " + e.toString());
}
catch(Exception e) {
System.err.println("MailContent.getMailContent(): " + e.toString());
}
return s;
}
我处理信息体的方式与你的有所不同。具体如下:
当前我的问题主要集中在 Content-Type:text/html;charset="gb2312"
我这里有一些比较特殊的邮件,在用下面的程序处理时都会出现问题,有兴趣的话,可以给你发几封。你的程序中
if (sct == null) {
s="无效";
return s;
}
经过测试存在问题,CSDN账号密码修改 后发给你的修改确认邮件报文中,就没有 Content-Type:text/* ……等设置。
**************************************************
// mailmessage.java
......
/**
* 返回消息体
* @return Object 消息体
*/
public Object getContent() throws MessagingException, IOException {
if (content==null) {
System.out.println("content is null");
retrieveContent();
}
return super.getContent();
}
/**
* 返回消息体类型
* @return String 消息体类型
*/
public String getMessageType() throws MessagingException {
String contentType = this.getContentType();
System.out.println(contentType);
if ( contentType== null)
return "UNKNOWN CONTENTTYPE";
else if ( contentType.indexOf("text/plain") >-1 )
return "text/plain";
else if ( contentType.indexOf("text/html") >-1 ) {
ContentType ct = new ContentType(contentType); //进行头处理
// System.out.println("SubType:" + ct.getSubType()); //html
// System.out.println("PrimaryType:" + ct.getPrimaryType()); //text
// System.out.println("Parameter:" + ct.getParameter("Content-Type:")); //null
ct.setParameter( "Content-Type:","text/html" );
System.out.println("setParam ago:" + this.getContentType() );
ct.setSubType("html");
System.out.println("setSubType ago:" + this.getContentType() );
ct.setPrimaryType("text");
System.out.println("setPrimaryType ago:" + this.getContentType() );
return "text/html";
}
else return contentType;
}
/**
* 获得消息体
*/
protected void retrieveContent() throws MessagingException,IOException {
this.getMessageType();
if (contentOffset < 0 || content!=null) return;
int fetchsize = MaildirStore.fetchsize;
byte bytes[];
RandomAccessFile file =
new RandomAccessFile(folder.getFullName() + File.separator + getMessageID(), "r");
file.seek(contentOffset);
String line;
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (line = file.readLine(); line!=null; line = file.readLine()) {
out.write(line.getBytes("ISO8859-1"));
out.write('\n');
}
content = out.toByteArray();
if (isNewMessage()) this.setFlag(this.flagMessageRead);
}
......
// messageview.java
......
try {
setContent(message.getContent());
message.hasAttachment(this.hasAttachment);
}
catch(Exception e) {
body.append(CodeConver.getISO("读取信件内容错误"));
e.printStackTrace();
}
}
private void setContent(Object obj){
if(obj instanceof Multipart){
System.out.println("message multipart type");
try{
Multipart mp = (Multipart)obj;
int count = mp.getCount();
for(int i=0;i<count;i++)
dumpPart(mp.getBodyPart(i));
if (attachList.size()>0) this.hasAttachment = true;
String content = body.toString();
if (content != null) {
String contenttype = null;
if (count > 0)
contenttype = mp.getBodyPart(0).getContentType();
if (contenttype != null && contenttype.indexOf("charset") > 0) {
int cur = contenttype.indexOf("charset");
String charset = contenttype.substring(cur + 9);
charset = charset.substring(0,charset.length() - 1);
if (charset != null && !CONST.DefaultCharSet.equals(charset) && !"us-ascii".equals(charset)) {
body = null;
body = new StringBuffer(new String(content.getBytes(charset),CONST.DefaultCharSet));
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
else if (obj instanceof String) {
System.out.println("message String type");
body.append((String)obj);
}else {
body.append(CodeConver.getISO("无法识别的信件体"));
}
} private int level = 0;
/**
* DumpPart
* @param p Part
*/
private void dumpPart(Object p) throws MessagingException,IOException {
if (p == null) return; if (p instanceof String) {
System.out.println("p instanceof String");
body.append((String)p);
body.append(Separator);
return;
}
if (p instanceof Multipart) {
System.out.println("p instanceof Multipart");
MimeMultipart mp = (MimeMultipart)p;
level++;
int count = mp.getCount();
for (int i= 0; i < count; i++) {
dumpPart((Object)mp.getBodyPart(i));
}
level--;
return;
}
if (p instanceof BodyPart) {
System.out.println("p instanceof BodyPart");
BodyPart bp = (BodyPart)p;
if (bp == null) return;
String tmpFileName = bp.getFileName();
if (tmpFileName != null && tmpFileName.length()>0) {
attachList.addElement(tmpFileName);
}
else {
if (bp.isMimeType("text/plain")) {
body.append((String)bp.getContent());
body.append(Separator);
}
else if (bp.isMimeType("text/html")) {
attachList.addElement("unknown.html");
}
}
}
}