[ JavaMail ] can't connect server
Hello everyone !
I want to send a E-mail via javaMail, but it doesn't work , the program can't connect the SMTP server ....
the debug message and source code is below, thanks for your time
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
connecting...
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;
publicclass SendMailWithSMTP{
private String host;
private String user;
private String password;
publicvoid setHost(String host){
this.host = host;
}
publicvoid setAccount(String user, String password){
this.user = user;
this.password = password;
}
publicvoid send(String from, String to, String subject, String content){
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
try{
MimeMessage message =new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Java Mail test");
message.setText(content);
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
System.out.println("connecting...");
transport.connect(host, user, password);
System.out.println("Sending message");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Send over");
}catch(MessagingException e){
e.printStackTrace(System.err);
}
}
publicstaticvoid main(String args[]){
SendMailWithSMTP sendMail =new SendMailWithSMTP();
sendMail.setAccount("test@gmail.com","password");
sendMail.setHost("smtp.gmail.com");
sendMail.send("test@gmail.com","tese@gmail.com","JavaMailTest","This is a test Mail");
}
}
Message was edited by:
Lesky

