Solved

How to send email in Java?

Posted on 2011-03-22
20
375 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
 
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
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.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now