• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

How to send email in Java?

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
Tolgar
Asked:
Tolgar
  • 8
  • 5
  • 5
  • +2
2 Solutions
 
CEHJCommented:
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
 
for_yanCommented:

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
 
for_yanCommented:

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
darren-w-Commented:
I  too would recommend Javamail above, used it recently and its very easy to implement and use.
0
 
CEHJCommented:
Tolgar - ARE you using it for logging alerts?
0
 
TolgarAuthor Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
>>any of the mailing details

Meaning any of the mailing code
0
 
TolgarAuthor Commented:
@ 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
 
for_yanCommented:
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
 
for_yanCommented:
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
 
CEHJCommented:
http://www.oracle.com/technetwork/java/jaf11-139815.html. activation.jar and any other javamail jars to go in classpath
0
 
TolgarAuthor Commented:
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
 
CEHJCommented:
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
 
for_yanCommented:

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
 
TolgarAuthor Commented:
@CEHJ: Now I am having the same problem with httpunit.

How should I define this dependency in pom.xml?

Thanks,
0
 
CEHJCommented:
Yes. See your other q
0
 
objectsCommented:
0
 
CEHJCommented:
:) Incidentally, my first comment uses JavaMail, though that's not the best approach imo
0
 
TolgarAuthor Commented:
:)
0

Featured Post

Technology Partners: 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!

  • 8
  • 5
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now