Solved

Example of a SMTP call using a java program on linux

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 45
more than one jdk and one jre 1 33
table example 4 23
javap not working 8 37
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

920 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

11 Experts available now in Live!

Get 1:1 Help Now