1

I have the following code (snippet) in my 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";

    }

In development this works fine but after I upload the WAR to the managed host and run the same thing the variable exceptionString is populated with:

pointOfFailure: 8 toString: javax.mail.AuthenticationFailedException

I had some previous problems being unable to connect to my SQL Server database and after a lengthy interaction the host managers unblocked a particular port but they are not saying this is the problem in this case, so far.

Does anyone have any ideas what the difference could be between dev and production that might cause this and what kind of debug code I could add to gather some more useful information?

Thanks.

hardya
  • 51
  • 8
  • Not sure.. see if solution mentioned in this post helps you. http://stackoverflow.com/questions/18778240/solve-error-javax-mail-authenticationfailedexception – mhasan Oct 19 '16 at 16:28
  • You're using the same user and password in both cases, right? What does the [JavaMail debug output](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug) show? – Bill Shannon Oct 19 '16 at 18:06
  • Currently the username and password are in the java code, so identical in both locations. Debug is good idea thank you, I will try to turn on debug on the session. Thing is I'm using hosting managed by a hosting organisation. I just upload the war file. I'm not really sure where console output would be directed to and be accessible. – hardya Oct 19 '16 at 23:00
  • OK. I partly got an answer from the host support, but not quite. Finally used just the right search string and find this more detailed and accurate solution on stack overflow below:. It is some what infuriating that one can as far as I can see use the smtp protocol perfectly correctly but have the connection refused. I am almost certainly wrong but I do wonder if this in some way contravenes the protocol. http://stackoverflow.com/questions/17968716/why-emails-are-not-sending-on-production-server. – hardya Oct 19 '16 at 23:19
  • I do see posts on here with answers that are virtually the same answer as some other similar post's answer. I'm assuming that's not right to do and that I should just do what I've done above and include a link to an existing answer. – hardya Oct 20 '16 at 06:45

0 Answers0