嗨,我想使用javaMailAPI通过邮件发送附件。我的代码是:
public class GmailSender {
private static String HOST = "smtp.gmail.com";
private static String USER = "######";
private static String PASSWORD = "######";
private static String PORT = "465";
private static String FROM = "######";
private static String TO = "######";
private static String STARTTLS = "true";
private static String AUTH = "true";
private static String DEBUG = "true";
private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static String SUBJECT = "Testing JavaMail API";
private static String TEXT = "This is a test message from my java application. Just ignore it";
public static void main(String[] args){
GmailSender.send();
}
public static void makeAttachment(MimeMessage message){
try{
Multipart mp = new MimeMultipart();
//start Code for Attachment
String fileName = "c:/tmp/f.txt";
File f = new File(fileName);
//read file and make byte array
FileInputStream fi = new FileInputStream(f);
byte[] attachmentData = new byte[8000000];
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "c:/tmp/f.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
}catch(Exception e){
System.out.println(">>Exception "+e);
}
}
public static synchronized void send() {
//Use Properties object to set environment properties
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.user", USER);
props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.debug", DEBUG);
props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
try {
//Obtain the default mail session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
//Construct the mail message
MimeMessage message = new MimeMessage(session);
message.setText(TEXT);
message.setSubject(SUBJECT);
message.setFrom(new InternetAddress(FROM));
message.addRecipient(RecipientType.TO, new InternetAddress(TO));
makeAttachment(message);
message.saveChanges();
//Use Transport to deliver the message
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
System.out.println(">>GmailSender "+e);
}
}
}
我得到了这个例外:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
boundary="----=_Part_1_21944831.1329056547036"
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:877)
at javax.activation.DataHandler.writeTo(DataHandler.java:302)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)
at GmailSender.send(GmailSender.java:117)
at GmailSender.main(GmailSender.java:34)
>>GmailSender javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
boundary="----=_Part_1_21944831.1329056547036"
当我运行您的代码示例时,它对我有效,这表明环境有所不同。我在谷歌上找到了这篇帖子,我相信它回答了你的问题。