1

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 = "parisa.roodheleh@gmail.com";
    String from = "parisainfinity@yahoo.com";
    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?

phts
  • 3,889
  • 1
  • 19
  • 31
8infinity8
  • 21
  • 7
  • 1
    Do u have any SMTP server installed in ur local machine?Check whether it is up and running or not.Seems u r trying to send an Email via Gmail SMTP server then ur host will be **smtp.gmail.com** and port will be **587**. – Bacteria Jun 23 '15 at 18:27
  • emmm, sry but how can I check?and then, how should I install it? what should I google? – 8infinity8 Jun 23 '15 at 18:43
  • well, i guss it's not just about SMTP sever, here is another error: at javax.mail.Service.connect(Service.java:364) – 8infinity8 Jun 23 '15 at 18:46

2 Answers2

1

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.

Community
  • 1
  • 1
GayashanNA
  • 478
  • 12
  • 22
  • tnx, ur answer make me another question, how this code can work with an account with no passwords? if this works, so everyone can use ur email address sending everything to others. – 8infinity8 Jun 23 '15 at 18:52
  • It doesn't work without the password. If you refer the link that i have provided you will see that the password (in addition to the correct host, port, and user) needs to be provided in order to get the correct solution. – GayashanNA Jun 23 '15 at 18:58
  • well, instead of doing this i replace that yahoo address with a gmail address, but steal the errors are the same. – 8infinity8 Jun 23 '15 at 19:09
  • It's not an issue with the smtp provider (gmail or yahoo), instead an issue with your configuration of using the api. Please make sure that you are using proper settings as mentioned in the referred answer. – GayashanNA Jun 23 '15 at 19:12
  • ok... I test the complete code in your link, and again it give me errors like: javax.mail.MessagingException: Could not convert socket to TLS; – 8infinity8 Jun 23 '15 at 19:23
  • 1
    wow... it works!!! tnx! now i will discover whats that code about... tnx tnx tnx tnxxx. but when i check senders account, there's nothing in the sent box.how? – 8infinity8 Jun 23 '15 at 19:34
  • @8infinity8 Glad i could help. Please accept this answer if it provides the correct solution to your question. – GayashanNA Jun 24 '15 at 03:46
0

check if your port 25 is block or not by :

https://mediatemple.net/community/products/dv/204404564/checking-your-outgoing-mail-server-(is-port-25-blocked)

Jegg
  • 549
  • 3
  • 11
  • I typed telnet example.com 25 in my cmd and the result was : 'telnet' is not recognized as an internal or external command, operable program or batch file. – 8infinity8 Jun 23 '15 at 19:06