我正在尝试使用 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!"