tinfx:
You need to autenticate to smtp servers for them to allow sending e-mails.
Please take the following example:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SendMail {
private String mailSMTPServer;
private String mailSMTPServerPort;
SendMail() {
mailSMTPServer = "smtp.gmail.com";
mailSMTPServerPort = "465";
}
SendMail(String mailSMTPServer, String mailSMTPServerPort) {
this.mailSMTPServer = mailSMTPServer;
this.mailSMTPServerPort = mailSMTPServerPort;
}
public void sendMail(String from, String to, String subject, String message) {
Properties props = new Properties();
/*
props.setProperty("proxySet","true");
props.setProperty("socksProxyHost","192.168.155.1");
props.setProperty("socksProxyPort","1080");
*/
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", mailSMTPServer);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", from);
props.put("mail.debug", "true");
props.put("mail.smtp.port", mailSMTPServerPort);
props.put("mail.smtp.socketFactory.port", mailSMTPServerPort);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SimpleAuth auth = null;
auth = new SimpleAuth ("login","password");
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(true);
Message msg = new MimeMessage(session);
try {
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setFrom(new InternetAddress(from));
msg.setSubject(subject);
msg.setContent(message,"text/plain");
} catch (Exception e) {
System.out.println(">> Error: Need to complete message");
e.printStackTrace();
}
Transport tr;
try {
tr = session.getTransport("smtp");
tr.connect(mailSMTPServer, "login", "password");
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
} catch (Exception e) {
System.out.println(">> Error: Send message");
e.printStackTrace();
}
}
}
class SimpleAuth extends Authenticator {
public String username = null;
public String password = null;
public SimpleAuth(String user, String pwd) {
username = user;
password = pwd;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (username,password);
}
}
I hope this helps
Best regards
Trade well and good luck
JL