Solved

Send an email with J2EE

Posted on 2009-07-06
14
480 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 86

Expert Comment

by:CEHJ
ID: 24789516
0
 
LVL 92

Expert Comment

by:objects
ID: 24790067
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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 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.

696 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