Improve company productivity with a Business Account.Sign Up

x
?
Solved

How to send email in Java?

Posted on 2011-03-22
20
Medium Priority
?
443 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 1000 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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

589 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