Solved

Send an email with J2EE

Posted on 2009-07-06
14
435 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
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 1

Author Comment

by:Nargzul
Comment Utility
@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
Comment Utility
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
Comment Utility
>>@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
Comment Utility
@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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

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

Open in new window

0
 
LVL 1

Author Comment

by:Nargzul
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
@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
Comment Utility
Nargzul, you don't need an Authenticator: the code Tuno85 posted will do the authentication
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
>  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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 66
word0 challenge 4 52
advertisement module in core php 4 78
compre toata in where clue oracle 4 41
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…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now