geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Petersson <pe...@pmb.mine.nu>
Subject Re: Mailing from geronimo
Date Tue, 03 Oct 2006 12:45:29 GMT
My attempt to mail from within a quart job is now working ;)
During correspondance with Rick McGuire that cindly dedicated som of his 
time to help me out, he sugested
moving the transport retriving to the setMailSession method and wrap it 
up within a classloader call and it did the trick ;)
Here is the code snippet:

   public void setMailSession(Session s) {
       mailSession = s;
       ClassLoader old = Thread.currentThread().getContextClassLoader();
       Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); 
       try {            
           tr = mailSession.getTransport("smtp");
       }catch(NoSuchProviderException pex){
           logger.error("setMailSession got a exeption trying to get the smtp transport "+pex.getMessage());
       }
       finally {
           Thread.currentThread().setContextClassLoader(old);
       }    
} 

Cheers
   Peter

Peter Petersson skrev:
> Hi again!
>
> Im just wondering if anyone is having a try on this ? It would be 
> really appreciated as im cind off stuck here.
>
> It would really be nice to get geronimo-mail runing but if it's not 
> gona happen maybe I will have beter luck with  suns JavaMail so Is 
> there a way to replace the mail service to use suns JavaMail ?
> Its vital for me to get any cind of mailing running in Geronimo so all 
> help/ideas is welcome and appreciated.
>
> Event though I set debug lever to ALL i cant trace down whats going 
> wrong with my mailing setup Is it posible to enable even more trace 
> information in Geronimo (mail).
>
> Thanks,
> Peter
>
> Peter Petersson skrev:
>> Here is a attatched simple tar.gz packed sample app with plans that 
>> uses a ant build.xml file to build the jar for you.
>> I have tested it at home and I get the same error as before.
>>
>> Appreciate all help on geting maling runing in it !
>>
>> Setup:
>> 1) Install geronimo 1.1.1 (if you dont allreddy have it)
>> 2) Install the Quartz Scheduler (0.2) and Quartz Job Deployer (0.2) 
>> plugins available from console/plugins.
>> 3) Pack up the tar.gz file and do some initial edits:
>>    QuartzMailTest.java -- fix mail addresses etc.
>>    mail-server.xml -- fix smtp address.
>>    build.xml -- set geronimo home.
>>    If you are using eclipse you can probably just import the proj 
>> after unpacking it.
>> 4) Build with:  ant dist -- and you will have a deploable test app in 
>> the "dist" dir.
>>
>> Deploy the apps and check geronimo.log It will put out somting from 
>> the job every 30 secs.
>>
>> Thanks
>>   Peter
>>
>> Rick McGuire wrote:
>>> Peter Petersson wrote:
>>>> Okey here comes the plans and a code snippet illustrating what im 
>>>> trying to do when i get the
>>>> javax.mail.NoSuchProviderException: Unable to locate provider for 
>>>> protocol: smtp
>>>>       at javax.mail.Session.getProvider(Session.java:227)
>>>>       at javax.mail.Session.getTransport(Session.java:336)
>>>>
>>>> Anny suggestions ?
>>> The NoSuchProviderException usually occurs because the 
>>> javamail-transport jar file isn't in the classpath, but you have the 
>>> correct dependency there, so this should be working.  I'm going to 
>>> have to investigate this a little more.  Any chance you can send me 
>>> your sample app (with any special usage/setup instructions) so I can 
>>> try to sort this out?
>>>
>>> Rick
>>>
>>>
>>>>
>>>> The execute method below hass the intresting mailing part (its a 
>>>> bit messy as I have tryed diffrent approaches
>>>> Code snippet:
>>>> public class UsedAllotmentSendJob implements Job {
>>>>
>>>>    private final static Log logger = 
>>>> LogFactory.getLog(UsedAllotmentSendJob.class);
>>>>    private DataSource dataSource;
>>>>    private Session mailSession;
>>>>
>>>>    public void setDatabase(DataSource ds) {
>>>>        dataSource = ds;
>>>>    }
>>>>
>>>>    public void setMailSession(Session s) {
>>>>        mailSession = s;
>>>>    }        public void execute(JobExecutionContext jobcontext) 
>>>> throws JobExecutionException {
>>>>        //trying with added autentication
>>>>        String smtphost="thednsaddress";
>>>>        String username="thelogginname";
>>>>        String password="thelogginpassword";
>>>>              Message message = new MimeMessage(mailSession);
>>>>        try{
>>>>        InternetAddress from = new InternetAddress("the_sender_addr");
>>>>        InternetAddress to = new InternetAddress("the_resiver_addr");
>>>>
>>>>        String mailer = "smtpsend";
>>>>        message.setHeader("X-Mailer", mailer);
>>>>        message.setSentDate(new Date());
>>>>        message.setFrom(from);
>>>>        message.addRecipient(Message.RecipientType.TO, to);
>>>>        message.setSubject("The subject");
>>>>        message.setText("The message");
>>>>             Transport tr = mailSession.getTransport("smtp");
>>>>        tr.connect(smtphost, username, password);
>>>>        //tr.connect(); //no extra params just the gbean settings 
>>>> (host,port)
>>>>        message.saveChanges(); // don't forget this
>>>>        tr.sendMessage(message, message.getAllRecipients());
>>>>        tr.close();
>>>>        //replaced with the settup above
>>>>        //mailSession.getTransport("smtp");
>>>>        //Transport.send(message);
>>>>
>>>>        }catch(AddressException aex){
>>>>            logger.error("execute got a AddressException 
>>>> "+aex.getMessage());
>>>>        }catch(MessagingException mex){
>>>>            logger.error("execute got a MessagingException 
>>>> "+mex.getMessage(),mex);
>>>>        }    }
>>>> }
>>>>
>>>>
>>>> Geronimo Quartz Plan
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <jobs xmlns="http://geronimo.apache.org/xml/ns/plugins/quartz-0.2">
>>>>    <environment 
>>>> xmlns="http://geronimo.apache.org/xml/ns/deployment-1.1">
>>>>              <moduleId>
>>>>            <groupId>test</groupId>
>>>>            <artifactId>ReportSendJobs</artifactId>
>>>>        </moduleId>
>>>>                     <dependencies>
>>>>            <dependency>
>>>>                 <groupId>test</groupId>
>>>>                 <artifactId>javamail-server</artifactId>
>>>>            </dependency>
>>>>                      <dependency>
>>>>                 <groupId>console.dbpool</groupId>
>>>>                 <artifactId>MySqlDB_report_sender</artifactId>
>>>>             </dependency>
>>>>        </dependencies>               </environment>
>>>>      <job>
>>>>        <job-name>Job name</job-name>
>>>>        <job-class>classpath to jobb class</job-class>
>>>>        <cron-expression>0/30 * * * * ?</cron-expression>
>>>>              <resource-ref>
>>>>            <property>Database</property>
>>>>            <res-type>javax.sql.DataSource</res-type>
>>>>            <res-auth>Container</res-auth>
>>>>            <res-sharing-scope>Shareable</res-sharing-scope>
>>>>            <pattern>
>>>>                <name>MySqlDB_report_sender</name>
>>>>            </pattern>
>>>>        </resource-ref>
>>>>              <resource-ref>
>>>>            <property>MailSession</property>
>>>>            <res-type>javax.mail.Session</res-type>
>>>>            <res-auth>Container</res-auth>
>>>>            <res-sharing-scope>Shareable</res-sharing-scope>
>>>>            <pattern>
>>>>                <name>mail/MailSession</name>
>>>>            </pattern>
>>>>        </resource-ref>    </job> </jobs>
>>>>
>>>>
>>>> The "mail-server" plan (slightly moddyfied version of what I got 
>>>> from Rick):
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>>
>>>> <module xmlns="http://geronimo.apache.org/xml/ns/deployment-1.1">
>>>> <dep:environment 
>>>> xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.1">
>>>>   <dep:moduleId>
>>>>     <dep:groupId>test</dep:groupId>
>>>>     <dep:artifactId>javamail-server</dep:artifactId>
>>>>   </dep:moduleId>
>>>>  
>>>>   <dep:dependencies>
>>>>     <dep:dependency>
>>>>       <dep:groupId>geronimo</dep:groupId>
>>>>       <dep:artifactId>geronimo-mail</dep:artifactId>
>>>>       <dep:version>1.1.1</dep:version>
>>>>       <dep:type>jar</dep:type>
>>>>       <dep:import>classes</dep:import>
>>>>     </dep:dependency>
>>>>     <dep:dependency>
>>>>       <dep:groupId>geronimo</dep:groupId>
>>>>       <dep:artifactId>geronimo-javamail-transport</dep:artifactId>
>>>>       <dep:version>1.1.1</dep:version>
>>>>       <dep:type>jar</dep:type>
>>>>       <dep:import>classes</dep:import>
>>>>     </dep:dependency>
>>>>     <dep:dependency>
>>>>       <dep:groupId>geronimo</dep:groupId>
>>>>       <dep:artifactId>rmi-naming</dep:artifactId>
>>>>       <dep:type>car</dep:type>
>>>>     </dep:dependency>
>>>>   </dep:dependencies>
>>>>   <dep:hidden-classes/>
>>>>   <dep:non-overridable-classes/>
>>>> </dep:environment>
>>>>
>>>> <gbean name="SMTPTransport" 
>>>> class="org.apache.geronimo.mail.SMTPTransportGBean">
>>>>   <attribute name="host">removed dns name</attribute>
>>>>   <attribute name="port">25</attribute>
>>>> </gbean>
>>>> <gbean name="mail/MailSession" 
>>>> class="org.apache.geronimo.mail.MailGBean">
>>>>   <attribute name="transportProtocol">smtp</attribute>
>>>>   <attribute name="debug">true</attribute>
>>>>   <reference name="Protocols">
>>>>      <name>SMTPTransport</name>
>>>>   </reference>
>>>> </gbean>
>>>> </module>
>>>>
>>>>
>>>> Peter Petersson skrev:
>>>>> It is a remote SMTP server (in our lan) and autentication shuld 
>>>>> not be needed from inside our LAN (I have sucsessfully accessed 
>>>>> the SMTP server from within a vanila tomcat5 web app without 
>>>>> autentication).
>>>>> Could a autentication faliur result in a NoSuchProviderException ?
>>>>> I have tryed setting the Transport attributes (smtphost, username, 
>>>>> password) from within the code
>>>>>
>>>>> Transport tr = mailSession.getTransport("smtp");
>>>>> tr.connect(smtphost, username, password);
>>>>> and also tryed a GBean via the attributes (host,port).
>>>>> The Gbean is from a sligtly moddified version of the "example mail 
>>>>> session" that Rick posted (he did a farly good shoot from the hip 
>>>>> ;)).
>>>>>
>>>>> I but regardles of a lott of testing with different setups i still 
>>>>> get
>>>>>
>>>>> javax.mail.NoSuchProviderException: Unable to locate provider for 
>>>>> protocol: smtp
>>>>>        at javax.mail.Session.getProvider(Session.java:227)
>>>>>        at javax.mail.Session.getTransport(Session.java:336)
>>>>>
>>>>>
>>>>> Im testing this on a fresh install of Geronimo 1.1.1
>>>>>
>>>>> Anny suggestions ?
>>>>>
>>>>> Cheers
>>>>>   Peter
>>>>>
>>>>> Aaron Mulder skrev:
>>>>>> Does your localhost have a mail server running?  Or are you 
>>>>>> trying to
>>>>>> send through a remote SMTP server?  Is the mail server open or do

>>>>>> you
>>>>>> need to authenticate to it?
>>>>>>
>>>>>> Thanks,
>>>>>>     Aaron
>>>>>>
>>>>>> On 9/29/06, Peter Petersson <peter@pmb.mine.nu> wrote:
>>>>>>> Hi all!
>>>>>>>
>>>>>>> I quite new to Geronimo (using 1.1.1) and have some problems

>>>>>>> geting mail
>>>>>>> to work from a geronimo-quartz job.
>>>>>>> As I understand it I need to set up a gbean or do some other
>>>>>>> configuration for javamail to work (?).
>>>>>>> The example i have folowed for the Quartz Scheduler Plugin over
at
>>>>>>> http://gplugins.sourceforge.net/ have a nice setup including

>>>>>>> mailing (in
>>>>>>> the "Deployable Jobs Example" section) but it dose not go into

>>>>>>> details
>>>>>>> on howto set up mailing (its not the scope of the example).
>>>>>>>
>>>>>>> What do I need to do to get it to work? as it is now i get this

>>>>>>> exception
>>>>>>>
>>>>>>> MessagingException Unable to locate provider for protocol: smtp
>>>>>>>
>>>>>>> Anny good pointer out there to get mailing working in Geronimo
?
>>>>>>>
>>>>>>> Cheers
>>>>>>>   Peter
>>>>>>>
>>>>
>>>
>>

Mime
View raw message