Aspiring Independant (a.k.a. not employed to do anything I should mention here) Massachusetts born, Yorkshire based builderGeek.

Report RSS JNDI (tomcat style) for GoogleMail / GMail

Posted by on

This took me over 2 hours to get working right - so I'm posting my results

<Resource name="mail/Session"
	auth="Container"
	type="javax.mail.Session"
	mail.smtp.host="smtp.gmail.com"
	mail.smtp.port="465"
	mail.smtp.auth="true"
	mail.smtp.user="website's name@gmail.com"
	password="the password"
	mail.smtp.starttls.enable="true"
	mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory" />

It can be used like this:

final InitialContext initialContext = new InitialContext();
final Session session = (Session)(initialContext.lookup("java:comp/env").lookup("mail/Session"));

final MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setSubject("Testing javamail plain");
mimeMessage.setContent("This is a one true tag", "text/plain");
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("user@email.host"));

final Transport transport = session.getTransport();
transport.connect();
transport.sendMessage(mimeMessage, mimeMessage.getRecipients(Message.RecipientType.TO));
transport.close();
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: