高分:。。。《无人问津》。。怎样存储邮件内容content为Object类型到SQL Server 请给出你的邮件代码,在getContent的返回值里有描述这个Object是一个什么样的对象。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String mbox = "INBOX"; Folder folder = store.getFolder(mbox); folder.open(Folder.READ_WRITE); Message[] messages = folder.getMessages(); /****************** 存储邮件服务器中的邮件 ******************/ for(int i=0; i<messages.length; i++) { int InboxID=i+InboxMsgTotalNum+1; //要存储的邮件在INBOX的位置 javax.mail.Address[] addr=messages[i].getFrom(); String senderAddr=new String(); senderAddr=addr[0].toString(); senderAddr.trim(); int start=senderAddr.indexOf("<"); int end=senderAddr.indexOf(">"); String sender=new String(); sender=senderAddr.substring(start+1, end); // 获得发送者地址 String subject=new String(); subject=messages[i].getSubject(); Object MsgContent = messages[i].getContent(); MsgContent = messages[i].getContent(); //怎样存储到数据库中????? //邮件的内容出错:javax.mail.internet.MimeMultipart@4d2197 java.text.Format formatter = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm"); String sendtime=new String(); sendtime=formatter.format(messages[i].getSentDate()); out.println(sendtime); int size=messages[i].getSize(); //执行数据库插入操作 String ConnInsert =new String(); ConnInsert = "jdbc:odbc:webmail"; //数据源 Connection connectInsert =null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(java.lang.ClassNotFoundException e) { System.err.println( e.getMessage()); } try { //连接数据库 connectInsert = DriverManager.getConnection(ConnInsert,logName,passwd); Statement stmtInsert = connectInsert.createStatement(); String sqlInsert=new String(); //sqlInsert="insert into INBOX(ID,userID,sender,subject,size)"; //sqlInsert+=" values('"+InboxID+"','"+userID+"','"+sender+"','"+subject+"','"+size+"')"; sqlInsert="insert into INBOX(ID,userID,sender,subject,sendtime,content,size)"; sqlInsert+=" values('"+InboxID+"','"+userID+"','"+sender+"','"+subject+"','"+sendtime+"','"+contents+"','"+size+"')"; stmtInsert.executeUpdate(sqlInsert); } catch(SQLException ex) { System.err.println(ex.getMessage()); } /****************** 判断是否附件 ************************/ if(messages[i].isMimeType("multipart/*")) //附件 { FetchProfile fp=new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add("X-Mailer"); folder.fetch(messages,fp); Object content=messages[i].getContent(); Multipart multipart = (Multipart)content; for (int c=0, n=multipart.getCount(); c<n; c++) { Part part = multipart.getBodyPart(c); //String msg=getStr(part.getContent()); /***StringBuffer buf=new StringBuffer(msg.length()+6); char ch=' '; for(int u=0;u<msg.length();u++) { ch=msg.charAt(u); if(ch=='\n')buf.append(""); else buf.append(ch); } String mm = buf.toString(); ********/ // out.println(new String(mm.getBytes("iso-8859-1"),"gb2312")); String disposition = null; disposition = part.getDisposition(); if ( disposition != null &&(disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) { //String fileName=getStr(part.getFileName()); //转换成中文GB2313 String fileName=null,AttachName =new String(); fileName=part.getFileName(); AttachName =fileName; //附件文件名 InputStream inputstream=part.getInputStream(); if(fileName==null) { fileName=File.createTempFile("attachment",".out").getName(); } String filePath=new String(); filePath="c:\\MailAttachment\\"+username+"\\" ; // 存储路径 fileName=filePath+fileName; File file=new File(fileName); for(int j=0;file.exists();j++) { file=new File(fileName+j); } FileOutputStream fileoutputstream =new FileOutputStream(file); BufferedOutputStream bufferedoutputstream =new BufferedOutputStream(fileoutputstream); BufferedInputStream bufferedinputstream =new BufferedInputStream(inputstream); int m; while ((m=bufferedinputstream.read())!=-1) bufferedoutputstream.write(m); //save the filename String SqlUpdate=new String(); SqlUpdate="UPDATE INBOX SET AttachmentName='"+AttachName+"',AttachmentPath='"+filePath+"' "; SqlUpdate+="WHERE ID='"+InboxID+"'"; //InboxID executeUpdateSql(SqlUpdate); bufferedoutputstream.flush(); bufferedoutputstream.close(); bufferedinputstream.close(); } } //out.println("Attachment2"); } connectInsert.close(); } //FOR 循环结束 Java中线程池中断子线程的问题。有代码,有经验的来看看。 PDF自动打印的问题~ java的事件解决?在线等? 怎样将char型数组转化成字符串类型 java 中如何存取和操作10亿这么大的数? 老鸟进。 在方法重载中方法的修饰符为什么要用static 请问,怎么才能直接调用outlook中的“新邮件”窗口,来发送邮件。如何直接将对方邮件地址传入。(比如网页上提供的该公司email发送)。 import的问题,请大家帮忙。 运行java会修改注册表码??在线等待!!!!! 不用上班也有钱花了,真是乐哉乐哉 处理键盘的事件有哪些方法呀?都是什么呢? 求助!!!!!!!
String mbox = "INBOX";
Folder folder = store.getFolder(mbox);
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
/****************** 存储邮件服务器中的邮件 ******************/
for(int i=0; i<messages.length; i++)
{
int InboxID=i+InboxMsgTotalNum+1; //要存储的邮件在INBOX的位置
javax.mail.Address[] addr=messages[i].getFrom();
String senderAddr=new String();
senderAddr=addr[0].toString();
senderAddr.trim();
int start=senderAddr.indexOf("<");
int end=senderAddr.indexOf(">");
String sender=new String();
sender=senderAddr.substring(start+1, end); // 获得发送者地址
String subject=new String();
subject=messages[i].getSubject();
Object MsgContent = messages[i].getContent();
MsgContent = messages[i].getContent(); //怎样存储到数据库中?????
//邮件的内容出错:javax.mail.internet.MimeMultipart@4d2197 java.text.Format formatter = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm");
String sendtime=new String();
sendtime=formatter.format(messages[i].getSentDate());
out.println(sendtime);
int size=messages[i].getSize();
//执行数据库插入操作
String ConnInsert =new String();
ConnInsert = "jdbc:odbc:webmail"; //数据源
Connection connectInsert =null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.println( e.getMessage());
}
try {
//连接数据库
connectInsert = DriverManager.getConnection(ConnInsert,logName,passwd);
Statement stmtInsert = connectInsert.createStatement();
String sqlInsert=new String();
//sqlInsert="insert into INBOX(ID,userID,sender,subject,size)";
//sqlInsert+=" values('"+InboxID+"','"+userID+"','"+sender+"','"+subject+"','"+size+"')";
sqlInsert="insert into INBOX(ID,userID,sender,subject,sendtime,content,size)";
sqlInsert+=" values('"+InboxID+"','"+userID+"','"+sender+"','"+subject+"','"+sendtime+"','"+contents+"','"+size+"')";
stmtInsert.executeUpdate(sqlInsert);
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
/****************** 判断是否附件 ************************/
if(messages[i].isMimeType("multipart/*")) //附件
{
FetchProfile fp=new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add("X-Mailer");
folder.fetch(messages,fp);
Object content=messages[i].getContent();
Multipart multipart = (Multipart)content;
for (int c=0, n=multipart.getCount(); c<n; c++)
{
Part part = multipart.getBodyPart(c);
//String msg=getStr(part.getContent());
/***StringBuffer buf=new StringBuffer(msg.length()+6);
char ch=' ';
for(int u=0;u<msg.length();u++)
{
ch=msg.charAt(u);
if(ch=='\n')buf.append("");
else buf.append(ch);
}
String mm = buf.toString(); ********/
// out.println(new String(mm.getBytes("iso-8859-1"),"gb2312"));
String disposition = null;
disposition = part.getDisposition();
if ( disposition != null &&(disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE)))
{
//String fileName=getStr(part.getFileName()); //转换成中文GB2313
String fileName=null,AttachName =new String();
fileName=part.getFileName();
AttachName =fileName; //附件文件名
InputStream inputstream=part.getInputStream();
if(fileName==null)
{
fileName=File.createTempFile("attachment",".out").getName();
}
String filePath=new String();
filePath="c:\\MailAttachment\\"+username+"\\" ; // 存储路径
fileName=filePath+fileName;
File file=new File(fileName);
for(int j=0;file.exists();j++)
{
file=new File(fileName+j);
}
FileOutputStream fileoutputstream =new FileOutputStream(file);
BufferedOutputStream bufferedoutputstream =new BufferedOutputStream(fileoutputstream);
BufferedInputStream bufferedinputstream =new BufferedInputStream(inputstream);
int m;
while ((m=bufferedinputstream.read())!=-1)
bufferedoutputstream.write(m);
//save the filename
String SqlUpdate=new String();
SqlUpdate="UPDATE INBOX SET AttachmentName='"+AttachName+"',AttachmentPath='"+filePath+"' ";
SqlUpdate+="WHERE ID='"+InboxID+"'"; //InboxID
executeUpdateSql(SqlUpdate);
bufferedoutputstream.flush();
bufferedoutputstream.close();
bufferedinputstream.close();
}
}
//out.println("Attachment2");
}
connectInsert.close();
} //FOR 循环结束