簡體   English   中英

Java:發送附件電子郵件時獲取異常

[英]Java: Getting Exception while sending attachment email

我在java中發送電子郵件時遇到運行時異常UnsupportedDataTypeException 這是一個例外

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
    boundary="----=_Part_0_764977973.1480687764115"

我該如何處理這個例外?

我正在使用這段代碼:完整的代碼

public static void main(String[] args) {

    String senderMail = "inzi769@gmail.com";
    String recepMail = "inzi.programmer@gmail.com";
    String pass = "*********";
    String host = "smtp.gmail.com";
    String filePath = "C:\\Users\\Inzimam\\Desktop\\helicopter_final.png";

    sendJavaMail(senderMail, pass, recepMail, host, filePath);
}

private static void sendJavaMail(String senderMail, String pass, String recepMail, String host, String filePath) {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", "25");
    // Get the Session object.
    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(senderMail, pass);
                }
            });
    session.setDebug(true);
    try {

        Message message = new MimeMessage(session);            
        message.setFrom(new InternetAddress(senderMail));            
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recepMail));            
        message.setSubject("Subject here");            
        BodyPart messageBodyPart = new MimeBodyPart();            
        messageBodyPart.setText("This is message body");            
        Multipart multipart = new MimeMultipart();            
        multipart.addBodyPart(messageBodyPart);            
        messageBodyPart = new MimeBodyPart();


        DataSource source = new FileDataSource(filePath);
        messageBodyPart.setDataHandler(new DataHandler(source));
        multipart.addBodyPart(messageBodyPart);            
        message.setContent(multipart); 
        SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
        t.connect("smtp.gmail.com", senderMail, pass);
        t.sendMessage(message, message.getAllRecipients());
        t.close();
//           Transport.send(message);

        JOptionPane.showMessageDialog(null, "Message has been sent  successfully!.");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

請指出我錯了。 謝謝

首先我使用的是Javamail API 1.4.6但是現在使用Javamail API 1.5.0或更高版本,上面的相同代碼工作正常。 所以,現在使用API​​ 1.5.0我能夠成功發送附件。

編輯:我使用時使用API​​ 1.4.6

Transport.send(message);

它不起作用但是使用API​​ 1.5.0或更高版本我們也可以使用

Transport.send(message);

代替

SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
            t.connect("smtp.gmail.com", senderMail, pass);
            t.sendMessage(message, message.getAllRecipients());
            t.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM