public boolean updateDraftMail(MailInfo mailInfo, Integer mailType , String mailNum) throws Exception {
//已读写的方式打开草稿箱
Store store = mailInfo.getReceiveProperties();
//qq邮箱的设置
String typeName = "";
switch (mailType) {
case 1:
typeName = "INBOX";
break;
case 2:
typeName = "Sent Messages";
break;
case 3:
typeName = "Drafts";
break;
default:
break;
}
//暂时只支持qq邮箱
Folder folder = store.getFolder(typeName);
folder.open(Folder.READ_WRITE);
//读取已有的邮件
MimeMessage message = null;
try {
message = (MimeMessage) folder.getMessage(Integer.parseInt(mailNum));
} catch (ArrayIndexOutOfBoundsException ex) {
message = (MimeMessage) folder.getMessage(Integer.parseInt(mailNum) - 1);
}
//更新邮件信息
updateMessageMsg(message, mailInfo);
if(null!=folder)
folder.close(true);
if(null!=store)
store.close();
return true;
}
public void updateMessageMsg(MimeMessage mailMessage, MailInfo mailInfo) throws GeneralSecurityException, MessagingException {
//设置可修改
mailMessage.saveChanges();
try {
//设置自定义发件人昵称
String nick="";
try {
if(!StringUtil.isEmpty(mailInfo.getNick())){
nick=javax.mail.internet.MimeUtility.encodeText(mailInfo.getNick());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 设置邮件消息的发送者
mailMessage.setFrom(new InternetAddress(nick+" <"+mailInfo.getUsername()+">"));
// 创建邮件的接收者地址 to:发送;cc:抄送
Address[][] maillToArr = getMailToAddress(mailInfo);
// 设置邮件消息的接收者,发送,抄送
if (maillToArr != null && maillToArr[0] != null && maillToArr[0].length > 0) {
mailMessage.setRecipients(Message.RecipientType.TO, maillToArr[0]);
}
if (maillToArr != null && maillToArr[1] != null && maillToArr[1].length > 0) {
mailMessage.setRecipients(Message.RecipientType.CC, maillToArr[1]);
}
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(Calendar.getInstance().getTime());
// MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart multiPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart bodyPart = new MimeBodyPart();
// 设置html邮件消息内容
bodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
multiPart.addBodyPart(bodyPart);
//添加附件
int i = 0;
if(null!=mailInfo.getAttachFilePaths()&&mailInfo.getAttachFilePaths().length != 0){
for(String attachFile : mailInfo.getAttachFilePaths()){
bodyPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource(attachFile); //得到数据源
bodyPart.setDataHandler(new DataHandler(fds)); //得到附件本身并放入BodyPart
bodyPart.setFileName(MimeUtility.encodeText(mailInfo.getAttachFileNames()[i])); //得到文件名并编码(防止中文文件名乱码)同样放入BodyPart
multiPart.addBodyPart(bodyPart);
i++;
}
}
// 设置邮件消息的主要内容
mailMessage.setContent(multiPart);
} catch (Exception e) {
e.printStackTrace();
}
}
报错信息:
javax.mail.IllegalWriteException: IMAPMessage is read-only
at com.sun.mail.imap.IMAPMessage.setHeader(IMAPMessage.java:813)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2181)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2142)
at util.javaEmail.MailSender.updateMessageMsg(MailSender.java:138)
at util.javaEmail.MailSender.updateDraftMail(MailSender.java:323)
at controller.mail.mailController.updateDraftBox(mailController.java:325)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:152)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
突然想到了,可以把原有邮件删除,然后新增邮件就可以了。。。。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。