Obrigado pelas respostas imediatas!
jakefrog,
Já tinha lido esses tutoriais. Porém, não consegui colocar em prática.
leorbarbosa,
Vi os seus fontes, bem bacanas!
O que eu tenho aqui é basicamente isso, só que na hora de rodar está retornando um erro. Compila normal, mas da erro na execução. Segue o fonte:
classpath:
C:\bibliotecas\mail.jar;C:\bibliotecas\dsn.jar;C:\bibliotecas\imap.jar;C:\bibliotecas\mailapi.jar;C:\bibliotecas\pop3.jar;C:\bibliotecas\smtp.jar;C:\bibliotecas\activation.jar
Estou compilando no DOS, javac e java para executar.
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;
import java.util.Properties;
public class Email {
private static final String SMTP_HOST_NAME = "<servidor smtp>";
private static final String SMTP_AUTH_USER = "<email>";
private static final String SMTP_AUTH_PWD = "<senha>";
public static void main(String[] args) throws Exception{
new Email().test();
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setContent("This is a test", "text/plain");
message.setFrom(new InternetAddress("<email>"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("<email>"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
e me retorna o seguinte erro:
Exception in thread "main" java.lang.NoClassDefFoundError: Email
Caused by: java.lang.ClassNotFoundException: Email
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Email. Program will exit.
já não sei mais muito o que fazer. Obs: não tem nenhum client de email configurado na máquina.
Obrigado!