1

我正在使用一个简单的代码以编程方式发送邮件

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

    public static void main(String [] args){
        String recipients[]={"abhijit@gmail.com"};
        String subject = "Report";
        String message = " Hi All.";
        String from ="abhijit@gmail.com";
        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依赖项。

现在,每当我尝试执行代码时,它都会给我异常消息说

13/07/20 02:19:24 INFO amazonaws.request: Sending Request: POST https://email.us-east-1.amazonaws.com / Parameters: (Action: SendRawEmail, AWSAccessKeyId=abhijit@gmail.com,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)

现在,如果我在删除 amazon-aws 依赖项后运行相同的代码,则一切正常。

谁能建议如何AWSJavaMailTransport.sendEmail调用而不是 Transport.send方法

任何帮助将不胜感激

4

1 回答 1

1

如果您不使用亚马逊邮件传输 - 只需删除 aws-java-sdk-XXXjar/META-INF/javamail.providers 并将其安装到您的本地存储库(最好将库版本重命名为另一个版本,例如 1.5.3a)或者你的 maven repo(Nexus 等),如果你有的话。

pom 示例版本 1.5.3 http://db.tt/cI9f2DFS jar 示例版本 1.5.3 http://db.tt/lgwJYJYr

于 2013-08-10T07:54:19.287 回答