Solved

Example of a SMTP call using a java program on linux

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now