You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Fabio Rossi <pa...@yahoo.it> on 2007/07/17 11:47:04 UTC

Cannot send mail from servlet

Hi. I'll try to explain my problem, that is about sending an email with JavaMail API from a servlet. Anyway, it seems to be a more general problem i'm having with servlet. I'm using Tomcat 5.0. I've a servlet that process a request, writes to a database some info and then send a email with a summary to the user.

In the method sendMail() i send the email. I test the method in isolation and it works fine. I can connect to my local smtp server and send the email...
Here's the code:

do some stuff.....and send the email

logInfo("Sending summary email");
        sendMail(summary.toString(),clientEmail);
        response.setStatus(HttpServletResponse.SC_OK);
        logInfo("Email sent");
        }catch(Exception e){
            logError("Error sending summary mail to address: "+clientEmail,e);
            e.printStackTrace();
        }
    }

No exceptions are throwed...but it seems that the servlet doesn't "wait" for the method. When i test the method in isolation, it takes some seconds before the mail is sent, and in the smtp local server application i can see that a connection has been established. 

If i call the same code (the sendMail method) from the servlet i get "Email Sent" log messagge immediately and the servlet "exit"..like if the message was sent. But nothing has happened (no connection is established with the local smtp server) and no exception is thrown..


Anyone can help me?


 	      
---------------------------------

---------------------------------
L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail

Re: Cannot send mail from servlet

Posted by hanasaki <ha...@hanaden.com>.
Running in tomcat 6.x?

Any chance there is a jndi and smtp config setup in tomcat that is
messing with this?  Post the code for the sendMail method and any custom
code it calls.

review your web.xml and tomcat configurations for smtp related stuff.

David Smith wrote:
> Seems to me the important part of this is the sendMail method.  Could
> you post that?
> 
> --David
> 
> Fabio Rossi wrote:
>> Hi. I'll try to explain my problem, that is about sending an email
>> with JavaMail API from a servlet. Anyway, it seems to be a more
>> general problem i'm having with servlet. I'm using Tomcat 5.0. I've a
>> servlet that process a request, writes to a database some info and
>> then send a email with a summary to the user.
>>
>> In the method sendMail() i send the email. I test the method in
>> isolation and it works fine. I can connect to my local smtp server and
>> send the email...
>> Here's the code:
>>
>> do some stuff.....and send the email
>>
>> logInfo("Sending summary email");
>>         sendMail(summary.toString(),clientEmail);
>>         response.setStatus(HttpServletResponse.SC_OK);
>>         logInfo("Email sent");
>>         }catch(Exception e){
>>             logError("Error sending summary mail to address:
>> "+clientEmail,e);
>>             e.printStackTrace();
>>         }
>>     }
>>
>> No exceptions are throwed...but it seems that the servlet doesn't
>> "wait" for the method. When i test the method in isolation, it takes
>> some seconds before the mail is sent, and in the smtp local server
>> application i can see that a connection has been established.
>> If i call the same code (the sendMail method) from the servlet i get
>> "Email Sent" log messagge immediately and the servlet "exit"..like if
>> the message was sent. But nothing has happened (no connection is
>> established with the local smtp server) and no exception is thrown..
>>
>>
>> Anyone can help me?
>>
>>
>>            ---------------------------------
>>
>> ---------------------------------
>> L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail
>>   
> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot send mail from servlet

Posted by Juha Laiho <Ju...@iki.fi>.
Fabio Rossi wrote:
> Here's sendMail method:
> 
>                 Properties props = new ConfigLoader().loadConfigFile();
>                 String prop;
>                 prop=(String)props.get("summary_header");
>                 if(prop!=null)
>                     summary=prop+"\n"+summary;
>                 prop= (String) props.get("mail.smtp.host");
>                 props.put( "mail.smtp.host",prop);
>                 prop= (String) props.get("mail.smtp.port");
>                 props.put("mail.smtp.port",prop);
>                Session session = Session.getDefaultInstance(props);
>                 Message message = new MimeMessage( session );
> 
>                 InternetAddress from = new InternetAddress((String) props.get("internet_address"));
>                 InternetAddress to[] = InternetAddress.parse( userEmail );
> 
>                 message.setFrom( from );
>                 message.setRecipients( Message.RecipientType.TO, to );
>                 message.setSubject((String)props.getProperty("subject"));
>                 message.setSentDate( new Date() );
>                 message.setText( summary );
>                 Transport.send(message);

Detail questions;
- are you certain that the ConfigLoader actually loads the relevant
  configuration (adding some logging lines to the code would help in
  seeing what is going on)?
- the processing of mail.smtp.host and mail.smtp.port seem to be "no
  action" for me: you're retrieving the entries from the same properties
  object to which you're storing them -- should you be retrieving them
  from some other place (such as system properties)?

-- 
..Juha

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Cannot send mail from servlet

Posted by Fabio Rossi <pa...@yahoo.it>.
Here's sendMail method:


                Properties props = new ConfigLoader().loadConfigFile();
                String prop;
                prop=(String)props.get("summary_header");
                if(prop!=null)
                    summary=prop+"\n"+summary;
                prop= (String) props.get("mail.smtp.host");
                props.put( "mail.smtp.host",prop);
                prop= (String) props.get("mail.smtp.port");
                props.put("mail.smtp.port",prop);
               Session session = Session.getDefaultInstance(props);
                Message message = new MimeMessage( session );

                InternetAddress from = new InternetAddress((String) p                    rops.get("internet_address"));
                InternetAddress to[] = InternetAddress.parse( userEmail );

                message.setFrom( from );
                message.setRecipients( Message.RecipientType.TO, to );
                message.setSubject((String)props.getProperty("subject"));
                message.setSentDate( new Date() );
                message.setText( summary );
                Transport.send(message);

David Smith <dn...@cornell.edu> ha scritto: Seems to me the important part of this is the sendMail method.  Could 
you post that?

--David

Fabio Rossi wrote:
> Hi. I'll try to explain my problem, that is about sending an email with JavaMail API from a servlet. Anyway, it seems to be a more general problem i'm having with servlet. I'm using Tomcat 5.0. I've a servlet that process a request, writes to a database some info and then send a email with a summary to the user.
>
> In the method sendMail() i send the email. I test the method in isolation and it works fine. I can connect to my local smtp server and send the email...
> Here's the code:
>
> do some stuff.....and send the email
>
> logInfo("Sending summary email");
>         sendMail(summary.toString(),clientEmail);
>         response.setStatus(HttpServletResponse.SC_OK);
>         logInfo("Email sent");
>         }catch(Exception e){
>             logError("Error sending summary mail to address: "+clientEmail,e);
>             e.printStackTrace();
>         }
>     }
>
> No exceptions are throwed...but it seems that the servlet doesn't "wait" for the method. When i test the method in isolation, it takes some seconds before the mail is sent, and in the smtp local server application i can see that a connection has been established. 
>
> If i call the same code (the sendMail method) from the servlet i get "Email Sent" log messagge immediately and the servlet "exit"..like if the message was sent. But nothing has happened (no connection is established with the local smtp server) and no exception is thrown..
>
>
> Anyone can help me?
>
>
>         
> ---------------------------------
>
> ---------------------------------
> L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail
>   


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org



       
---------------------------------

---------------------------------
L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail

Re: Cannot send mail from servlet

Posted by David Smith <dn...@cornell.edu>.
Seems to me the important part of this is the sendMail method.  Could 
you post that?

--David

Fabio Rossi wrote:
> Hi. I'll try to explain my problem, that is about sending an email with JavaMail API from a servlet. Anyway, it seems to be a more general problem i'm having with servlet. I'm using Tomcat 5.0. I've a servlet that process a request, writes to a database some info and then send a email with a summary to the user.
>
> In the method sendMail() i send the email. I test the method in isolation and it works fine. I can connect to my local smtp server and send the email...
> Here's the code:
>
> do some stuff.....and send the email
>
> logInfo("Sending summary email");
>         sendMail(summary.toString(),clientEmail);
>         response.setStatus(HttpServletResponse.SC_OK);
>         logInfo("Email sent");
>         }catch(Exception e){
>             logError("Error sending summary mail to address: "+clientEmail,e);
>             e.printStackTrace();
>         }
>     }
>
> No exceptions are throwed...but it seems that the servlet doesn't "wait" for the method. When i test the method in isolation, it takes some seconds before the mail is sent, and in the smtp local server application i can see that a connection has been established. 
>
> If i call the same code (the sendMail method) from the servlet i get "Email Sent" log messagge immediately and the servlet "exit"..like if the message was sent. But nothing has happened (no connection is established with the local smtp server) and no exception is thrown..
>
>
> Anyone can help me?
>
>
>  	      
> ---------------------------------
>
> ---------------------------------
> L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail
>   


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org