Skip to Content

Sending Email from EJB exposed as Webservice

Hi,

I have created a EJB, which i have wrapped in an EAR & exposed as a WebService.

<b>I want the code to send an email from the EJB</b>

I know there is a code to send email, but that code is to send an email from WebDynpro. I am not sure if that will work with the EJB.

Any ideas??

Regards,

Hanoz

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

3 Answers

  • Best Answer
    avatar image
    Former Member
    Aug 22, 2007 at 10:44 AM

    Hi Hanoz,

    you can create a Webservice for email sendings, please follow this usefull and smart weblog:

    /people/renald.wittwer/blog/2005/01/05/develop-a-web-service-that-sends-an-email

    Then you can call webservice methods from your EJB project in order to send email.

    That's all. Hope this help you.

    Regards,

    Vito

    Add comment
    10|10000 characters needed characters exceeded

    • Hi,

      Thanx for the prompt response.

      I went through that blog. Its good, but it requires me to install additional plugins, which is dont want to do.

      Here is the code to send email from WebDynpro:

      String l_str_to = "<destination email id>";
      
      			String l_str_from = "sender";
      			String p_subject = "U've got mail!!! ";
      	
      
      //			String p_l_request_id = "1" ;
      
      			// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
      			String host = "<SMTP SERver ip>";
      
      			// Create properties, get Session
      			Properties props = new Properties();
      
      			// If using static Transport.send(),
      			// need to specify which host to send it to
      			props.setProperty("mail.smtp.host", host);
      			// To see what is going on behind the scene
      			//props.put("mail.debug", "true");
      			Session session = Session.getInstance(props);
      
      
      			try {
      				// Instantiatee a message
      				Message msg = new MimeMessage(session);
      
      				//Set message attributes
      					msg.setFrom(new InternetAddress(l_str_from));
      					InternetAddress[] address = { new InternetAddress(l_str_to)};
       
      
      					msg.setRecipients(Message.RecipientType.TO, address);
      					msg.setSubject(p_subject);
      
      					//msg.setSentDate(new Date());
      				java.util.Date dt = new java.util.Date();
      				msg.setSentDate(dt);
      
      				
      				// Set message content
      				msg.setText("This is a test email");					 
      
      			
      				//Send the message
      				Transport.send(msg);
      			}
      
      			catch (Exception mex)
      			{
      				// Prints all nested (chained) exceptions as well
      				mex.printStackTrace();
      			}

      <b>i want a similar code, which can be executed from the EJB!!!!</b>

  • avatar image
    Former Member
    Aug 22, 2007 at 12:51 PM

    This looks like standard java mail API from SUN...

    try this link

    http://www.codeproject.com/useritems/Java_Mail.asp

    you only need the jars on the classpath

    bye

    Stefan

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 23, 2007 at 07:41 AM
    Add comment
    10|10000 characters needed characters exceeded