?
Solved

Steps for setting up JavaMail for only sending messages

Posted on 2004-09-29
6
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12179189
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 400 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
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 

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 600 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 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