Solved

Steps for setting up JavaMail for only sending messages

Posted on 2004-09-29
6
196 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SPLUNK REST  API call to Splunk to create and index? 2 67
mapBully challenge 6 88
wordappend challenge 8 84
Python Assistance 7 31
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 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

20 Experts available now in Live!

Get 1:1 Help Now