Solved

Steps for setting up JavaMail for only sending messages

Posted on 2004-09-29
6
198 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
3 Use Cases for Connected Systems

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

 

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

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

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…
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 …
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 …
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.

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

16 Experts available now in Live!

Get 1:1 Help Now