Solved

Example of a SMTP call using a java program on linux

Posted on 2014-01-16
5
365 Views
Last Modified: 2014-01-22
Hi All,

I'd like to write a java program that makes an SMTP connection to an email server & then send a test email eg "hello world" to a specified address.

Is there any example showing how this works?

javax.mail.service.connect

is the API, I'd like to use.

Thanks
0
Comment
Question by:eeclint
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 400 total points
ID: 39787664
Here is the basic idea...
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class TestSendingEmail
{
   public static void main(String [] args)
   {    
      String to = "you@yahoo.com";
      String from = "me@gmail.com";
      
      String smtpHost = "smtp.host.com";

      Properties properties = System.getProperties();
      properties.setProperty("mail.smtp.host", smtpHost);
      Session session = Session.getDefaultInstance(properties);

      try {
         MimeMessage message = new MimeMessage(session);

         message.setFrom(new InternetAddress(from));
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

         message.setSubject("An email from me to you");

         message.setText("Hello World!!");

         Transport.send(message);
         
         System.out.println("Success!!");
         
      } catch (MessagingException ex) {
         ex.printStackTrace();
      }
   }
}

Open in new window

For this you will need to have the following ...

Javamail - http://www.oracle.com/technetwork/java/javamail/index.html
JavaBeans Activation Framework - http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39787730
Your smtp server must be up & running with proper configuration to support above code.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 39787930
Most smtp servers will expect authentication of some kind. Here's an example of one of the most demanding:

http://technojeeves.com/joomla/index.php/free/80-send-secure-mail-via-google-with-java
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39799757
Is this problem resolved?
0
 

Author Closing Comment

by:eeclint
ID: 39801170
Sorry for the delay as I didn't see the solutions.

I will attempt to run this code in my box & ask further questions in a new thread if required.

Thanks for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question