Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Mail Sender Demo Using JavaMail

4.33/5 (3 votes)
16 Nov 2014CPOL2 min read 13.1K   478  
This is a simple app that may have not many features but it sets the basis for a good Desktop Email Sender App.

Image 1

Introduction

To send an e-mail using your Java Application is simple enough but to start with, you should have JavaMail API and Java Activation Framework (JAF) installed on your machine. We will see how to include these jar files in the projects. If you are using Netbean or Eclipse, then that is very simple. This is a simple app that may not have many features but it sets the basis for a good Desktop Email Sender App.

Package com.sun.mail.smtp Description

An SMTP protocol provider for the JavaMail API that provides access to an SMTP server. Refer to RFC 821 for more information.

When sending a message, detailed information on each address that fails is available in an SMTPAddressFailedException chained off the top level SendFailedException that is thrown. In addition, if the mail.smtp.reportsuccess property is set, an SMTPAddressSucceededException will be included in the list for each address that is successful. Note that this will cause a top level SendFailedException to be thrown even though the send was successful.

Using the Code

Before you start the coding, it is important that you should have to do the following things:

  1. Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. You need to add mail.jar and activation.jar files in your CLASSPATH. These two jar files can be downloaded easily. Feel free.

Here are the two links:

  1. For JAF: http://www.oracle.com/technetwork/java/jaf11-139815.html
  2. For javaMail: http://www.oracle.com/technetwork/java/javamail/index.html

Image 2

Code is quite simple. Just parse through it:

Java
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  //It works as a process bar            
  jLabel7.setVisible(true);
  
  String host="smtp.gmail.com";   
  final String user = jTextField1.getText();  
  final String password = new String(jPasswordField1.getPassword());
  


   if(!user.equals("") && !password.equals(""))
   {
     String SMTP_PORT = "465";
     String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";    
    
     String to=jTextField3.getText();
  
     //Get the session object  
     Properties props = new Properties();
     props.put("mail.smtp.starttls.enable", "true");
     props.put("mail.smtp.host",host);  
     props.put("mail.smtp.auth", "true");  
     props.put("mail.debug", "true");
     props.put("mail.smtp.port", SMTP_PORT);
     props.put("mail.smtp.socketFactory.port", SMTP_PORT);
     props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
     props.put("mail.smtp.socketFactory.fallback", "false");  
   
     Session session = Session.getDefaultInstance(props,  
     new javax.mail.Authenticator() {
        
       protected PasswordAuthentication getPasswordAuthentication() {  
       return new PasswordAuthentication(user,password);  
       }  
     });    
   
     //Compose the message  
     
     try 
     {  
        MimeMessage message = new MimeMessage(session);  
        
        // creates message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(message, "text/html");     
     
        message.setFrom(new InternetAddress(user));  
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));        
        
        message.setSubject(jTextField5.getText());
        message.setText(jTextPane1.getText());  
        //send the message  
        Transport.send(message);    
     
        JOptionPane.showMessageDialog(null,"message sent successfully...");
        jLabel7.setVisible(false);
      } 
      catch (MessagingException e) {e.printStackTrace();}  
      }
  
   else 
   {
      JOptionPane.showMessageDialog(null,"Dear User! Please Enter Email or Password");
   }  
}

Points of Interest

There is an interesting thing that I also tried such an app in C#.NET. Making that is also a very simple. If you are planning to make a quality and good HCI following app build a Attachment portion in it. Many websites give a simple attachment code of variety. It may be true that you find another way for building such Desktop Mail Sender but it's also going to help you in that way. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)