Reputation: 23
I am working on a final project for one of my classes and this program is meant to send emails to address in the code. I know how most of the code works, just having trouble understanding the password authentication and how to connect to SMTP servers and using specific ports. The problem with the code is it's not sending the email when run, and not giving any error messages. Any help would be much appreciated. Here's the code.
package application;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
public static void main (String [] args) {
String host="smtp.gmail.com";
final String user="[email protected]";
final String password="password";
String to="targetemail.com";
//imported code
Properties props = new Properties();
props.put("mail.smtp.socketfactory.port", "465");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//imported code
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Dwight from the future");
message.setText("At 8:00, someone poisons the coffee. Do NOT drink
it.");
Transport.send(message);
System.out.println("message sent!");
}
catch (MessagingException mex)
{
System.out.println("Error: unable to send message....");
mex.printStackTrace();
}
}
}
Upvotes: 2
Views: 5548
Reputation: 711
Please try the below code (modified the port value).
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Main {
public static void main(String[] args) {
String host="smtp.gmail.com";
final String user="[email protected]";
final String password="*********";
String to="[email protected]";
//imported code
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", user);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//imported code
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Dwight from the future");
message.setText("At 8:00, someone poisons the coffee. Do NOT drinkit.");
Transport.send(message);
System.out.println("message sent!");
}
catch (MessagingException mex)
{
System.out.println("Error: unable to send message....");
mex.printStackTrace();
}
}
}
Upvotes: 0
Reputation: 1539
I think the port value should be as below
props.put("mail.smtp.port", "587");
example configuration
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "*");
Upvotes: 1