【问题标题】:Sending an email: no object DCH for MIME type multipart/mixed发送电子邮件:对于 MIME 类型 multipart/mixed 没有对象 DCH
【发布时间】:2013-08-08 23:50:54
【问题描述】:

我正在尝试使用 Java 代码发送电子邮件,但从 GroovyConsole 运行。当我只发送没有附件的电子邮件时,它工作得很好,但一旦我添加附件的多部分逻辑,它就会失败。

编辑:我使用的是 Javamail 1.4.7 版

这是我遇到的错误。

javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 

boundary="----=_Part_16_24710054.1375885523061"

at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:891)

它发生在下面的 Transport.send(mimeMsg) 行。

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


Properties properties = new Properties()
Session session
MimeMessage mimeMsg 

properties.put("mail.smtp.host", "[my host ip]")

session = Session.getDefaultInstance(properties)
mimeMsg = new MimeMessage(session)

String recipient = "[to email address]"

mimeMsg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient))

mimeMsg.setSubject("This is the subject")
mimeMsg.setText("This is the message #1")

mimeMsg.setFrom(new InternetAddress("[from email address]"))

BodyPart messageBodyPart = new MimeBodyPart()
messageBodyPart.setText(mimeMsg.getContent())    
Multipart multipart = new MimeMultipart()

String filePath = "[full & correct path to test.txt]"
DataSource source = new FileDataSource(filePath)
messageBodyPart.setDataHandler(new DataHandler(source))

String fileName = "test.txt"
messageBodyPart.setFileName("test.txt")

multipart.addBodyPart(messageBodyPart)

mimeMsg.setContent(multipart)

Transport.send(mimeMsg)

println "Message Sent!"

【问题讨论】:

  • 您使用的是什么版本的 JavaMail?
  • 我使用的是 1.4.7 版本
  • 认为这是控制台的类加载器问题,见下文:-)

标签: groovy jakarta-mail mime groovy-console


【解决方案1】:

我已经通过以下步骤解决了同样的问题。

1-解压mail.jar文件(解压jar文件后会得到如下文件夹 com,javax,META-INF) 2-将以上所有文件夹放在类文件夹中 3- 重启你的网络服务器。

【讨论】:

    【解决方案2】:

    我认为这是与 GroovyConsole 运行方式有关的类加载器问题...

    如果我将以下 @Grab@GrabcConfig 添加到脚本中,它会起作用...

    @Grab( 'javax.mail:mail:1.4.7' )
    @GrabConfig(systemClassLoader=true, initContextClassLoader=true)
    import javax.mail.*
    import javax.mail.internet.*
    import javax.activation.*
    
    def props = new Properties().with { p ->
        p.'mail.smtp.host' = 'my mail server'
        p
    }
    
    def session = Session.getDefaultInstance( props )
    
    def message = new MimeMessage( session )
    
    message.addRecipient( Message.RecipientType.TO, new InternetAddress( 'to address' ) )
    message.subject = 'This is the subject'
    message.text = 'This is the message #1'
    message.from =  new InternetAddress( 'from address' )
    
    def textpart = new MimeBodyPart()
    textpart.text = 'This is the message #2'
    
    def attachment = new MimeBodyPart()
    attachment.dataHandler = new DataHandler( new FileDataSource( '/path/to/file.txt' ) )
    attachment.fileName = 'file.txt'
    
    def multi = new MimeMultipart()
    multi.addBodyPart( textpart )
    multi.addBodyPart( attachment )
    
    message.content = multi
    
    Transport.send( message )
    

    或者,删除两个 @Grab@GrabConfig 行,然后从命令行运行它:

    groovy -cp /path/to/mail-1.4.7.jar:/path/to/activation-1.1.jar mail.groovy
    

    【讨论】:

    • 我通过 Add Jar(s) To Classpath 选项添加 javax.mail jar,所以我尝试从上面添加 GrabConfig,但它不起作用。它需要 Grab 和 Grabconfig 才能工作。谢谢!
    • @twbbas 很高兴我能帮上忙 :-) 我认为邮件 api 在添加附件时需要查找一些 mime 类型,并且由于运行脚本,它不知道该怎么做。
    • 对,没错,JavaMail需要在jar文件中查找配置文件。
    猜你喜欢
    • 2014-03-18
    • 1970-01-01
    • 2016-02-29
    • 2014-11-18
    • 2016-01-11
    • 1970-01-01
    • 2014-10-16
    • 2017-08-07
    • 2020-06-11
    相关资源
    最近更新 更多