title: How do I send a email with java?
body: <p>I am trying to send a email using java mail. But when I run the code bellow I get a ton of errors. Can you explain to me why the code is not working. So what I am trying to do is to send a email using a java. If there is any other way can you please tell me bellow. My guess is something is wrong about the smpt server. I am new to stack overflow so please forgive me if I have not wrote the question correctly.</p>
  <p><em><strong>Edit: I now understand what is wrong I do not have a SMPT server running on my local host. Can someone explain how to create a SMPT server for free.</strong></em></p>
  <p><strong>These are the errors I get...</strong></p>
  <pre class="lang-java prettyprint-override"><code>/home/theprogrmmer/.jdks/openjdk-15.0.1/bin/java -javaagent:/home/theprogrmmer/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/202.8194.7/lib/idea_rt.jar=44243:/home/theprogrmmer/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/202.8194.7/bin -Dfile.encoding=UTF-8 -classpath /home/theprogrmmer/IdeaProjects/practice 1/out/production/practice 1:/home/theprogrmmer/IdeaProjects/practice 1/lib/javax.ejb.jar:/home/theprogrmmer/IdeaProjects/practice 1/lib/javax.jms.jar:/home/theprogrmmer/IdeaProjects/practice 1/lib/javax.annotation.jar:/home/theprogrmmer/IdeaProjects/practice 1/lib/javax.persistence.jar:/home/theprogrmmer/IdeaProjects/practice 1/lib/javax.resource.jar:/home/theprogrmmer/IdeaProjects/practice 1/lib/javax.transaction.jar:/home/theprogrmmer/IdeaProjects/practice 1/lib/javax.servlet.jar:/home/theprogrmmer/IdeaProjects/practice 1/lib/javax.servlet.jsp.jar:/home/theprogrmmer/IdeaProjects/practice 1/lib/javax.servlet.jsp.jstl.jar:/home/theprogrmmer/IdeaProjects/practice 1/src/com/company/mail.jar:/home/theprogrmmer/IdeaProjects/practice 1/src/com/company/jaf-1.0.2/activation.jar com.company.SendEmail
  javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
    nested exception is:
      java.net.ConnectException: Connection refused
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
      at javax.mail.Service.connect(Service.java:295)
      at javax.mail.Service.connect(Service.java:176)
      at javax.mail.Service.connect(Service.java:125)
      at javax.mail.Transport.send0(Transport.java:194)
      at javax.mail.Transport.send(Transport.java:124)
      at com.company.SendEmail.main(SendEmail.java:48)
  Caused by: java.net.ConnectException: Connection refused
      at java.base/sun.nio.ch.Net.connect0(Native Method)
      at java.base/sun.nio.ch.Net.connect(Net.java:574)
      at java.base/sun.nio.ch.Net.connect(Net.java:563)
      at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
      at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
      at java.base/java.net.Socket.connect(Socket.java:648)
      at java.base/java.net.Socket.connect(Socket.java:597)
      at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
      ... 7 more
  </code></pre>
  <p><strong>This is my java code....</strong></p>
  <pre class="lang-java prettyprint-override"><code>package com.company;
  
  // File Name SendEmail.java
  
  import java.util.*;
  import javax.mail.*;
  import javax.mail.internet.*;
  import javax.activation.*;
  
  public class SendEmail {
  
      public static void main(String [] args) {
          // Recipient's email ID needs to be mentioned.
          String to = &quot;abcd@gmail.com&quot;;
  
          // Sender's email ID needs to be mentioned
          String from = &quot;web@gmail.com&quot;;
  
          // Assuming you are sending email from localhost
          String host = &quot;localhost&quot;;
  
          // Get system properties
          Properties properties = System.getProperties();
  
          // Setup mail server
          properties.setProperty(&quot;mail.smtp.host&quot;, host);
  
          // Get the default Session object.
          Session session = Session.getDefaultInstance(properties);
  
          try {
              // Create a default MimeMessage object.
              MimeMessage message = new MimeMessage(session);
  
              // Set From: header field of the header.
              message.setFrom(new InternetAddress(from));
  
              // Set To: header field of the header.
              message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  
              // Set Subject: header field
              message.setSubject(&quot;This is the Subject Line!&quot;);
  
              // Now set the actual message
              message.setText(&quot;This is actual message&quot;);
  
              // Send message
              Transport.send(message);
              System.out.println(&quot;Sent message successfully....&quot;);
          } catch (MessagingException mex) {
              mex.printStackTrace();
          }
      }
  }
  </code></pre>