Vid*_*hee 21 java email jakarta-mail ioexception jnotify
目前我正在编写一个将要监听目录的代码.当使用.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"
Run Code Online (Sandbox Code Playgroud)
我在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();
}
}
Run Code Online (Sandbox Code Playgroud)
Som*_*Som 33
JavaMail依赖于一些配置文件来将MIME类型映射到Java类(例如,multipart/mixedto 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");
Run Code Online (Sandbox Code Playgroud)
Vid*_*hee 19
通过以下两个步骤可以解决此问题.
在发送电子邮件之前添加当前线程是解决方案:
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
Run Code Online (Sandbox Code Playgroud)
小智 6
我正忙于将Java 8项目转换为Java10。与此同时,我一直在更新所有依赖项。我遇到了类似的例外,上述任何解决方案都不适合我。
我的pom.xml中包含以下内容:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我进行了更多研究,发现以下链接:
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>
Run Code Online (Sandbox Code Playgroud)
解决了这个问题,我得以再次发送带有附件的邮件。
| 归档时间: |
|
| 查看次数: |
34613 次 |
| 最近记录: |