0

We have a web application developed with JSP, Servlet and Hibernate. We have to send some automated emails from the application (We only have to send. we can hard code the send account name etc).

I came across Apache James. But I am not sure whether you can distribute it with the application as a library, where users don't have to configure it manually.

PHP developers claim that they can send emails without the server stuff, so Java must can too. If Apache James is not possible with this, what else?

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • Take a look at http://stackoverflow.com/questions/26087018/sending-emails-through-java-javax-mail-messagingexception-could-not-connect-t/26092701#26092701 – Anptk Mar 10 '15 at 06:50

3 Answers3

1

You don't need Apache James for sending emails. Use Java mail api: http://www.oracle.com/technetwork/java/javamail/index.html. Java mail api connects to a mail server for sending email (typically SMTP protocol). There are several samples at http://java.net/projects/javamail/downloads/download/javamail-samples.zip. Look at the *send* samples to understand how to send email. If you are using spring framework you get java mail wrappers which simplify the code a lot.

RaviH
  • 3,544
  • 2
  • 15
  • 14
0

As someone said, Java Mail is all you need to send e-mail in Java. You will need to provide the configuration that will allow you to connect to an SMTP server though.

If PHP developers don't need to provide that stuff, it's because it's usually done by the internet service provider.

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
0

Simple example of sending email using JavaMail API

In this example, we are going to learn how to send email by SMTP server installed on the machine e.g. Postcast server, Apache James server, Cmail server etc. If you want to send email by using your SMTP server provided by the host provider, see the example after this one. For sending the email using JavaMail API, you need to load the two jar files:

  • mail.jar
  • Activation.jar

Heres the link

import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
import javax.activation.*;  

public class SendEmail  
{  
 public static void main(String [] args){  
      String to = "sonoojaiswal1988@gmail.com";//change accordingly  
      String from = "sonoojaiswal1987@gmail.com";change accordingly  
      String host = "localhost";//or IP address  

     //Get the session object  
      Properties properties = System.getProperties();  
      properties.setProperty("mail.smtp.host", host);  
      Session session = Session.getDefaultInstance(properties);  

     //compose the message  
      try{  
         MimeMessage message = new MimeMessage(session);  
         message.setFrom(new InternetAddress(from));  
         message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
         message.setSubject("Ping");  
         message.setText("Hello, this is example of sending email  ");  

         // Send message  
         Transport.send(message);  
         System.out.println("message sent successfully....");  

      }catch (MessagingException mex) {mex.printStackTrace();}  
   }  
}  
George Rosario
  • 761
  • 11
  • 24
  • This is what I do not want to do. This connects to a host, which has a mail server. Try running your code without any change, it wont work as there is no active server. – PeakGen Mar 10 '15 at 17:13