2012-02-12 64 views
1

嗨我想用javaMailAPI發送郵件附件。我的代碼是:無法使用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" 

回答

1

您的代碼示例爲我工作,當我運行它,它指向環境的差異。我搜索了一下,發現this post,我相信這可以解答你的問題。

+0

謝謝高度不飽和,嘗試過你的suggessioin,當我把最新的mail.jar從http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html# javamail-1.4.4-oth-JPR,代碼按預期工作。 – 2012-02-12 18:06:41

相關問題