• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaMail how to send a email

 
Miguel Enriquez
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 2488
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you upgrade to Java 6 you get JAF for free in the standard libraries.
 
srinivas srinivasmeenavalli
Ranch Hand
Posts: 65
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't share your user credentials to the Group
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic