Use Java to send email

walden systems, geeks corner, programming, languages, java, scope, local scope, global scope, functions, variables, developer, object oriented language, OOP, File, output, stream, fileoutputstream, string, getbyte, sendmail
Java Powers the Biggest Technology. Mobile. Web apps. The Internet of Things. Big data. Machine learning. Cloud platform and cloud infrastructure. The next big things are here now, and Java is at the center of them all. Whether you’re developing the robust enterprise back end, building the agile front end, or thriving in a DevOps role, Java skills can up your game. With more than 10 million programmers and 13 billion Java-enabled devices worldwide, Java is the language that connects applications to data, data to people, and people to their digital lifestyle.

In order to create a Java applicagtion to send email, you will have to have JavaMail API and Java Activation Framework installed on your machine.

JavaMail can be found here.
Java Activation Framework can be found here.

Download and unzip these files, in the newly unzipped directories, you will find jar filess for both the applications. You need to add mail.jar and activation.jar files in your CLASSPATH.

In this example, it is assumed that you have a smtp server installed on the machine such as Postcast server, Apache James server, Cmail server etc.

First, the jar files must me imported by adding the following lines :

1   import java.util.*;
2   import javax.mail.*;
3   import javax.mail.internet.*;
4   import javax.activation.*;

Next, we will declare string variables for the message components and the host :

1       String to          = "recipient@some_email.com";
2       String from        = "from@some_email.com";
3       String subject     = "Some subject";
4       String messageBody = "Text in the body of the email" ;
5
6       String host        = "localhost";
7       String smtpServer  = "someSMTPserver" ;


Finally, assemble the message by instantiating a MimeMessage ( "message" in this case ) and send it out using Transport :

 1       try 
 2       {
 3          MimeMessage message = new MimeMessage( session );
 4          message.setFrom( new InternetAddress( from ) );
 5          message.addRecipient( Message.RecipientType.TO, new InternetAddress( to ) );
 6          message.setSubject( subject );
 7          message.setText( messageBody );
 8 
 9          Transport.send(message);
10          System.out.println( "Sucess!" );
11       }
12       catch (MessagingException ex) 
13       {
14          ex.printStackTrace();
15       }

Full code

Here is the full code assembled :

 1    import java.util.*;
 2    import javax.mail.*;
 3    import javax.mail.internet.*;
 4    import javax.activation.*;
 5 
 6 
 7    public class SendEmail 
 8    {
 9 
10       public static void main(String [] args) 
11       {    
12    
13          String to          = "recipient@some_email.com";
14          String from        = "from@some_email.com";
15          String host        = "localhost";
16          String smtpServer  = "someSMTPserver" ;
17          String subject     = "Some subject";
18          String messageBody = "Text in the body of the email" ;
19    
20          // Get system properties
21          Properties properties = System.getProperties();
22    
23          properties.setProperty( smtpServer, host );
24 
25          Session session = Session.getDefaultInstance(properties);
26   
27          try 
28          {
29             MimeMessage message = new MimeMessage( session );
30             message.setFrom( new InternetAddress( from ) );
31             message.addRecipient( Message.RecipientType.TO, new InternetAddress( to ) );
32             message.setSubject( subject );
33             message.setText( messageBody );
34 
35             Transport.send(message);
36             System.out.println( "Sucess!" );
37          }
38          catch (MessagingException ex) 
39          {
40             ex.printStackTrace();
41          }
42       }
43    }