Solved

Send an email with J2EE

Posted on 2009-07-06
14
454 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

805 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