Solved

Steps for setting up JavaMail for only sending messages

Posted on 2004-09-29
6
201 Views
Last Modified: 2013-11-24
Hello All,
I was wondering how do you setup JavaMail in an application to only send mail, there is no need to receive mail. I will be deploying the application to a JBoss application server which will be where the mail is sent from, what do I need to setup on it?

Do I need to install some sort of mailing server or will JavaMail take care of everything?
Thanks in advance.
0
Comment
Question by:gdlp2004
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12179189
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 total points
ID: 12179201
0
 
LVL 4

Expert Comment

by:gdrnec
ID: 12179203
Here is a very simple mail handler class that does the trick:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

/**
 *
 * @author  gdrnec
 */
public class MailHandler {
    private Session session;
    private String from = "bob@loadsaenergy.com";
    /** Creates a new instance of MailHandler */
    public MailHandler() {
        // Get system properties
        Properties props = System.getProperties();
       
        // Setup mail server
        props.put("mail.smtp.host", "smtp.loadsaenergy.com");
       
        try {
            // Get session
            session = Session.getDefaultInstance(props, null);
        } catch (Exception X) {
            System.out.println("Coudn't create mail session. " + X.getMessage());
        }
    }
   
    public void sendMail(String to, String msg, String subject) {
        try {            
            // Define message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setText(msg);
           
            // Send message
            Transport.send(message);
        } catch (Exception X) {
            System.out.println("Couldn't send mail. " + X.getMessage());
        }        
    }
   
}
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:gdlp2004
ID: 12179552
Thanks for the quick responses...

Now the server doesn't have any mail server on it, will I have to install a SMTP server? Also, is there some special setting in JBoss that I will have to add or configure to send the mail out correctly?

Thanks.
0
 
LVL 4

Accepted Solution

by:
gdrnec earned 150 total points
ID: 12179731
The server itself doesn't require a mail server on it. However, you do need an smtp server to send mail with. Usually this is provided by your isp so if you have access to the internet (which if you are sending mails, I would expect you to), you can contact your isp and they will give you an smtp host, probably in the form smtp.yourispdomain.com. When you find that address, just use it with the mail.smtp.host property and java mail should do the rest.

There are also a number of free smtp servers out there but because of the recent spam and virus nightmares, they are diminishing quickly.

Geoff
0
 

Author Comment

by:gdlp2004
ID: 12219858
This helped but we are building an application that will use the mailing function quite a bit. So, we created a Svc that is called when we need to mail something out. Once deployed we are using a JBoss server to run the application, so I was sort of looking for instructions on how to setup up both the Java side and the JBoss side to handle the Java side. If you know of a better place to look please let me know.
Thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

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