I wrote an incredibly simple component that I'm using on an internal website. The google-code project is here:

http://code.google.com/p/israfil-mail-service/

The usage is documented in the javadoc comments. I'll put up some better usage docs later. It's BSD licensed so it's unrestricted.

I use this in my maven pom (or you can download the jars from 
http://repo1.maven.org/maven2/net/israfil/services/mail/)

                <dependency>
                        <groupId>net.israfil.services.mail</groupId>
<artifactId>israfil-mail-service-javamail</ artifactId>
                        <version>0.6</version>
                </dependency>
                <dependency>
                        <groupId>net.israfil.services.mail</groupId>
<artifactId>israfil-mail-service-api</ artifactId>
                        <version>0.6</version>
                </dependency>

and then I add this to my AppModule (to get a javax.mail session from jetty - you have to declare it in jetty.xml, but there's a default one there you can modify.)

    @EagerLoad
public static javax.mail.Session buildJavaMailSession() throws NamingException {
        InitialContext cxt = new InitialContext();
javax.mail.Session session = (javax.mail.Session) cxt.lookup( "java:comp/env/mail/main/session" );
        if ( session == null ) {
           throw new NamingException("javax.mail.Session not found!");
        }
        return session;
    }

Since the module I include in the javamail service implementation has metadata for tapestry-ioc autoloading then if its in the classpath it's loaded. If you're using this all from eclipse or another IDE, you have to lauch with - Dtapestry .modules =net.israfil.service.mail.transport.javamail.JavaMailTransportModule. From here you can just inject the service like this in any component or service in Tapestry:

        @Inject
        private MailService mailService;

Then you just use the service. Here's a simple example I use for internal notification.

private MailMessage prepareInternalNotificationMail(Registration registration,String address) {
                SMTPMessage message = new SMTPMessage();
message.setFromAddress(new SMTPAddress("Registration System","train...@israfil.net")); message.addAddress(SMTPHeaderTypes.To,new SMTPAddress(address)); message.setSubject(String.format("Registration for %s %s (%s: %s)",
                        registration.getFirstName(),
                        registration.getLastName(),
                        registration.getEvent().getCourse().getCode(),
                        registration.getEvent().getStartDate()
                ));
                // Here I set up a string to add to the body.
                String body = String.format(
"%s %s has regisered for an %s class on %s\n\n %s",
                        registration.getFirstName(),
                        registration.getLastName(),
                        registration.getEvent().getCourse().getCode(),
                        registration.getEvent().getStartDate(),
                        registration.getPlainText()
                );
                message.setBody(new PlaintextMessageBody(body));
                return message;
        }

which I call from:

    ...
mailService.send(prepareInternalNotificationMail(registration, registrationNotificationEmail);
    ...

It's pretty much as simple as that. The annoying part is the setup, but this is the same for nearly any service/component you want to stage with Dependency Injection.

If you bother using it, and have any questions, shoot me an e-mail. There's no compelling advantage of mine over others necessarily, except that it's small, doesn't have a lot of bloat, and the API allows for alternate mail transport back-ends, though none are implemented at the moment.

cheers,
Christian.


On 17-Jun-09, at 23:01 , newtonik wrote:


Can anyone give me an example on how to use this
http://www.chenillekit.org/chenillekit-mail/SmtpService.html mailer . I am new to Tapestry and I have been trying some of their components. I decided to try the mailer today but I don't seem to know how to get it to work. I
don't seem to know how to Inject the Service into my pages.

Can someone point me in the right direction?

How about what people use to mail? Does everyone basically use the Apache
Commons EMail?
--
View this message in context: 
http://www.nabble.com/ChenilleKit-Mail----How-to-Use--or-Any-Mailer--tp24085676p24085676.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org


Christian Edward Gruber
e-mail: christianedwardgru...@gmail.com
weblog: http://www.geekinasuit.com/


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to