send mail thru java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crossroadsk
    New Member
    • Nov 2006
    • 30

    send mail thru java

    <pre>
    Hi all,

    I'm new to Javamail concept and i got the following exceptions when i tried to send a simple mail.

    First exception is :
    using java Could not connect to SMTP host: smtp.gmail.com, port: 25;

    Second Exception is :
    java.net.Socket Exception: Software caused connection abort: connect

    i'm using Eudora as my mail client and its working fine.
    what could be the problem with port.

    Also,
    Does a mail client should necessarily exist on the local machine to send mail thru java?

    My code is like this:

    import java.util.Prope rties;
    import javax.mail.*;
    import javax.mail.inte rnet.*;

    public class SendMail
    {
    public static void main(String[] args)
    {
    if(args.length! =3)
    {
    System.out.prin tln("Usage: SendMail SMTPHost ToAddress FromAddress");
    System.exit(1);
    }

    String smtpHost = args[0];
    String to = args[1];
    String from = args[2];

    Properties props = System.getPrope rties();
    props.put("mail .smtp.host",smt pHost);
    props.put("mail .smtp.port","46 5");

    try{
    Session session = Session.getDefa ultInstance(pro ps, null);

    MimeMessage message = new MimeMessage(ses sion);
    message.setText ("Hi from the J2EE in 21 Days authors");
    message.setSubj ect("Hi!");
    message.setFrom (new InternetAddress (from));
    message.addReci pient(Message.R ecipientType.TO , new InternetAddress (to));

    Transport.send( message);
    System.out.prin tln("Message Sent");
    }
    catch(Messaging Exception me)
    {
    System.err.prin tln("Messaging Exception : "+me.getMessage ());
    }
    }
    }

    Please help me ............!
    </pre>
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by crossroadsk
    <pre>
    Hi all,

    I'm new to Javamail concept and i got the following exceptions when i tried to send a simple mail.

    First exception is :
    using java Could not connect to SMTP host: smtp.gmail.com, port: 25;

    Second Exception is :
    java.net.Socket Exception: Software caused connection abort: connect

    i'm using Eudora as my mail client and its working fine.
    what could be the problem with port.

    Also,
    Does a mail client should necessarily exist on the local machine to send mail thru java?

    My code is like this:

    import java.util.Prope rties;
    import javax.mail.*;
    import javax.mail.inte rnet.*;

    public class SendMail
    {
    public static void main(String[] args)
    {
    if(args.length! =3)
    {
    System.out.prin tln("Usage: SendMail SMTPHost ToAddress FromAddress");
    System.exit(1);
    }

    String smtpHost = args[0];
    String to = args[1];
    String from = args[2];

    Properties props = System.getPrope rties();
    props.put("mail .smtp.host",smt pHost);
    props.put("mail .smtp.port","46 5");

    try{
    Session session = Session.getDefa ultInstance(pro ps, null);

    MimeMessage message = new MimeMessage(ses sion);
    message.setText ("Hi from the J2EE in 21 Days authors");
    message.setSubj ect("Hi!");
    message.setFrom (new InternetAddress (from));
    message.addReci pient(Message.R ecipientType.TO , new InternetAddress (to));

    Transport.send( message);
    System.out.prin tln("Message Sent");
    }
    catch(Messaging Exception me)
    {
    System.err.prin tln("Messaging Exception : "+me.getMessage ());
    }
    }
    }

    Please help me ............!
    </pre>
    1.) Please use code tags when posting code [code=java]//code goes here[/code].You've been around long enough to have heard about them.
    Now about sending mail, Jos has already posted some code he uses for sending mail so you can try searching for that thread ...

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by crossroadsk
      props.put("mail .smtp.port","46 5");
      Try it again with the above line removed; most likely your smtp server doesn't
      listen to that port.

      kind regards,

      Jos

      Comment

      • tommybobby
        New Member
        • May 2007
        • 2

        #4
        Regarding the comment above, if you think the issue is your mail server, its a worthwhile excercise to see can you for example telnet to the smtp server on port 25, if you get connected it will rule that issue out.

        example: telnet mysmtp.server.h ost 25

        Comment

        Working...