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.