Solved

Steps for setting up JavaMail for only sending messages

Posted on 2004-09-29
6
210 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 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

729 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