Solved

Example of a SMTP call using a java program on linux

Posted on 2014-01-16
5
360 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
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 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