Solved

Example of a SMTP call using a java program on linux

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 67
couple of eclipse 5 36
type mismatch (Object[] to double[] 4 24
JAVA API design with micro service cloud in mind 1 14
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

776 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