Solved

How to send email in Java?

Posted on 2011-03-22
20
391 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

786 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