Solved

How to send email in Java?

Posted on 2011-03-22
20
415 Views
Last Modified: 2012-05-11
Hi,
In Java how can I send email? Let's say I want to send email like that in an if-else condition:

From: somename1@domain.com //somename1 is always gonna be same
To: somename2@domain.com    //somename2 is the first input to the java code which is args[0]
Subject: Logging failed
Message: Logging failed due to loss of data

Thanks,
0
Comment
Question by:Tolgar
[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
  • 8
  • 5
  • 5
  • +2
20 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35189969
http://technojeeves.com/joomla/index.php/free/80-send-secure-mail-via-google-with-java

A better alternative (guessing) could be an STMPAppender for logging
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 35190084

JavaMail is very convenient and simple API to send mail

Thiese are general examples:

http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274
http://www.tutorialspoint.com/java/java_sending_email.htm

You'll need to connect to some host which runs SMTP server,
which should be mentioned in    
props.put("mail.smtp.host", "smtp.jcom.net");
(like in one of the examples above)

usually there are many hosts which can be used as SMTP connection
on your intranet

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35190148

First you'll need to download JavaMail
http://www.oracle.com/technetwork/java/javaee/index-138643.html

Don't know maybe now they changed, but I use two jar files which I put in my classpath when
I need to send emails - those are mail.jar and activation.jar
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 13

Expert Comment

by:darren-w-
ID: 35190397
I  too would recommend Javamail above, used it recently and its very easy to implement and use.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35190519
Tolgar - ARE you using it for logging alerts?
0
 

Author Comment

by:Tolgar
ID: 35190686
I don't know what you mean by logging alerts but in general I would like to send email to the user if something is not acceptable according to requirements.

if (string <64) {
DO SOMETHING
}
else{
SEND MAIL  TO args[0]
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35190740
Yes, that's a logging alert. Since professional quality Java apps should use a logging framework anyway, it becomes trivial to simply add an SMTPAppender. Plus, you don't have to implement any of the mailing details yourself:

See http://logback.qos.ch/manual/appenders.html#SMTPAppender
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35190751
>>any of the mailing details

Meaning any of the mailing code
0
 

Author Comment

by:Tolgar
ID: 35190972
@ for_yan

In the link you sent me I downloaded JavaMail 1.4.4. It does have mail.jar in it but there is no activation.jar. And there are some other jars as well. Which once should I put in my classpath?

Thanks,
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35191070
Perhaps, they no longer need activation.jar.
Try to use only mail.jar - if you lack something let us know.
I thnik they now consolidated - i continue using 1.2 or even earluiier, that's why I wrote about activation
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35191087
Maybe you stiill need it:

Q: Where is javax.activation?
A: javax.activation is part of the JavaBeans Activation Framework (JAF). The JAF is part of the JavaBeans "Glasgow" specification (see http://www.oracle.com/technetwork/java/javase/index-jsp-136939.html). The JavaMail API uses the JAF for data content handling.


This excerpt is from:
http://www.oracle.com/technetwork/java/faq-135477.html#JAF
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35191098
http://www.oracle.com/technetwork/java/jaf11-139815.html. activation.jar and any other javamail jars to go in classpath
0
 

Author Comment

by:Tolgar
ID: 35192886
Hi,
I am getting these errors when I compile my code in maven. How can I include them in my code?

package javax.mail does not exist

package javax.mail.internet does not exist

Thanks,
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 35193006
Try placing this in pom.xml
<dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35193055

This  is where they explain with examples how to add external dependences in Maven:
http://maven.apache.org/guides/getting-started/index.html#How_do_I_use_external_dependencies
0
 

Author Comment

by:Tolgar
ID: 35193518
@CEHJ: Now I am having the same problem with httpunit.

How should I define this dependency in pom.xml?

Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35193536
Yes. See your other q
0
 
LVL 92

Expert Comment

by:objects
ID: 35194844
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35199629
:) Incidentally, my first comment uses JavaMail, though that's not the best approach imo
0
 

Author Comment

by:Tolgar
ID: 35200187
:)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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 contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

717 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