Results 1 to 16 of 16
  1. #1
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default Error in forwading file to mail...

    hello friends,

    i am making one application. in that i have to forward file to mail through attechment. i have taken one example code but its not working properlly.

    code is


    import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*;

    public class AttachExample {

    public static void main (String args[]) throws Exception {
    String host = "smtp.kar.com";
    String from = "FromAddre@kar.com";
    String to[] = new String[]{"ABC@kar.com","XYZ@kar.com"};
    String filename = "AttachFile.txt";
    // Get system properties
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);

    Session session = Session.getInstance(props, null);
    System.out.println(session.getProperties());

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));

    InternetAddress[] toAddress = new InternetAddress[to.length];
    for (int i = 0; i < to.length; i++)
    toAddress[i] = new InternetAddress(to[i]);
    message.setRecipients(Message.RecipientType.TO, toAddress);
    message.setSubject("Hello JavaMail Attachment");
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText("Here's the file");
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);
    message.setContent(multipart);
    try{
    Transport.send(message);
    }
    catch(SendFailedException sfe)
    {
    message.setRecipients(Message.RecipientType.TO, sfe.getValidUnsentAddresses());
    Transport.send(message);

    }
    }

    }


    i am getting followig error::::


    init:
    deps-module-jar:
    deps-ear-jar:
    deps-jar:
    compile-single:
    run-main:
    Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
    nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTra nsport.java:1391)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SM TPTransport.java:412)
    at javax.mail.Service.connect(Service.java:288)
    at javax.mail.Service.connect(Service.java:169)
    at javax.mail.Service.connect(Service.java:118)
    at javax.mail.Transport.send0(Transport.java:188)
    at javax.mail.Transport.send(Transport.java:118)
    at AttachExample.main(AttachExample.java:49)
    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at com.sun.mail.util.SocketFetcher.createSocket(Socke tFetcher.java:233)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFe tcher.java:189)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTra nsport.java:1359)
    ... 7 more
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)



    can anybody help me

    thanks in advance...
    Rakesh Mehta

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Go to GMail's web site and review their documentation. Many SMTP services don't accept connections on port 25, and I think gmail may required SSL/TLS. They seem pretty good about describing their requirements, including ports.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default Reply

    hello
    if anybody knows some other code for that then plz give me ...
    Rakesh Mehta

  5. #5
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default reply

    Hello friends,

    As you told gmail needs SSL/TLS for connections. i change my coding now its not giving error of port.. but another error

    new code ::
    ================================================== ==
    import java.util.*;
    import java.io.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;

    public class AttachExample {

    public static void main (String args[]) throws Exception {
    String host = "mail.gmail.com";
    String from = "abc@gmail.com";
    String to[] = new String[]{"abc@abc.com","abc@abc.com"};
    String filename = "C:\\Documents and Settings\\Desktop\\trick.txt";
    System.out.print("Step 1:: ");
    // Get system properties
    Properties props = System.getProperties();
    props.put("mail.smtp.host","mail.gmail.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    // added below line
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    System.out.println("Step 2:: ");
    Session session = Session.getDefaultInstance(props, null);
    //Session session = Session.getInstance(props, null);
    //final String username = "abc";
    // final String password = "abc";
    // Session session = Session.getDefaultInstance(props, new Authenticator(){protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password); }});
    System.out.println(session.getProperties());
    System.out.println("Step 3:: ");
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));

    InternetAddress[] toAddress = new InternetAddress[to.length];
    for (int i = 0; i < to.length; i++)
    toAddress[i] = new InternetAddress(to[i]);
    message.setRecipients(Message.RecipientType.TO, toAddress);
    message.setSubject("Hello JavaMail Attachment");
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText("Here's the file");
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);
    System.out.println("Step 4:: ");
    message.setContent(multipart);
    try{
    Transport transport = session.getTransport("smtp");
    System.out.println("Step 5:: in try");
    transport.connect("smtp.gmail.com","abc","abc");
    System.out.println("Step 6:: in try before mail:");

    message.saveChanges();
    Transport.send(message);
    System.out.println("Step 7:: in try after mail:");
    }
    catch(SendFailedException sfe)
    {
    message.setRecipients(Message.RecipientType.TO, sfe.getValidUnsentAddresses());
    Transport.send(message);

    }
    }

    }

    output and error is::
    ================================================== ===
    Step 1:: Step 2::
    {java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=..........
    .........
    ........

    bugreport.cgi, mail.smtp.host=mail.gmail.com, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.desktop=windows, sun.cpu.isalist=}
    Step 3::
    Step 4::
    Step 5:: in try
    Step 6:: in try before mail:
    Exception in thread "main" javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:319)
    at javax.mail.Service.connect(Service.java:169)
    at javax.mail.Service.connect(Service.java:118)
    at javax.mail.Transport.send0(Transport.java:188)
    at javax.mail.Transport.send(Transport.java:118)
    at AttachExample.main(AttachExample.java:69)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 8 seconds)


    so any suggestion ??




    thank for reply..
    Rakesh Mehta

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In simply for me, authentication fail, as the exceptions shows there. How did you authenticate it?

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Glancing at your code, it appears you commented out the code where you specify the user name and password in the Properties instance. That would certainly lead to an authentication error ;-)

    The real question is, what do you put in these fields?

    First, gmail won't let you use their service without creating an account, which seems reasonable. Create a gmail account, and use the user name and password from that.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And one more thing too, to our thread starter. Please use the code tags when you posting code segments again. Un-formated codes are hard to read lol.

  9. #9
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default Thanks to all..

    Hey friend ,

    thanks to all.
    also i will mention code tags from next time.
    Rakesh Mehta

  10. #10
    sp_29 is offline Member
    Join Date
    Feb 2009
    Location
    philippines
    Posts
    16
    Rep Power
    0

    Default is that you?

    ;)
    ;)
    .................................................. .................................................. .......

  11. #11
    sp_29 is offline Member
    Join Date
    Feb 2009
    Location
    philippines
    Posts
    16
    Rep Power
    0

    Default

    it seems that you are great.....

  12. #12
    sp_29 is offline Member
    Join Date
    Feb 2009
    Location
    philippines
    Posts
    16
    Rep Power
    0

    Default

    steve11235
    ...
    you seems great....

  13. #13
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default

    thanks 4 that also...

    :cool:
    Rakesh Mehta

  14. #14
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default

    :)............
    Rakesh Mehta

  15. #15
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    Rakesh is it working now ?
    i am the future

  16. #16
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default code is working fine

    yes rjuyal

    it working fine ...
    having some problem when we r sending pdf file but that is because of its size .. i will solve that problem.. main problem was wth line

    props.put("mail.smtp.host","mail.gmail.com");


    in that i have to write props.put("mail.smtp.host","smtp.gmail.com");

    ....and authentication..

    Session session = Session.getDefaultInstance(props, new Authenticator(){protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password); }});


    now code is working...:)
    Rakesh Mehta

Similar Threads

  1. javamail exception: 557 mail head error
    By allanwakes in forum Java Servlet
    Replies: 2
    Last Post: 10-02-2008, 11:05 AM
  2. error in reading mail contents
    By kib_tse in forum Networking
    Replies: 1
    Last Post: 08-22-2008, 12:14 AM
  3. Replies: 2
    Last Post: 05-05-2008, 06:01 AM
  4. Sending a mail with the local mail program
    By thedude in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2007, 12:19 PM
  5. Mail error
    By orchid in forum New To Java
    Replies: 1
    Last Post: 04-22-2007, 02:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •