2013-07-19 86 views
1

我用一個簡單的代碼來發送郵件衝突的編程的javax郵件與亞馬遜SDK

private String HOST_NAME = "gmail-smtp.l.google.com"; 
    String messageBody; 

    public static void main(String [] args){ 
     String recipients[]={"[email protected]"}; 
     String subject = "Report"; 
     String message = " Hi All."; 
     String from ="[email protected]"; 
     String emailPassword = "******"; 
     String [] files = {"/home/ubuntu/RportSystem.txt"}; 
     try { 
      new MailServerTest().postMail(recipients, subject, message, from, emailPassword, files); 
     } catch (MessagingException e) { 
      e.printStackTrace(); 
     } 
    } 


    public void postMail(String recipients[], String subject, String message, 
         String from, String emailPassword, String[] files) throws MessagingException { 
     boolean debug = false; 

     Properties props = new Properties(); 
     props.put("mail.transport.protocol", "smtp"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", HOST_NAME); 
     props.put("mail.smtp.auth", "true"); 

     Authenticator authenticator = new CustomAuthenticator (from, emailPassword); 
     Session session = Session.getDefaultInstance(props, authenticator); 

     session.setDebug(debug); 

     Message msg = new MimeMessage(session); 

     InternetAddress addressFrom = new InternetAddress(from); 
     msg.setFrom(addressFrom); 

     InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
     for (int i = 0; i < recipients.length; i++) { 
      addressTo[i] = new InternetAddress(recipients[i]); 
     } 
     msg.setRecipients(Message.RecipientType.TO, addressTo); 

     msg.setSubject(subject); 
     msg.setContent(message, "text/plain"); 

     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText(message); 

     Multipart multipart = new MimeMultipart(); 

     multipart.addBodyPart(messageBodyPart); 

     addAtachments(files, multipart); 
     msg.setContent(multipart); 
     Transport.send(msg); 
     System.out.println("Sucessfully Sent mail to All Users"); 
    } 

    protected void addAtachments(String[] attachments, Multipart multipart) 
      throws MessagingException, AddressException { 
     for (int i = 0; i <= attachments.length - 1; i++) { 
      String filename = attachments[i]; 
      MimeBodyPart attachmentBodyPart = new MimeBodyPart(); 

      DataSource source = new FileDataSource(filename); 
      attachmentBodyPart.setDataHandler(new DataHandler(source)); 
      attachmentBodyPart.setFileName(filename); 

      multipart.addBodyPart(attachmentBodyPart); 
     } 
    } 

    private class CustomAuthenticator extends javax.mail.Authenticator { 

     String username; 
     String password; 

     private CustomAuthenticator (String authenticationUser, String authenticationPassword) { 
      username = authenticationUser; 
      password = authenticationPassword; 
     } 

     @Override 
     public javax.mail.PasswordAuthentication getPasswordAuthentication() { 

      return new javax.mail.PasswordAuthentication(username, password); 
     } 
    } 

我的項目也有AWS-Java的SDK依賴。

現在每當我試圖執行它給了我excveption消息說

13/07/20 02:19:24 INFO amazonaws.request: Sending Request: POST https://email.us-east-1.amazonaws.com/Parameters: (Action: SendRawEmail, [email protected],Algorithm=HmacSHA256,Signature=Y4sqXX2bw7N0uiPoCVsZavgnbTR1j30NaIryVBbbn9I=, x-amz-nonce: a335527c-e25a-48f8-8ec1-67348173cc68, Date: Fri, 19 Jul 2013 20:49:23 GMT,) 
13/07/20 02:19:31 INFO amazonaws.request: Received error response: Status Code: 403, AWS Request ID: b2064183-f0b4-11e2-9355-21325bfa2b4f, AWS Error Code: InvalidClientTokenId, AWS Error Message: The security token included in the request is invalid 
javax.mail.SendFailedException: Unable to send email; 
    nested exception is: 
    Status Code: 403, AWS Request ID: b2064183-f0b4-11e2-9355-21325bfa2b4f, AWS Error Code: InvalidClientTokenId, AWS Error Message: The security token included in the request is invalid 
    at com.amazonaws.services.simpleemail.AWSJavaMailTransport.sendEmail(AWSJavaMailTransport.java:276) 
    at com.amazonaws.services.simpleemail.AWSJavaMailTransport.sendMessage(AWSJavaMailTransport.java:95) 
    at javax.mail.Transport.send0(Transport.java:195) 
    at javax.mail.Transport.send(Transport.java:124) 

現在,如果相同的代碼移除亞馬遜AWS的依賴後,我跑了,事情是做工精細的代碼。

任何人都可以建議得到如何AWSJavaMailTransport.sendEmail調用,而不是Transport.send方法

任何幫助將如果你沒有使用亞馬遜的郵件傳輸理解

回答

1

- 只需刪除AWS-Java的SDK-XXXjar/META -INF/javamail.providers並將它安裝到你的本地倉庫(如果你有的話,可以把它更名爲另一個,例如1.5.3a)或你的Maven倉庫(Nexus等)。

POM例如版本1.5.3 http://db.tt/cI9f2DFS 罐子例如版本1.5.3 http://db.tt/lgwJYJYr