import java.io.;
import javax.mail.;
import javax.mail.internet.;
import javax.activation.;
import java.security.;
import java.util.Properties;
import com.sun.net.ssl.;
import java.io.IOException;
import java.net.InetAddress;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
public class SMTPSend extends DummySSLSocketFactory {
public SMTPSend() {
}
public static void main(String[] argv) {
SMTPSend smtpSend = new SMTPSend();
smtpSend.msgsend();
}
public void msgsend() {
[b]Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Security.setProperty( “ssl.SocketFactory.provider”, “DummySSLSocketFactory”);[/b]
String username = "eu@host.com.br";
String password = "senha";
String smtphost = "mail.host.com.br";
String subject = "This is a JavaMail API Test";
String from = "eu@host.com.br";
String to = "eu@host2.com.br";
String mailer = "JavaMail API";
try {
[b]final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.setProperty( "mail.smtp.socketFactory.class", SSL_FACTORY);
// Stop socket from falling back to insecure connection)
props.setProperty( "mail.smtp.socketFactory.fallback", "false");
// changing properties to secure port
props.setProperty( "mail.smtp.port", "25");
props.setProperty( "mail.smtp.socketFactory.port", "25");[/b]
// Get a Session object
Session session = Session.getDefaultInstance(props, null);
// construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText("Sample Message");
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());
System.out.println("antes do transporte getsmtp");
Transport tr = session.getTransport("smtp"); System.out.println("depois do transporte getsmtp");
tr.connect(smtphost, username, password); System.out.println("antes do tr connect");
msg.saveChanges(); // don't forget this
tr.sendMessage(msg, msg.getAllRecipients()); System.out.println("depois de sendmail");
tr.close();
System.out.println("\nMail was sent successfully.");
}
catch (Exception e) {
System.out.println("\nErro falha");
e.printStackTrace();
}
}
}
Eu uso SMTP, com imap eu consigo enviar email entre pessoas do mesmo host que eu, mas com smtp, nem isso consigo. PArece que ele nao acha o SSLSocketFactory. E da essas excecoes:
DummySocketFactory instantiated
antes do transporte getsmtp
depois do transporte getsmtp
Erro falha
javax.mail.MessagingException: Could not connect to SMTP host: mail.host.com.br, port: 25;
nested exception is:
java.io.IOException: Couldn’t connect using “javax.net.ssl.SSLSocketFactory” socket factory to host, port: mail.cimm.com.br, 25; Exception: java.lang.reflect.InvocationTargetException
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:911)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:158)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at SMTPSend.msgsend(SMTPSend.java:65)
at SMTPSend.main(SMTPSend.java:22)
Alguem sabe oq ta errado?
Desde ja agradeço
Arthur Pereira