Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

smtps and javamail

 
Michal Palec
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have problem with sending emails via smtps..This is debug info:

DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
t javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at mail.Send.send(Send.java:99)
at mail.MainFrame.jBu_Send_actionPerformed(MainFrame.java:327)
at mail.Frame_jBu_Send_actionAdapter.actionPerformed(MainFrame.java:444)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:452)
at java.net.Socket.connect(Socket.java:402)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 33 more

And this code of one of classes in my program:

package mail;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.swing.*;
import java.util.*;
import java.io.*;

public class Send {
public Send(){
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

public static void send(String smtpServer, String user, String pass, String to, String from, String subject, String body, File[] files){
try{
Properties props = System.getProperties();
Authenticator auth = new MyAuthenticator();
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.host", smtpServer);
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setHeader("X-Mailer", "PastelMail v.1.0");
msg.setSentDate(new Date());

if (files!=null){
Multipart multi = new MimeMultipart();
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setText(body);
multi.addBodyPart(bodyPart);
for (int i=0; i<files.length; i++){
bodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(files[i].getAbsolutePath());
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName(files[i].getName());
multi.addBodyPart(bodyPart);
}
msg.setContent(multi);
}else{
msg.setText(body);
}

msg.saveChanges();

if (user!=null && pass!=null){
Transport transport = session.getTransport("smtps");
transport.connect(smtpServer, user, pass);
JOptionPane.showMessageDialog(null, smtpServer+" "+user+" "+pass+" "+transport.isConnected());
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}else
// -- Send the message --

try{
Transport.send(msg);
}catch(SendFailedException ex){
Address[] ad = ex.getInvalidAddresses();
String s = "Bad adresses:\n";
for (int i=0; i<ad.length; i++){
s+=ad[i].toString();
s+="\n";
s+=ex.getMessage();
}
JOptionPane.showMessageDialog(null, s, "Error", JOptionPane.ERROR_MESSAGE);
}
JOptionPane.showMessageDialog(null, "Mail sent OK", "Sent information", JOptionPane.INFORMATION_MESSAGE);

}
catch (Exception ex)
{
ex.printStackTrace();
}
}

public static void send(String smtpServer, String user, String pass, String to, String from, String subject, String body){
send(smtpServer, user, pass, to, from, subject, body, null);
}

private void jbInit() throws Exception {
}
}

I tried to add also:

props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.ssl", "true");
props.put("mail.smtps.starttls.enable","true");
props.put("mail.smtps.socketFactory.port", "25");
props.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtps.socketFactory.fallback", "false");

but it's not helipg Maybe someone could help me please. Best regards.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have an SMTP server running locally?
 
Michal Palec
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I have account created in o2.pl and I connect to their smtp server. That is why I don't understand this probe to connecto to localhost :/
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, that is odd. What value do you pass in to your own send method for smtpServer?
 
Michal Palec
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The address of smtp server. It deepend of the account but e.g. smtp.o2.pl. It works great when I use smtp not smtps protocol :/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic