2013-03-25 95 views
1

我試圖發送沒有用戶交互的郵件。我創建了以下類:試圖在沒有用戶交互的情況下發送郵件:java.lang.NoClassDefFoundError:javax.activation.DataHandler

import java.util.Calendar; 
import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMultipart; 

public class MailService { 
    // public static final String MAIL_SERVER = "localhost"; 

    private String toList; 
    private String ccList; 
    private String bccList; 
    private String subject; 
    final private static String SMTP_SERVER = "smtp.gmail.com"; 
    final private static String SMTP_TLSENABLE = "true"; 
    final private static String SMTP_AUTH = "true"; 
    final private static String SMTP_PORT = "587"; 
    final private static String SMTP_USERNAME = "[email protected]"; 
    final private static String SMTP_PASSWORD = "Password"; 


    private String from; 
    private String txtBody; 
    private String htmlBody; 
    private String replyToList; 
    private boolean authenticationRequired = false; 

    public MailService(String from, String toList, String subject, String txtBody, String htmlBody) { 
     this.txtBody = txtBody; 
     this.htmlBody = htmlBody; 
     this.subject = subject; 
     this.from = from; 
     this.toList = toList; 
     this.ccList = null; 
     this.bccList = null; 
     this.replyToList = null; 
     this.authenticationRequired = true; 

    } 


    public void sendAuthenticated() throws AddressException, MessagingException { 
     authenticationRequired = true; 
     send(); 
    } 

    /** 
    * Send an e-mail 
    * 
    * @throws MessagingException 
    * @throws AddressException 
    */ 
    public void send() throws AddressException, MessagingException { 
     Properties props = new Properties(); 

     // set the host smtp address 
     props.put("mail.smtp.host", SMTP_SERVER); 
     props.put("mail.user", from); 

     props.put("mail.smtp.starttls.enable", SMTP_TLSENABLE); // needed for gmail 
     props.put("mail.smtp.auth", SMTP_AUTH); // needed for gmail 
     props.put("mail.smtp.port", SMTP_PORT); // gmail smtp port 

     /*Authenticator auth = new Authenticator() { 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("[email protected]", "mypassword"); 
      } 
     };*/ 


     Session session; 

     if (authenticationRequired) { 
      Authenticator auth = new SMTPAuthenticator(); 
      props.put("mail.smtp.auth", SMTP_AUTH); 
      session = Session.getDefaultInstance(props, auth); 
     } else { 
      session = Session.getDefaultInstance(props, null);   
     } 

     // get the default session 
     session.setDebug(true); 

     // create message 
     Message msg = new javax.mail.internet.MimeMessage(session); 

     // set from and to address 
     try { 
      msg.setFrom(new InternetAddress(from, from)); 
      msg.setReplyTo(new InternetAddress[]{new InternetAddress(from,from)}); 
     } catch (Exception e) { 
      msg.setFrom(new InternetAddress(from)); 
      msg.setReplyTo(new InternetAddress[]{new InternetAddress(from)}); 
     } 

     // set send date 
     msg.setSentDate(Calendar.getInstance().getTime()); 

     // parse the recipients TO address 
     java.util.StringTokenizer st = new java.util.StringTokenizer(toList, ","); 
     int numberOfRecipients = st.countTokens(); 

     javax.mail.internet.InternetAddress[] addressTo = new javax.mail.internet.InternetAddress[numberOfRecipients]; 

     int i = 0; 
     while (st.hasMoreTokens()) { 
      addressTo[i++] = new javax.mail.internet.InternetAddress(st 
        .nextToken()); 
     } 
     msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo); 

     // parse the replyTo addresses 
     if (replyToList != null && !"".equals(replyToList)) { 
      st = new java.util.StringTokenizer(replyToList, ","); 
      int numberOfReplyTos = st.countTokens(); 
      javax.mail.internet.InternetAddress[] addressReplyTo = new javax.mail.internet.InternetAddress[numberOfReplyTos]; 
      i = 0; 
      while (st.hasMoreTokens()) { 
       addressReplyTo[i++] = new javax.mail.internet.InternetAddress(
         st.nextToken()); 
      } 
      msg.setReplyTo(addressReplyTo); 
     } 

     // parse the recipients CC address 
     if (ccList != null && !"".equals(ccList)) { 
      st = new java.util.StringTokenizer(ccList, ","); 
      int numberOfCCRecipients = st.countTokens(); 

      javax.mail.internet.InternetAddress[] addressCC = new javax.mail.internet.InternetAddress[numberOfCCRecipients]; 

      i = 0; 
      while (st.hasMoreTokens()) { 
       addressCC[i++] = new javax.mail.internet.InternetAddress(st 
         .nextToken()); 
      } 

      msg.setRecipients(javax.mail.Message.RecipientType.CC, addressCC); 
     } 

     // parse the recipients BCC address 
     if (bccList != null && !"".equals(bccList)) { 
      st = new java.util.StringTokenizer(bccList, ","); 
      int numberOfBCCRecipients = st.countTokens(); 

      javax.mail.internet.InternetAddress[] addressBCC = new javax.mail.internet.InternetAddress[numberOfBCCRecipients]; 

      i = 0; 
      while (st.hasMoreTokens()) { 
       addressBCC[i++] = new javax.mail.internet.InternetAddress(st 
         .nextToken()); 
      } 

      msg.setRecipients(javax.mail.Message.RecipientType.BCC, addressBCC); 
     } 

     // set header 
     msg.addHeader("X-Mailer", "MyMailer"); 
     msg.addHeader("Precedence", "bulk"); 
     // setting the subject and content type 
     msg.setSubject(subject); 

     Multipart mp = new MimeMultipart("related"); 

     // set body message 
     MimeBodyPart bodyMsg = new MimeBodyPart(); 
     bodyMsg.setText(txtBody, "iso-8859-1"); *THIS IS THE ERROR LINE* 
     bodyMsg.setContent(htmlBody, "text/html"); 
     mp.addBodyPart(bodyMsg); 
     msg.setContent(mp); 

     // send it 
     try { 
      javax.mail.Transport.send(msg); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    /** 
    * SimpleAuthenticator is used to do simple authentication when the SMTP 
    * server requires it. 
    */ 
    private static class SMTPAuthenticator extends javax.mail.Authenticator { 

     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 

      String username = SMTP_USERNAME; 
      String password = SMTP_PASSWORD; 

      return new PasswordAuthentication(username, password); 
     } 
    } 

    public String getToList() { 
     return toList; 
    } 

    public void setToList(String toList) { 
     this.toList = toList; 
    } 

    public String getCcList() { 
     return ccList; 
    } 

    public void setCcList(String ccList) { 
     this.ccList = ccList; 
    } 

    public String getBccList() { 
     return bccList; 
    } 

    public void setBccList(String bccList) { 
     this.bccList = bccList; 
    } 

    public String getSubject() { 
     return subject; 
    } 

    public void setSubject(String subject) { 
     this.subject = subject; 
    } 

    public void setFrom(String from) { 
     this.from = from; 
    } 

    public void setTxtBody(String body) { 
     this.txtBody = body; 
    } 

    public void setHtmlBody(String body) { 
     this.htmlBody = body; 
    } 

    public String getReplyToList() { 
     return replyToList; 
    } 

    public void setReplyToList(String replyToList) { 
     this.replyToList = replyToList; 
    } 

    public boolean isAuthenticationRequired() { 
     return authenticationRequired; 
    } 

    public void setAuthenticationRequired(boolean authenticationRequired) { 
     this.authenticationRequired = authenticationRequired; 
    } 

} 

我下載了JavaMail http://www.oracle.com/technetwork/java/javamail/index-138643.html 並於activation.jar:http://www.oracle.com/technetwork/java/jaf11-139815.html#download

添加了的mail.jar和activation.jar到lib文件夾。

但是,一旦我在活動中調用如下

MailService mailer = new MailService("[email protected]","[email protected]","Subject","TextBody", "<b>HtmlBody</b>"); 
      try { 
       mailer.sendAuthenticated(); 
      } catch (Exception e) { 
       Log.e("MAIL", "Failed sending email.", e); 
      } 

我收到以下錯誤:

03-25 09:18:37.999: E/dalvikvm(12922): Could not find class 'javax.activation.DataHandler', referenced from method javax.mail.internet.MimeBodyPart.updateHeaders 
03-25 09:18:37.999: W/dalvikvm(12922): VFY: unable to resolve new-instance 1138 (Ljavax/activation/DataHandler;) in Ljavax/mail/internet/MimeBodyPart; 
03-25 09:18:37.999: D/AndroidRuntime(12922): Shutting down VM 
03-25 09:18:37.999: W/dalvikvm(12922): threadid=1: thread exiting with uncaught exception (group=0x40018578) 
03-25 09:18:38.009: E/AndroidRuntime(12922): FATAL EXCEPTION: main 
03-25 09:18:38.009: E/AndroidRuntime(12922): java.lang.NoClassDefFoundError: javax.activation.DataHandler 

我嘗試添加的jar文件到構建路徑,但仍然是相同的。任何人有任何建議?

+2

看看這個線程http://stackoverflow.com/questions/6368704/java-lang-noclassdeffounderror-javax-activation-datahandler-in-android – Alex 2013-03-25 08:26:30

+0

確實亞歷克斯,我不得不添加這3個Jar文件,它的工作原理: ) 謝謝! – Diego 2013-03-27 18:25:17

回答

1

澄清 - activation.jar依賴於核心Java rt.jar庫中的類,這些類在Android運行時不提供,因此失敗。

javamail-android庫已經由第三方開發者被親切地放在一起,也不必這種依賴性提供所需的JavaMail功能。他們現在可能有點老(2009),但是他們對大多數功能都適用。

相關問題