1. 程式人生 > >Java mail 郵件傳送 附件檔名過長的問題 版本1.5及以上

Java mail 郵件傳送 附件檔名過長的問題 版本1.5及以上

在使用javamail 1.5版本的時候,如果附件檔名過長,會錯誤顯示為“未命名的附件 00439.dat”

解決方案如下:

一、Javamail jar包退回1.4版本

二、修改引數mail.mime.splitlongparameters為 false

1、新增以下紅色程式碼

messageBodyPart.setFileName(MimeUtility.encodeText(docName));

2、

在new MimeMessage、new MimeMultipart、new MimeBodyPart 之前(一個比較靠前的位置,如果在new MimeMultipart之後新增,有可能無效),新增如下紅色程式碼。

修改完成後記得重啟Tomcat

System.getProperties().setProperty("mail.mime.splitlongparameters", "false");

Properties props = new Properties();

		try {
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.auth", "true");
			props.put("username", from);
			props.put("password", pwd);
			
//			System.getProperties().setProperty("mail.mime.encodefilename", "true");
//			System.getProperties().setProperty("mail.mime.decodefilename", "true");
//			System.getProperties().setProperty("mail.mime.encodeparameters", "false");
//			System.getProperties().setProperty("mail.mime.decodeparameters", "false");


			System.getProperties().setProperty("mail.mime.splitlongparameters", "false");
			
			
			MailAuthenticatorService auth = new MailAuthenticatorService(from,
					pwd);
			Session session = Session.getInstance(props, auth);
			session.setDebug(true);
//			session.setDebug(false);
			Transport transport = null;
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			transport = session.getTransport("smtp");
			InternetAddress[] addresses = new InternetAddress[receiver.size()];
			for (int i = 0; i < receiver.size(); i++) {
				addresses[i] = new InternetAddress(receiver.get(i)
						.getMailAddress());
				logger.debug("==>receiver mail address: "
						+ receiver.get(i).getMailAddress());
			}
			message.setRecipients(Message.RecipientType.TO, addresses);
			message.setSubject(theme);
			Multipart multipart = new MimeMultipart();
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setText(content);
			multipart.addBodyPart(contentPart);
			BodyPart messageBodyPart = new MimeBodyPart();
			DataSource source = new FileDataSource(attachFilePath);
			messageBodyPart.setDataHandler(new DataHandler(source));
			docName = "中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文.doc";
//			docName = "asdfghjklpasdfghjklpasdfghjklpasdfghjklpasdfghjklpasdfghjklpasdfghjklpasdfghjklpasdfghjklpasdfghjklpasdfghjklp.doc";
//			docName = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.doc";
		 
//			messageBodyPart.setFileName(docName);
			messageBodyPart.setFileName(MimeUtility.encodeText(docName));
 
			
			
			multipart.addBodyPart(messageBodyPart);
			message.setContent(multipart);
			message.saveChanges();
			transport.connect(host, user, pwd);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
			logger.debug("email send finish");
		
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			logger.error("email send error");
		}