Solved

Send an email with J2EE

Posted on 2009-07-06
14
470 Views
Last Modified: 2012-05-07
Hi!

I've seen that J2EE contains email specification( http://en.wikipedia.org/wiki/J2ee ), but I've searched and I didn't find a standardized method to send email. After some search I've found some complicate class  programmed by hand.

So how can we simply send an email with from/to/subject/content informations?

Can you give me a sample code?

Thanks you!
0
Comment
Question by:Nargzul
[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
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 4

Expert Comment

by:Tuno85
ID: 24788992
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24789516
0
 
LVL 92

Expert Comment

by:objects
ID: 24790067
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.

 
LVL 1

Author Comment

by:Nargzul
ID: 24794151
@Tuno85, objects
And if the server need an authentification? what are the parameters name for this?

@CEHJ I think the improvement to not do a Session.put, ... isn't enough to justify to add a custom library
0
 
LVL 4

Expert Comment

by:Tuno85
ID: 24794424
Something like this will do the trick.
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24795327
>>@CEHJ I think the improvement to not do a Session.put, ... isn't enough to justify to add a custom library

I don't understand i'm afraid. There is no custom library involved - just JavaMail
0
 
LVL 1

Author Comment

by:Nargzul
ID: 24797888
@CEHJ: My fault sorry, it's ok
@Tuno85: Do I still need  this:

        sessionProperties.put("mail.smtp.host", SMTP_SERVER);
        Session session = Session.getDefaultInstance(sessionProperties, null);

because the host is already given in the "Transport" now?
0
 
LVL 4

Expert Comment

by:Tuno85
ID: 24798018
Yes but you could leave it out at Transport
Transport transport = session.getTransport();

Open in new window

0
 
LVL 1

Author Comment

by:Nargzul
ID: 24798234
Ok, but we specify it two times:


            sessionProperties.put("mail.smtp.host", SMTP_SERVER);
and
            transport.connect(SMTP_SERVER, SMTP_USER, SMTP_PWD);

Why we need to specify twice the server, I'm curious
0
 
LVL 4

Accepted Solution

by:
Tuno85 earned 350 total points
ID: 24798287
Please check this:

http://www.rgagnon.com/javadetails/java-0538.html

There are a lot other example on the net and solutions here at EE.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 24799428
> And if the server need an authentification? what are the parameters name for this?

Use an Authenticator

        Authenticator auth = new SMTPAuthenticator();
        Session mailSession = Session.getDefaultInstance(sessionProperties, auth);

Where the authentication looks like

    class SMTPAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication("smtpuser", "smtppassword");
        }
    }


> Why we need to specify twice the server, I'm curious

you don't
0
 
LVL 4

Expert Comment

by:Tuno85
ID: 24800988
@objects
That's what I am saying and that's what example in the link I provided shows. Please don't copy my answers.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24844947
Nargzul, you don't need an Authenticator: the code Tuno85 posted will do the authentication
0
 
LVL 92

Expert Comment

by:objects
ID: 24844972
>  Please don't copy my answers.

Didn't copy your answer, was just responding to question asked about the code I had already posted.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 49
servlet filter example 37 83
glassfish admin console 1 21
DTD and JAVA versions 1 29
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

756 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