Meaningless Drivel is fun!*
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
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail how to send a email" Watch "JavaMail how to send a email" New topic
Author

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

unziped:

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:

CLASSPATH=.:/usr/scripts/javamail/mail.jar
export CLASSPATH

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.host","192.168.x.x");
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth", "true");
props.setProperty("mail.user", "me");
props.setProperty("mail.password", "***");
System.out.println("Enviando.");
// Preparamos la sesion
//Session session = Session.getDefaultInstance(props,null);
Session session = Session.getInstance(props,null);
// Construimos el mensaje
Message message = new MimeMessage(session);
System.out.println("Enviando2");
message.setFrom(new InternetAddress("me@domain.com"));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress("me@domain.com"));
message.setSubject("Prueba");
System.out.println("Enviando3");
message.setText("Esto es una prueba.");

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

// Cierre.
t.close();

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)
{
e.printStackTrace();
}
}
}
--------------------------------------------------------
when compile:
[root@mail scripts]# javac aviso.java
[root@mail scripts]#
(NO ERRORS)

when run the program:
i get these errors:

[root@mail scripts]# java aviso
Enviando.
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
Rancher

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
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JavaMail how to send a email
 
Similar Threads
Changing run levels in Linux
Configure Jboss in Ant???
Cannot find jni_md.h in /usr/bin/java/ when installing APR Native Library in Tomcat 6
is my tomcat there?---UNIX