Quantcast

Sending an email with Camel

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Sending an email with Camel

bgoetzmann
Hi,

Camel is a very interesting project ! Thank you for all developers of this project.

Trying a first Java application I would like to send emails using a route ; so I try something like

... .to("smtp://ausernam@ahost")

but is ausernam@ahost must be a address mail at witch you send the mail? How can I specify the smtp server?

I appreciate an example.

Thank you!

Bertrand.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending an email with Camel

Frank Lynch-2
Hi Bertrand,
I believe "ausernam@ahost" is the address to which the email will be
delivered.
You can set the SMTP server via a properties object eg:

        Properties properties = new Properties();
        properties.put("mail.smtp.host", "localhost");
        mailSession = Session.getInstance(properties, null);

This code fragment was taken from MailMessageTest.java in:
 /components/camel-mail/src/test/java/org/apache/camel/component/mail/
HTH,
--Frank



On Fri, 2007-09-14 at 08:32 -0700, bgoetzmann wrote:

> Hi,
>
> Camel is a very interesting project ! Thank you for all developers of this
> project.
>
> Trying a first Java application I would like to send emails using a route ;
> so I try something like
>
> ... .to("smtp://ausernam@ahost")
>
> but is ausernam@ahost must be a address mail at witch you send the mail? How
> can I specify the smtp server?
>
> I appreciate an example.
>
> Thank you!
>
> Bertrand.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending an email with Camel

bgoetzmann
Thank you.
Yes I saw this file ; but how define this property and others (the "from" or "suject") using Java DSL?

Here my test class:


public class Test {

        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
                CamelContext context = new DefaultCamelContext();

                context.addRoutes(new RouteBuilder() {

                    public void configure() {
                        from("timer:myTimerEvent?fixedRate=true&delay=0&period=1000")
                                  // this works!
                        //.setBody(Builder.constant("Salut !")).to("log:test");

                                  // How define smtp host?
                        .setBody(Builder.constant("Salut !")).to("smtp://bgoetzmann@sophia.symag.com");
                       
                    }
                   
                });
               
                context.start();
               
                Thread.sleep(5000);

        }

}


Frank Lynch-2 wrote
Hi Bertrand,
I believe "ausernam@ahost" is the address to which the email will be
delivered.
You can set the SMTP server via a properties object eg:

        Properties properties = new Properties();
        properties.put("mail.smtp.host", "localhost");
        mailSession = Session.getInstance(properties, null);

This code fragment was taken from MailMessageTest.java in:
 /components/camel-mail/src/test/java/org/apache/camel/component/mail/
HTH,
--Frank



On Fri, 2007-09-14 at 08:32 -0700, bgoetzmann wrote:
> Hi,
>
> Camel is a very interesting project ! Thank you for all developers of this
> project.
>
> Trying a first Java application I would like to send emails using a route ;
> so I try something like
>
> ... .to("smtp://ausernam@ahost")
>
> but is ausernam@ahost must be a address mail at witch you send the mail? How
> can I specify the smtp server?
>
> I appreciate an example.
>
> Thank you!
>
> Bertrand.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending an email with Camel

jstrachan
So the idea is that things like host names of your mail server or JMS
server can be configured on a Component, then the endpoints don't have
to have all that configuration in them. e.g. a mail endpoint can be
just the email address; or a JMS endpoint can be just the queue/topic
name etc.

http://activemq.apache.org/camel/endpoint.html
http://activemq.apache.org/camel/component.html


So the idea is you can configure the component with things like JMS
ConnectionFactory, or JPA EntityManagerFactory or whatnot; then just
use the endpoints as nice and simple URIs. So in the case of the mail
component you can configure the MailComponent with all the various
connection details via the MailConfiguration POJO which has a bunch of
setters for things like the host/port of your mail server etc.

so if you are using spring you could do something like

<bean id="mail" class="org.apache.camel.component.mail.MailComponent">
  <property name="configuration">
   <bean class="org.apache.camel.component.mail.MailConfiguration">
     <property name="host" value="myMailServerHost">
...


or if you are using Java code and not using spring you could add a
configured MailComponent into JNDI


MailComponent component = new MailComponent();
component.getConfiguration().setHost("myMailServerHost");
context.put("mail", component);


On 9/14/07, bgoetzmann <[hidden email]> wrote:

>
> Thank you.
> Yes I saw this file ; but how define this property and others (the "from" or
> "suject") using Java DSL?
>
> Here my test class:
>
>
> public class Test {
>
>         /**
>          * @param args
>          * @throws Exception
>          */
>         public static void main(String[] args) throws Exception {
>                 CamelContext context = new DefaultCamelContext();
>
>                 context.addRoutes(new RouteBuilder() {
>
>                     public void configure() {
>                         from("timer:myTimerEvent?fixedRate=true&delay=0&period=1000")
>                                   // this works!
>                         //.setBody(Builder.constant("Salut !")).to("log:test");
>
>                                   // How define smtp host?
>                         .setBody(Builder.constant("Salut
> !")).to("smtp://[hidden email]");
>
>                     }
>
>                 });
>
>                 context.start();
>
>                 Thread.sleep(5000);
>
>         }
>
> }
>
>
>
> Frank Lynch-2 wrote:
> >
> > Hi Bertrand,
> > I believe "ausernam@ahost" is the address to which the email will be
> > delivered.
> > You can set the SMTP server via a properties object eg:
> >
> >         Properties properties = new Properties();
> >         properties.put("mail.smtp.host", "localhost");
> >         mailSession = Session.getInstance(properties, null);
> >
> > This code fragment was taken from MailMessageTest.java in:
> >  /components/camel-mail/src/test/java/org/apache/camel/component/mail/
> > HTH,
> > --Frank
> >
> >
> >
> > On Fri, 2007-09-14 at 08:32 -0700, bgoetzmann wrote:
> >> Hi,
> >>
> >> Camel is a very interesting project ! Thank you for all developers of
> >> this
> >> project.
> >>
> >> Trying a first Java application I would like to send emails using a route
> >> ;
> >> so I try something like
> >>
> >> ... .to("smtp://ausernam@ahost")
> >>
> >> but is ausernam@ahost must be a address mail at witch you send the mail?
> >> How
> >> can I specify the smtp server?
> >>
> >> I appreciate an example.
> >>
> >> Thank you!
> >>
> >> Bertrand.
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Sending-an-email-with-Camel-tf4443341s22882.html#a12678558
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>


--
James
-------
http://macstrac.blogspot.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending an email with Camel

bgoetzmann
Thank you James.

Using the Java code you indicated:

MailComponent component = new MailComponent();
component.getConfiguration().setHost("myMailServerHost");
...

Does the instance must be put in a Camel context? Like this:
camelContext.put("mail", component);

Sorry, I don't see the relation with JNDI, and the URI I can use like in my sample code.
Must I use something like ... .to(smtp://...) or ... .to(mail: ...)?

Cheers,

Bertrand.


James.Strachan wrote
So the idea is that things like host names of your mail server or JMS
server can be configured on a Component, then the endpoints don't have
to have all that configuration in them. e.g. a mail endpoint can be
just the email address; or a JMS endpoint can be just the queue/topic
name etc.

http://activemq.apache.org/camel/endpoint.html
http://activemq.apache.org/camel/component.html


So the idea is you can configure the component with things like JMS
ConnectionFactory, or JPA EntityManagerFactory or whatnot; then just
use the endpoints as nice and simple URIs. So in the case of the mail
component you can configure the MailComponent with all the various
connection details via the MailConfiguration POJO which has a bunch of
setters for things like the host/port of your mail server etc.

so if you are using spring you could do something like

<bean id="mail" class="org.apache.camel.component.mail.MailComponent">
  <property name="configuration">
   <bean class="org.apache.camel.component.mail.MailConfiguration">
     <property name="host" value="myMailServerHost">
...


or if you are using Java code and not using spring you could add a
configured MailComponent into JNDI


MailComponent component = new MailComponent();
component.getConfiguration().setHost("myMailServerHost");
context.put("mail", component);


On 9/14/07, bgoetzmann <bgoetzmann@sophia.symag.com> wrote:
>
> Thank you.
> Yes I saw this file ; but how define this property and others (the "from" or
> "suject") using Java DSL?
>
> Here my test class:
>
>
> public class Test {
>
>         /**
>          * @param args
>          * @throws Exception
>          */
>         public static void main(String[] args) throws Exception {
>                 CamelContext context = new DefaultCamelContext();
>
>                 context.addRoutes(new RouteBuilder() {
>
>                     public void configure() {
>                         from("timer:myTimerEvent?fixedRate=true&delay=0&period=1000")
>                                   // this works!
>                         //.setBody(Builder.constant("Salut !")).to("log:test");
>
>                                   // How define smtp host?
>                         .setBody(Builder.constant("Salut
> !")).to("smtp://bgoetzmann@sophia.symag.com");
>
>                     }
>
>                 });
>
>                 context.start();
>
>                 Thread.sleep(5000);
>
>         }
>
> }
>
>
>
> Frank Lynch-2 wrote:
> >
> > Hi Bertrand,
> > I believe "ausernam@ahost" is the address to which the email will be
> > delivered.
> > You can set the SMTP server via a properties object eg:
> >
> >         Properties properties = new Properties();
> >         properties.put("mail.smtp.host", "localhost");
> >         mailSession = Session.getInstance(properties, null);
> >
> > This code fragment was taken from MailMessageTest.java in:
> >  /components/camel-mail/src/test/java/org/apache/camel/component/mail/
> > HTH,
> > --Frank
> >
> >
> >
> > On Fri, 2007-09-14 at 08:32 -0700, bgoetzmann wrote:
> >> Hi,
> >>
> >> Camel is a very interesting project ! Thank you for all developers of
> >> this
> >> project.
> >>
> >> Trying a first Java application I would like to send emails using a route
> >> ;
> >> so I try something like
> >>
> >> ... .to("smtp://ausernam@ahost")
> >>
> >> but is ausernam@ahost must be a address mail at witch you send the mail?
> >> How
> >> can I specify the smtp server?
> >>
> >> I appreciate an example.
> >>
> >> Thank you!
> >>
> >> Bertrand.
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Sending-an-email-with-Camel-tf4443341s22882.html#a12678558
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>


--
James
-------
http://macstrac.blogspot.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending an email with Camel

jstrachan
On 9/17/07, bgoetzmann <[hidden email]> wrote:

>
> Thank you James.
>
> Using the Java code you indicated:
>
> MailComponent component = new MailComponent();
> component.getConfiguration().setHost("myMailServerHost");
> ...
>
> Does the instance must be put in a Camel context? Like this:
> camelContext.put("mail", component);

Yes, or...

> Sorry, I don't see the relation with JNDI, and the URI I can use like in my
> sample code.
> Must I use something like ... .to(smtp://...) or ... .to(mail: ...)?

So when you use a URI in camel, it tries to find the endpoint in your
registry (which is usually the ApplicationContext if you're a spring
user or JNDI if not). Then if it can't find it, it takes the scheme
(mail, or smtp, pop3 or whatever) and tries to find a Component in the
register for that -if it does it then asks the Component to make the
endpoint.

You can avoid the use of a Registry if you prefer and just put your
components/endpoints into the CamelContext directly.


> > so if you are using spring you could do something like
> >
> > <bean id="mail" class="org.apache.camel.component.mail.MailComponent">
> >   <property name="configuration">
> >    <bean class="org.apache.camel.component.mail.MailConfiguration">
> >      <property name="host" value="myMailServerHost">
> > ...

So in this example the "mail" entry in the ApplicationContext will
point to the configured mail compnent.


> > or if you are using Java code and not using spring you could add a
> > configured MailComponent into JNDI
> >
> >
> > MailComponent component = new MailComponent();
> > component.getConfiguration().setHost("myMailServerHost");
> > context.put("mail", component);

Here we're adding the component into the initial JNDI context so that
the CamelContext can resolve "mail" endpoints.

The use of the registry is optional really; its really just there to
let folks use whatever they are already using (like JNDI in some app
server or Spring etc) to configure things.

--
James
-------
http://macstrac.blogspot.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending an email with Camel

bgoetzmann
Thank you James,

I succeed to send email, using this URI:

smtp://my_username@my_host?password=mypassword#my_email_destination

I've seen that you can use of # to define a destination email by look at code source (otherwise it seems that the email my_username@my_host is used.


Now I try to use my own MailComponent instance with the scheme "mail"; here the code:


JndiContext context = new JndiContext();
               
MailComponent mail = new MailComponent();
context.bind("mail", mail);
               
CamelContext camelContext = new DefaultCamelContext(context);
               
camelContext.addRoutes(new RouteBuilder() {

  public void configure() {
    from("timer:myTimerEvent?fixedRate=true&delay=0&period=1000")
    .setBody(Builder.constant("Salut !")).to("mail://xxx@yyy?password=zzz#ttt@www.com");        
  }
                   
});

When running the Camel context I get the following exception that makes me crasy:

GRAVE: On delivery attempt: 0 caught: org.springframework.mail.MailSendException; nested exceptions (0) are:
org.springframework.mail.MailSendException; nested exceptions (0) are:
Caused by: javax.mail.NoSuchProviderException: No provider for mail
        at javax.mail.Session.getProvider(Session.java:455)
        at javax.mail.Session.getTransport(Session.java:650)
        at javax.mail.Session.getTransport(Session.java:631)
        at org.springframework.mail.javamail.JavaMailSenderImpl.getTransport(JavaMailSenderImpl.java:419)
        at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:378)

Any idea about this exception?

Thank you again!

Bertrand.
http://www.odelia-technologies.com/



James.Strachan wrote
On 9/17/07, bgoetzmann <bgoetzmann@sophia.symag.com> wrote:
>
> Thank you James.
>
> Using the Java code you indicated:
>
> MailComponent component = new MailComponent();
> component.getConfiguration().setHost("myMailServerHost");
> ...
>
> Does the instance must be put in a Camel context? Like this:
> camelContext.put("mail", component);

Yes, or...

> Sorry, I don't see the relation with JNDI, and the URI I can use like in my
> sample code.
> Must I use something like ... .to(smtp://...) or ... .to(mail: ...)?

So when you use a URI in camel, it tries to find the endpoint in your
registry (which is usually the ApplicationContext if you're a spring
user or JNDI if not). Then if it can't find it, it takes the scheme
(mail, or smtp, pop3 or whatever) and tries to find a Component in the
register for that -if it does it then asks the Component to make the
endpoint.

You can avoid the use of a Registry if you prefer and just put your
components/endpoints into the CamelContext directly.


> > so if you are using spring you could do something like
> >
> > <bean id="mail" class="org.apache.camel.component.mail.MailComponent">
> >   <property name="configuration">
> >    <bean class="org.apache.camel.component.mail.MailConfiguration">
> >      <property name="host" value="myMailServerHost">
> > ...

So in this example the "mail" entry in the ApplicationContext will
point to the configured mail compnent.


> > or if you are using Java code and not using spring you could add a
> > configured MailComponent into JNDI
> >
> >
> > MailComponent component = new MailComponent();
> > component.getConfiguration().setHost("myMailServerHost");
> > context.put("mail", component);

Here we're adding the component into the initial JNDI context so that
the CamelContext can resolve "mail" endpoints.

The use of the registry is optional really; its really just there to
let folks use whatever they are already using (like JNDI in some app
server or Spring etc) to configure things.

--
James
-------
http://macstrac.blogspot.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending an email with Camel

jstrachan
On 9/19/07, bgoetzmann <[hidden email]> wrote:

>
> Thank you James,
>
> I succeed to send email, using this URI:
>
> smtp://my_username@my_host?password=mypassword#my_email_destination
>
> I've seen that you can use of # to define a destination email by look at
> code source (otherwise it seems that the email my_username@my_host is used.
>
>
> Now I try to use my own MailComponent instance with the scheme "mail"; here
> the code:
>
>
> JndiContext context = new JndiContext();
>
> MailComponent mail = new MailComponent();
> context.bind("mail", mail);
>
> CamelContext camelContext = new DefaultCamelContext(context);
>
> camelContext.addRoutes(new RouteBuilder() {
>
>   public void configure() {
>     from("timer:myTimerEvent?fixedRate=true&delay=0&period=1000")
>     .setBody(Builder.constant("Salut
> !")).to("mail://xxx@yyy?password=zzz#[hidden email]");
>   }
>
> });

In general that should all work fine; one complication with the Mail
component is that it supports a few different protocols; pop3, smtp
and so forth. So if you use, say, "smpt" instead of "mail" in the
above code (both the URL and the registration in JNDI) it should work
just fine.

e.g.

JndiContext context = new JndiContext();

MailComponent mail = new MailComponent();
context.bind("smtp", mail);

CamelContext camelContext = new DefaultCamelContext(context);

camelContext.addRoutes(new RouteBuilder() {

 public void configure() {
   from("timer:myTimerEvent?fixedRate=true&delay=0&period=1000")
   .setBody(Builder.constant("Salut
!")).to("smtp://xxx@yyy?password=zzz#[hidden email]");
 }

});


BTW the use of JNDI or Spring ApplicationContext is an optional
feature to help you reuse a single registry for configuration; if you
prefer you can just register components and endpoints directly on the
CamelContext via addComponent/addSingletonEndpoint.

--
James
-------
http://macstrac.blogspot.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending an email with Camel

bgoetzmann
Thank you again,

What I try to get is to use my own instance of MailComponent and then using URIs starting by mail://, and also be able to configure some parameters like the smtp protocol and the email destination.
I've seen that I can configure an endpoint and use it in route.
As you said it's not mandatory to use a JNDI context.

Camel is really interesting!

As soon as I get more experiment with Apache Camel, I will put some articles on my web site www.odelia-technologies.com/


Here the code that's good:

                CamelContext camelContext = new DefaultCamelContext();
               
                MailComponent mail = new MailComponent();
                camelContext.addComponent("mail", mail);
               
                final MailEndpoint ep = (MailEndpoint) camelContext.getEndpoint("mail://xxx@yyy?password=zzz");
                ep.getConfiguration().setProtocol("smtp");
                ep.getConfiguration().setDestination("aaa@bbb.com");
               
                camelContext.addRoutes(new RouteBuilder() {

                    public void configure() {
                        from("timer:myTimerEvent?fixedRate=true&delay=0&period=1000")
                        .setBody(Builder.constant("Salut !")).to(ep);        
                    }
                   
                });
               
                camelContext.start();


James.Strachan wrote
On 9/19/07, bgoetzmann <bgoetzmann@sophia.symag.com> wrote:
>
> Thank you James,
>
> I succeed to send email, using this URI:
>
> smtp://my_username@my_host?password=mypassword#my_email_destination
>
> I've seen that you can use of # to define a destination email by look at
> code source (otherwise it seems that the email my_username@my_host is used.
>
>
> Now I try to use my own MailComponent instance with the scheme "mail"; here
> the code:
>
>
> JndiContext context = new JndiContext();
>
> MailComponent mail = new MailComponent();
> context.bind("mail", mail);
>
> CamelContext camelContext = new DefaultCamelContext(context);
>
> camelContext.addRoutes(new RouteBuilder() {
>
>   public void configure() {
>     from("timer:myTimerEvent?fixedRate=true&delay=0&period=1000")
>     .setBody(Builder.constant("Salut
> !")).to("mail://xxx@yyy?password=zzz#ttt@www.com");
>   }
>
> });

In general that should all work fine; one complication with the Mail
component is that it supports a few different protocols; pop3, smtp
and so forth. So if you use, say, "smpt" instead of "mail" in the
above code (both the URL and the registration in JNDI) it should work
just fine.

e.g.

JndiContext context = new JndiContext();

MailComponent mail = new MailComponent();
context.bind("smtp", mail);

CamelContext camelContext = new DefaultCamelContext(context);

camelContext.addRoutes(new RouteBuilder() {

 public void configure() {
   from("timer:myTimerEvent?fixedRate=true&delay=0&period=1000")
   .setBody(Builder.constant("Salut
!")).to("smtp://xxx@yyy?password=zzz#ttt@www.com");
 }

});


BTW the use of JNDI or Spring ApplicationContext is an optional
feature to help you reuse a single registry for configuration; if you
prefer you can just register components and endpoints directly on the
CamelContext via addComponent/addSingletonEndpoint.

--
James
-------
http://macstrac.blogspot.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending an email with Camel

jstrachan
On 9/20/07, bgoetzmann <[hidden email]> wrote:
>
> Thank you again,
>
> What I try to get is to use my own instance of MailComponent and then using
> URIs starting by mail://, and also be able to configure some parameters like
> the smtp protocol and the email destination.

That should be fine; the MailComponent is a rare component that
supports multiple schemes so you might wanna register your configured
MailComponent under multiple schemes ("pop3", "smtp" etc).



> I've seen that I can configure an endpoint and use it in route.
> As you said it's not mandatory to use a JNDI context.
>
> Camel is really interesting!

Thanks! :)


> As soon as I get more experiment with Apache Camel, I will put some articles
> on my web site www.odelia-technologies.com/

Awesome! Let us know when its there and we can add a link to it on the
Camel site.


--
James
-------
http://macstrac.blogspot.com/
Loading...