Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JAVA»JavaMail API – Sending Email using Java from Local SMTP

    JavaMail API – Sending Email using Java from Local SMTP

    RahulBy RahulMay 24, 20141 Min Read

    JavaMail API supports the JavaMail (javax.mail) interface for sending email messages. javax.mail has all the required classes for sending emails an other mail protocols like SMTP, POP3, and IMAP.

    In this article we are using JavaMail API for sending emails using Java programming language through local smtp server. make sure you are running smtp server on localhost.

    Step 1: Setup JavaMail Environment

    First we need to download jar file containing all classes in javax.mail. Download jar (mail.jar) file from oracle official website.

    Now set the classpath in system environment. Windows users make sure you have configured PATH variable for your Java installation.

    Windows:

    c:> set classpath=mail.jar;.;
    

    Linux:

    # export JAVA_HOME=/opt/jdk1.8.0_05/
    # export PATH=$PATH:$JAVA_HOME/bin
    # export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar:mail.jar:.
    

    Step 2: Write a Java Program to Send Email

    Create a java file SendMailJavaAPI.java with the following content. In this script you need to change email to and from variable as per your requirements.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
     
    public class SendMailJavaAPI {
     
            public static void main(String[] args) throws Exception{
     
            String to="recipient@example.com";
            String from="sender@example.com";
     
            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);
     
            String msgBody = "Sending email using JavaMail API...";
     
            try {
                Message msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress(from, "NoReply"));
                msg.addRecipient(Message.RecipientType.TO,
                                 new InternetAddress(to, "Mr. Recipient"));
                msg.setSubject("Welcome To Java Mail API");
                msg.setText(msgBody);
                Transport.send(msg);
                System.out.println("Email sent successfully...");
     
            } catch (AddressException e) {
                throw new RuntimeException(e);
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
        }
    }

    Step 3: Execute Program to Send Email

    Finally we need to execute java program to send email. as we know that this happens in two stpes, first to compile program and second to run it.

    # javac SendMailJavaAPI.java
    # java SendMailJavaAPI
    
    API Java JavaMail mail
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleSending Emails using PHP with PHPMailer
    Next Article JavaMail API – Sending Email using Java from Gmail SMTP

    Related Posts

    How to Install JAVA on Ubuntu 22.04

    Updated:May 16, 20224 Mins Read

    Java HashMap – How to Get Value from Key

    Updated:April 6, 20222 Mins Read

    Java HashMap – How to Get Key from Value

    Updated:April 6, 20222 Mins Read

    5 Methods to Print an Array in Java

    Updated:April 21, 20222 Mins Read

    How To Install Java on Debian 11

    4 Mins Read

    How to Install Roundcube Webmail on Ubuntu 20.04

    5 Mins Read

    2 Comments

    1. Veeramani on July 2, 2015 6:31 am

      It is working fine in Windows 7 but not working in Windows 8

      Reply
      • Sumit Shahu on November 23, 2016 1:26 am

        Veeramani mine is not working in window 7. guide me what to do

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    • How to Install Composer on Ubuntu 22.04
    • How to Create DMARC Record For Your Domain
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.