PDA

View Full Version : Tomcat - Configuring an Email Resource



sonavor
02-25-2009, 09:45 AM
I have a business account on Westhost that uses the Westhost installed Tomcat 5.5 server.

The server is working and I can connect to MySql and can run a J2EE Struts application. However, I have hit the wall in trying to send an email from my web application that runs on the Tomcat server.

According to Apache Tomcat I should just need to make an entry in the Tomcat server.xml file to define a mail resource (similar to a data source resource).

After that I need an entry in my web application's web.xml file to define a resource reference that points to the Tomcat configured mail resource.

Finally, in the Java code I can just ask for a context using the resource name to get a mail session and send an email.

So far it looks like I am successfully getting a mail session and composing an email. However, the Westhost server is not letting me send an email. I am getting this error -

class javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class javax.mail.SendFailedException: 550 5.7.1 <myemail@mydomain.com>... Relaying denied

Note that I just substituted in this post the value of myemail@mydomain.com in place of the actual email....which is a known, valid email. The 550 error saying Relaying denied appears to mean I don't have permission to send out an email from my Westhost Tomcat. That must mean I don't have Tomcat configured correctly.

So far the Westhost support team is claiming that is a "custom" script problem that they don't support. I contend that it is not custom. It is just a standard Tomcat configuration issue.

Has anyone configured Tomcat on Westhost to send email? If so can you provide some instructions or an example?

Thanks.

sonavor
02-25-2009, 02:29 PM
I was able to get my Tomcat 5.5 server on Westhost configured to send emails.

One issue I had was that I needed to place my Tomcat mail resource definition in the tomcat5/conf/context.xml file instead of the server.xml file. The big issue thought was my not realizing what the true email account user name was. When configuring an email client for Westhost the email account user name is shown as <your email account name>@<your domain>.com. So I was using that whole string as my main.user value in the configuration file. I should have just been using the <your email account name> without the "@yourdomain.com.

I also found that you need to make the Tomcat mail resource entries manually. Using the Tomcat administration screen allows you to create the mail resource but it doesn't provide enough attributes. Here is what my Tomcat mail resource configuration file ended up looking like -

<Resource
name="mail/Session"
type="javax.mail.Session"
auth="Container"
mail.smtp.host="mydomain IP address"
mail.smtp.auth="true"
username="my email account username"
password="the email account password"
mail.user="my email account username"
mail.password="the email account password"
mail.port="25"
mail.transport.protocol="smtp"
mail.debug="true"/>

You should be able to use your domain.com or the domain IP address in the mail.smtp.host property value.

The next entry you have to have is in the web application that will be running on Tomcat and using the mail server. This entry goes in the web.xml file -

<resource-ref>
<description>Resource reference to a factory for javax.mail.Session
instances that may be used for sending electronic mail
messages, preconfigured to connect to the appropriate
SMTP server.</description>
<res-ref-name>mail/Session</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Note that the resource and resource-ref names ( mail/Session ) must match each other.

Finally, in the Java code of your web application where the email is to be used (in my case a Struts action class) you get the mail server as follows -

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
Session session = (Session) envCtx.lookup("mail/Session");

Note that the lookup uses the resource name. Also note that the Session class here is a javax.mail.Session class.

Once you have the mail session you can create a MimeMessage -

Message message = new MimeMessage(session);

then setup the message. There are plenty of examples out on the web of using the javax.mail libraries.