wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes JavaMail how to send a email Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail how to send a email" Watch "JavaMail how to send a email" New topic

JavaMail how to send a email

Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 94
Hi friends, i installed java on my centos:

1) yum groupinstall "Java Development"

2) downloaded from: http://www.oracle.com/technetwork/java/javamail/index-138643.html

downloaded on /usr/scripts/javamail1_4_4


3) unzip javamail1_4_4.zip

moved the folder: javamail1_4_4 to javamail

mv javamail1_4_4 javamail

4) added the classpath in .bash_profile

vi /root /.bash_profile

and added this lines:


here my code: aviso.java

//package aviso;

import java.util.*;
//import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

public class aviso {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.setProperty("mail.user", "me");
props.setProperty("mail.password", "***");
// Preparamos la sesion
//Session session = Session.getDefaultInstance(props,null);
Session session = Session.getInstance(props,null);
// Construimos el mensaje
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("me@domain.com"));
new InternetAddress("me@domain.com"));
message.setText("Esto es una prueba.");

/*DataHandler dh = new DataHandler("Texto del mensaje","text/plain");
// Lo enviamos.
Transport t = session.getTransport("smtp");
t.sendMessage(message, message.getAllRecipients());

// Cierre.

System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nEmail Enviado!");
catch (Exception e)
when compile:
[root@mail scripts]# javac aviso.java
[root@mail scripts]#

when run the program:
i get these errors:

[root@mail scripts]# java aviso
Exception in thread "main" java.lang.NoClassDefFoundError: javax.mail.internet.MimeMessage
at java.lang.Class.initializeClass(libgcj.so.7rh)
at aviso.main(aviso.java:23)
Caused by: java.lang.ClassNotFoundException: javax.activation.DataSource not found in gnu.gcj.runtime.SystemClassLoader{urls=[./,file:/usr/scripts/javamail/mail.jar], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.Class.initializeClass(libgcj.so.7rh)
...1 more
[root@mail scripts]#

Note: in the directory javamail, if you see only have mail.jar, not activation.jar (in some posts (here) recommend have activation.jar and mail.jar)

[root@mail javamail]# ls -l
total 652
-rw-r--r-- 1 root root 26846 Jan 14 2011 CHANGES.txt
-rw-r--r-- 1 root root 5899 Jan 14 2011 COMPAT.txt
-rw-r--r-- 1 root root 3322 Jan 14 2011 COPYRIGHT.txt
drwxr-xr-x 7 root root 4096 Jun 23 2010 demo
-rw-r--r-- 1 root root 1726 Jan 14 2011 distributionREADME.txt
drwxr-xr-x 3 root root 4096 Jun 23 2010 docs
drwxr-xr-x 2 root root 4096 Jan 14 2011 lib
-rw-r--r-- 1 root root 16181 Jan 14 2011 LICENSE.txt
-rw-r--r-- 1 root root 494975 Jan 14 2011 mail.jar
-rw-r--r-- 1 root root 9142 Jan 14 2011 NOTES.txt
-rw-r--r-- 1 root root 1200 Jan 14 2011 NTLMNOTES.txt
-rw-r--r-- 1 root root 8467 Jan 14 2011 README.txt
-rw-r--r-- 1 root root 11663 Jan 14 2011 SSLNOTES.txt
[root@mail javamail]#

Thanks, how to fix the error?
Tim Moores

Joined: Sep 21, 2011
Posts: 2408
You need the Java Activation Framework (JAF) in addition to JavaMail. Look for a jar file called "activation.jar" or something like that.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

If you upgrade to Java 6 you get JAF for free in the standard libraries.

How To Ask Questions How To Answer Questions
srinivas srinivasmeenavalli
Ranch Hand

Joined: Jul 13, 2008
Posts: 65
Don't share your user credentials to the Group
Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

Miguel, Srinivas is right. These forums are available for everybody, and unfortunately that includes people who may be up to no good. I've replaced the user name, password and email addresses with something else.

Could you also UseCodeTags next time, and remember to preserve indentation? That would make it easier to read your code.
I agree. Here's the link: http://aspose.com/file-tools
subject: JavaMail how to send a email
Similar Threads
Cannot find jni_md.h in /usr/bin/java/ when installing APR Native Library in Tomcat 6
is my tomcat there?---UNIX
Configure Jboss in Ant???
Changing run levels in Linux