Reputation: 21
I'm a beginner in socket programming. So I can't solve my project's errors, I can't even understand where the problem is!
here is my code:
public static void main(String[] args) {
String to = "[email protected]";
String from = "[email protected]";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host",host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the subject Line!");
message.setText("This is autual message!");
Transport.send(message);
System.out.println("sent message successfully...");
}catch(MessagingException mex){
mex.printStackTrace();
}
}
and here is some errors:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2054)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
any idea?
Upvotes: 1
Views: 450
Reputation: 478
Seems like you are using an invalid smtp host since you are trying to send an email using a yahoo id. Try using smtp.mail.yahoo.com
as the host.
properties.setProperty("mail.smtp.host","smtp.mail.yahoo.com");
Although a bit different question, a similar solution is provided in Sending mail from yahoo id to other email ids using Javamail API.
Also please note that once you set this up, if you keep sending emails continuously, eventually (and quite soon), yahoo (or the smtp host) will terminate/hold the sender's account since you are spamming an account imitating the actions of a potential bot.
Upvotes: 1