A Problem with Java Mail API
843830Mar 27 2003 — edited Jul 11 2007Hi there,
I'm new to the Java Mail API and I have tried to send a mail via this api, but to no avail until how, I hope you can help me with this.
The problem is: I have read and hopefully understood the api documentation and have written the following frontend to allow me to send an email, but, on execution it says:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Message
at org.abstraxion.bib.Main.main(Main.java:48)
CLASSPATH and everything is setup ok, since there is no error when it comes to instantiating a new Session object or getting the correct Transport (smtp) from the Session.
Except when trying to access SMTPMessage.RecipientType.TO the above mentioned error occurs, why?
Since SMTPMessage is derived from MimeMessage and eventually javax.mail.Message, why the java runtime does not find the appropriate class definition? Or is there, somewhere hidden, a typo or a logical error of mine? I use Eclipse, which does not state any error and compiles the code without any complains.
Thanks in advance,
Carsten
---- snip
/* JAVA MAIL API */
/* <<Composite>> */
/* send and compose message
0. setup Properties for the Session Configuration
1. get configured (Default) Session Instance Object
2. compose messages for this session
3. get Transport from Session for given protocol (smtp)
4. connect to service via Transport
5. sendMessages iteratively via Transport (for a given number of receipients)
6. disconnect from Service
7. destroy objects
*/
package org.abstraxion.bib;
import java.lang.String;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
/*
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
*/
import com.sun.mail.smtp.SMTPMessage;
import com.sun.mail.smtp.SMTPTransport;
public final class SendMail {
private String _user;
private String _from;
private String _to;
private String _host;
private String _protocol;
private int _port;
private Object _content;
private String contenttype;
public SendMail(String user, String from, String to, String host, String protocol, int port, Object content, String content_type) {
_user = user;
_from = from;
_to = to;
_host = host;
_protocol = protocol;
_port = port;
_content = content;
contenttype = content_type;
}
public void send() throws Exception {
Properties props = new Properties();
props.setProperty("mail.smtp.user", _user);
props.setProperty("mail.smtp.host", _host);
props.setProperty("mail.smtp.port", Integer.toString(_port));
props.setProperty("mail.smtp.connectiontimeout", "10000");
props.setProperty("mail.smtp.timeout", "10000");
props.setProperty("mail.smtp.from", _from);
props.setProperty("mail.smtp.localhost", InetAddress.getLocalHost().getHostName());
props.setProperty("mail.smtp.ehlo", "true");
props.setProperty("mail.smtp.allow8bitmime", "true");
// get default Session
Session session = Session.getDefaultInstance(props);
// compose message
// create receipient / could be done via address String, too
InternetAddress inetaddr = new InternetAddress(_to);
SMTPMessage message = new SMTPMessage(session);
message.setRecipient(SMTPMessage.RecipientType.TO, inetaddr);
message.setContent(_content, contenttype);
message.saveChanges();
// send message
SMTPTransport transport = (SMTPTransport)session.getTransport(_protocol);
transport.connect();
transport.sendMessage(message, new InternetAddress[] {inetaddr}); // wird nicht klappen
transport.close();
}
}