?
Solved

Send an email with J2EE

Posted on 2009-07-06
14
Medium Priority
?
482 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1400 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 600 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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

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…
A solution for Fortify Path Manipulation.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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