javaMail 奇怪的NullPointerException
来源:互联网 发布:java建立个人博客网站 编辑:程序博客网 时间:2024/06/06 07:08
环境:spring-framework-4.0.6.RELEASE,javamail-1.4.5,xheditor-1.1.14
调用org.springframework.mail.javamail.JavaMailSenderImpl.send(MimeMessage mimeMessage) throws MailException产生的异常:
Exception in thread "main" Java.lang.NullPointerException at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226) at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021) at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
原因:url的网络资源获取不到产生的
if(url.matches("\\w+:.+")) { //网络文件 LogUtil.printLog(url); DataSource dataSource = new URLDataSource(new URL(url)); try { //here will be exception, if can't get net resource dataSource.getInputStream(); } catch (IOException e) { e.printStackTrace(); return ; }
例子:
class SendMail { private int isEmail; void sendMail(){ if(1==isEmail) { UserEmail userEmail=userDao.loadUserEmail(SystemContext.getLoginUser().getId()); List<String> userEmails=userDao.listEmailList(userId); taskExecutor.execute(new SendMailThread(message, userEmails, attachmentDto, newName, uploadPath, userEmail)); } } private class SendMailThread implements Runnable { //need send message private Message message; //need send to the emails List<String> emails; //attachments with the email private AttachmentDto attachmentDto; private String[] newNames; //the local resource path private String path; //the sender email private UserEmail userEmail; public SendMailThread(Message message, List<String> emails, AttachmentDto attachmentDto, String[] newNames, String path, UserEmail userEmail) { this.message = message; this.emails = emails; this.attachmentDto = attachmentDto; this.newNames = newNames; this.path = path; this.userEmail=userEmail; } @Override public void run() { try { SendMail(); } catch (UnsupportedEncodingException | MessagingException | MalformedURLException e) { e.printStackTrace(); } } private void SendMail() throws UnsupportedEncodingException, MessagingException, MalformedURLException { //创建一个JavaMailSenderImpl, 根据用户设置数据 LogUtil.printLog("start to send email!"); JavaMailSenderImpl jmSenderImpl=(JavaMailSenderImpl)mailSender; jmSenderImpl.setHost(userEmail.getHost()); jmSenderImpl.setProtocol(userEmail.getProtocol()); jmSenderImpl.setUsername(userEmail.getUsername()); jmSenderImpl.setPassword(userEmail.getPassword()); MimeMessage email=jmSenderImpl.createMimeMessage(); MimeMessageHelper helper=new MimeMessageHelper(email, true, "utf-8"); helper.setFrom(userEmail.getUsername()); //获取要发送用户的邮件 for(String emailTo: emails) { helper.addTo(emailTo); } helper.setSubject(MimeUtility.encodeText(message.getTitle())); if(attachmentDto.isHasAttachment()) { File[] attachments=attachmentDto.getAttachments(); String[] fileNames=attachmentDto.getAttachmentsFilename(); for(int i=0; i<attachments.length; ++i) { File file=new File(path+File.separator+newNames[i]); String name=fileNames[i]; helper.addAttachment(MimeUtility.encodeText(name), file); } } //格式化内容 String content=message.getContent(); LogUtil.printLog(content); //一、 //必须先设置好内容的值,之后再设置相应的附件,否则最后一个图片不会替换,所以不会显示; //helper.setText(MimeUtility.encodeText(content), true); //List<String> imgSrcs=listContentImgUrl(content); //for(String src: imgSrcs) { // String cid=FilenameUtils.getBaseName(src)+System.currentTimeMillis()+"."+FilenameUtils.getExtension(src); // //注意先添加content到MessageHelper中, 再添加dataSource,才能查找到 // content=content.replace(src, "cid:"+cid); // URLDataSource dataSource=null; // try { // dataSource = new URLDataSource(new URL(src)); // } catch (MalformedURLException e) { // e.printStackTrace(); // } // helper.addInline(cid, dataSource); //} //二、 //建议先把内容设置好,之后再来一次性的添加附件 List<String> imgSrcs=listContentImgUrl(content); Map<String, String> cidImgMap=new HashMap<>(); int i=0; //替换content内容为cid for(String src: imgSrcs) { String cid="image_"+(++i); //注意先添加content到MessageHelper中, 再添加dataSource,才能查找到 if(src.matches("\\w+:.+")) { //网络文件 LogUtil.printLog(src); DataSource dataSource = new URLDataSource(new URL(src)); //get network resource stream,if exception that show it is not available try { dataSource.getInputStream(); content=content.replace(src, "cid:"+cid); cidImgMap.put(cid, src); } catch (IOException e) { e.printStackTrace(); LogUtil.printLog("can't get network resource!!:"+src); } }else { content=content.replace(src, "cid:"+cid); cidImgMap.put(cid, src); } } helper.setText(content, true); //添加cid的dataSource //如果是服务器上的图片,不以http等协议开头的,应该获取文件路径,然后通过FileSystemResource来获取 for(Iterator<String> iterator=cidImgMap.keySet().iterator(); iterator.hasNext(); ) { String cid=iterator.next(); String url=cidImgMap.get(cid); LogUtil.printLog(cid+": "+url); if(url.matches("\\w+:.+")) { //网络文件 LogUtil.printLog(url); DataSource dataSource = new URLDataSource(new URL(url)); try { dataSource.getInputStream(); } catch (IOException e) { e.printStackTrace(); return ; } helper.addInline(cid, dataSource); }else { //本地文件 LogUtil.printLog(path+File.separator+".."+File.separator+url); File file=new File(path+File.separator+".."+File.separator+url); if(!file.exists()) { throw new RuntimeException(file.getAbsolutePath()+" isn't exist!"); } LogUtil.printLog(file.length()); FileSystemResource fsr=new FileSystemResource(path+File.separator+".."+File.separator+url); helper.addInline(cid, fsr); } } LogUtil.printLog(content); jmSenderImpl.send(email); LogUtil.printLog("email send Successful============================="); } //get image source list by regExp private List<String> listContentImgUrl(String content){ Pattern pattern=Pattern.compile("<img[^>]*\\s{1,}src\\s{0,}=\\s{0,}\\\\?['\"]([^'\"]+)\\\\?['\"]"); Matcher matcher=pattern.matcher(content); List<String> imgUrlList=new ArrayList<>(); while(matcher.find()) { imgUrlList.add(matcher.group(1)); } return imgUrlList; } }}
阅读全文
0 0
- javaMail 奇怪的NullPointerException
- 一个无聊的NullPointerException
- Xmap的NullPointerException
- 避免Java的NullPointerException
- NullPointerException异常的原因
- getResources 带来的 NullPointerException
- NullPointerException异常的解决方案
- NullpointerException异常的产生
- struts2的Action nullpointerexception
- 奇妙的NullPointerException
- 莫名其妙的NullPointerException
- NullPointerException异常的原因
- 布局文件的NullPointerException
- 关于数据库的 NullPointerException
- NullPointerException的例子、原因
- NullpointerException的解决方法
- NullPointerException
- NullPointerException
- 页面自动延伸的方法
- Tomcat中更改网站根目录和默认页的配置方法
- Mac 下sublime配置使用Markdown和配置使用源码
- LocalMQ:从零构建类 RocketMQ 高性能消息队列
- Service Hot ITSOM企业级应用SaaS平台——工时管理
- javaMail 奇怪的NullPointerException
- MongoDB基本命令用
- php 获取移动端ip和pc端ip
- 二、文字转成图片,图片上写字
- 软件与硬件的关系
- Android Log三方框架 KLog
- 基于3D卷积神经网络的人体行为理解(论文笔记)
- find 用xargs来处理带空格的文件名文件
- CF841E