目前我正在内联编写一段代码,该代码将监听一个目录。当目录用.apk文件更新时,我会用这个.apk文件给一个gmail帐户发送一封邮件。我在我的程序中使用了Jnotify和JAVA Mail。
我得到的错误是,
javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_145238.1392728439484"
我寻找了stackoverflow中给出的解决方案来寻求帮助,但它们都没有帮助。
提前感谢
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name);
if (name.contains(".apk"))
SendEmail(name);
else
System.out.println("Not the APK file");
}
void SendEmail(String strname){
String Path = "D:/POC/Email/apk folder/"+strname;
System.out.println("Path->" + Path);
Properties props = new Properties();
props.put("mail.smtp.host","173.194.78.108");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","465");
System.out.println("Properties has been set properly");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("SenderEmailID@gmail.com", "senderPassword");
}
}
);
System.out.println("Session Created successfully");
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("SenderEmailID@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("ToReceiverEmailID@gmail.com"));
message.setSubject("Android : " + strname);
MimeBodyPart msgbody = new MimeBodyPart();
msgbody.setText("This is the message content which is sent using JAVA MAIL 1.4.5");
Multipart mulpart = new MimeMultipart();
mulpart.addBodyPart(msgbody);
//Attachement Starts here.
msgbody = new MimeBodyPart();
javax.activation.DataSource source = new FileDataSource(Path);
msgbody.setDataHandler(new DataHandler(source));
msgbody.setFileName(strname);
message.setContent(mulpart);
System.out.println("Attached the message going to start transporting the mail");
//If I've the code before sending the email is getting sent but without attachment.
//Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
Transport.send(message);
System.out.println("Mail Sent successfully");
}
catch(MessagingException msg){
msg.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
发布于 2014-09-04 01:10:07
JavaMail依赖于一些配置文件将MIME类型映射到Java类(例如,multipart/mixed
到javax.mail.internet.MimeMultipart
)。这些配置文件是使用应用程序的ClassLoader加载的。如果ClassLoader不能正常工作,将找不到这些配置文件。
您只需添加以下行即可。这就解决了问题。
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content- handler=com.sun.mail.handlers.message_rfc822");
发布于 2015-11-13 01:25:58
在发送电子邮件之前添加当前线程是解决方案:
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
发布于 2018-06-27 12:46:27
我正忙于将一个Java 8项目转换为Java 10,同时我一直在更新所有的依赖项。我得到了一个类似的异常,上面的解决方案对我都不起作用。
我的pom.xml中有以下内容:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
我做了更多的研究,找到了以下链接:
http://www.jguru.com/faq/view.jsp?EID=237257
因此,我尝试向我的pom.xml添加以下依赖项:
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
这解决了问题,我可以再次发送带有附件的邮件了。
https://stackoverflow.com/questions/21856211
复制相似问题