生产中的javax.mail.AuthenticationFailedException,但不是Dev

时间:2016-10-19 15:58:42

标签: java spring-mvc smtp javamail

我的spring MVC Web Application中有以下代码(片段):

    private String sendEmail(DataElements.OutgoingEmailOperation email)
    {

        email.success = false;

        // Recipient's email ID needs to be mentioned.
        //String to = "andrew_hardy@sky.com";
        String to = email.emailAddress;

        // Sender's email ID needs to be mentioned
        String from = "therotasystem@gmail.com";

        // Assuming you are sending email from localhost
        String host = "smtp.gmail.com";

        // Get system properties
        Properties properties = System.getProperties();

        // Setup mail server
        //properties.setProperty("mail.smtp.host", host);

        properties.setProperty("mail.smtp.starttls.enable", "true");
        //properties.setProperty("mail.smtp.ssl.trust", host);
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.auth", "true");

        properties.setProperty("mail.smtp.port", "587");
        properties.setProperty("mail.smtp.user", "<user>");
        properties.setProperty("mail.smtp.password", "<password>");


        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties);

        String pointOfFailure = "0";
        try
        {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);
            pointOfFailure = "1";

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));
            pointOfFailure = "2";

            // Set To: header field of the header.
            message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
            pointOfFailure = "3";

            // Set Subject: header field
            //message.setSubject("This is the Subject Line!");
            //message.setSubject(theSubject);
            message.setSubject(email.subject);
            pointOfFailure = "4";

            // Now set the actual message
            //message.setText("This is actual message");
            //message.setText(theMessage);
            message.setText(email.body);
            pointOfFailure = "5";


            // An SmtpListener object is called back to on the main thread
            // out of an event queue.  There can sometimes be multiple cache objects
            // for example spring MVC will have one per http session, so the SmtpListener 
            // needs to know which cache
            SmtpListener smtpListener = new SmtpListener(email.ID,
                    email.latestDue.getTime(),
                    email.until.getTime(),
                    email.timeOfSending.getTime(),
                    email.status,
                    RotaCache.getCache());
            pointOfFailure = "6";

            // Send message
            Transport tr = session.getTransport("smtp");
            pointOfFailure = "7";
            tr.addTransportListener(smtpListener);
            pointOfFailure = "8";
            tr.connect(host, "<username>", "<password>");
            pointOfFailure = "9";
            message.saveChanges();
            pointOfFailure = "10";

            // asynchronous?  So callback when smtp server responds?  And this returns immediately?
            tr.sendMessage(message, message.getAllRecipients());
            pointOfFailure = "11";

            tr.close();
            pointOfFailure = "12";


            //Transport.send(message);
            //System.out.println("Sent message successfully....");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            String exceptionString;
            exceptionString = " toString: " + e.toString();
            if (e.getMessage() != null)
                exceptionString = " getMessage: " + e.getMessage();

            exceptionString = "pointOfFailure: " + pointOfFailure + exceptionString;
            outgoingEmail.error_message = exceptionString;
            return exceptionString;

        }
        catch(Error e)
        {
            System.out.println("#################: " + e.toString());

            String exceptionString;
            exceptionString = " toString: " + e.toString();
            if (e.getMessage() != null)
                exceptionString = " getMessage: " + e.getMessage();

            exceptionString = "pointOfFailure: " + pointOfFailure + exceptionString;
            outgoingEmail.error_message = exceptionString;
            return exceptionString;
        }
        finally
        {
        }

        email.success = true;

        return "SUCCESS";

    }

在开发中,这工作正常但在我将WAR上传到托管主机并运行相同的东西之后,变量exceptionString被填充:

pointOfFailure: 8 toString: javax.mail.AuthenticationFailedException

我之前遇到的一些问题是无法连接到我的SQL Server数据库,经过长时间的交互后,主机管理员解锁了一个特定的端口,但到目前为止他们并没有说这是问题所在。

有没有人有任何想法可能会导致开发和生产之间的差异,以及我可以添加什么样的调试代码来收集更多有用的信息?

感谢。

0 个答案:

没有答案