Results 1 to 15 of 15
  1. #1
    khushbu is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default JavaMail and Gmail

    Hi!

    The program of JavaMail to send email by using gmail SMTP server was working correctly in Windows 7. When I tested the program in Windows XP in a different machine, then the email is not going. Either Messaging or SendMail exception comes. I have used JDK1.6 and NetBeans 5.5 in both the machines.

    Kindly let me know whether the problem is because of change of OS (in which case some changes in coding may be needed) or some setting needs to be done in NetBeans, which was taken automatically in Windows 7.

    The code is=

    Java Code:
    /*
     * SendMail.java
     *
     * Created on 19 April, 2010, 10:14 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    package utility;
    
    import java.io.Console;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import java.util.*; 
    
    
    /**
     *
     * @author Khushbu
     * @version
     */
    public class SendMail {
        
        String username=null;
        String password=null;
        
        /** Creates a new instance of SendMail */
        public SendMail() {
        }
        
        public void postMail( String recipients[ ], String subject, String message, String from, String pwd) throws MessagingException
        {
             boolean debug = false;
    
             //Set the host smtp address
             Properties props = new Properties();
             //props.put("mail.smtp.host", "smtp.live.com");
             
             String host="smtp.gmail.com";
             props.put("mail.smtps.auth", "true");                       
    
             
         //    Authenticator a1= new PopupAuthenticator();
    
    
            // create some properties and get the default Session
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(debug);
    
            // create a message
            Message msg = new MimeMessage(session);
    
            // set the from and to address
            //InternetAddress addressFrom = new InternetAddress(from);
            //msg.setFrom(addressFrom);
    
            InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
            for (int i = 0; i < recipients.length; i++)
            {
                addressTo[i] = new InternetAddress(recipients[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);
    
            // Setting the Subject and Content Type
            msg.setSubject(subject);
            msg.setContent(message, "text/plain");
            //msg.setText(message);
            //Transport.send(msg);                    
            
            username = from;
            password = pwd;              
    
            Transport t = session.getTransport("smtps");
            try {
                t.connect(host, username, password);
                //t.connect(host, username);
                t.sendMessage(msg, msg.getAllRecipients());
            }         
            catch(Exception ex){
                         out.println(ex);
            }
            finally {
                t.close();
            }
    
        }
        
    }
    Last edited by khushbu; 05-21-2010 at 01:55 PM. Reason: code tags added

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

    Default

    Please use code tags when you posting again in the forum. Unformated codes are really hard to read. If you don't know how to do it, then please check on my forum signature. You can find the relevant link there.

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

  4. #4
    khushbu is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Yes, the exception generated is:

    javax.mail.MessagingException: can't determine local email address
    Last edited by khushbu; 05-21-2010 at 01:49 PM.

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

    Default

    Please complete the complete error message you comes with. What you've post above is part of it. Complete error message tells you a lot.

  6. #6
    khushbu is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Its the complete error message only that I have posted. This error message is displayed by following line:

    out.println(ex);

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

    Default

    Rather print just the error message print the stack trace. It'll gives you lots of useful information.

    Java Code:
    ex.printStackTrace();

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

    Default

    Here in the following line of code,

    Java Code:
    t.connect(host, username, password);
    check all the values at runtime. Simply add some print statement before that line and check those.

  9. #9
    khushbu is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks for your reply. I've already checked the values and are all correct. "host" has got the value smtp.gmail.com, "username" has correctly got my gmail email-id and "password" has also got the password of my gmail account. I even tried ex.printStackTrace() but its displaying no output. If I replace out.print(ex) with ex.printStackTrace(), then a blank page appears.

    I even added print statements after every line of code and it was seen that all the messages are displayed except the print statement just after t.connect() because the moment control comes to this line, exception is raised and it goes to the catch block.

    I think that the issue is not really related to the code but of some setting in Win XP. Kindly help me in this asap.
    Last edited by khushbu; 05-22-2010 at 03:13 PM.

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

    Default

    This is nothing to do with your OS.

    javax.mail.MessagingException: can't determine local email address
    It's all about the configurations you are using. I cannot test your complete code since I don't have those additional resources.

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

    Default

    Quote Originally Posted by khushbu View Post
    I even added print statements after every line of code and it was seen that all the messages are displayed except the print statement just after t.connect() because the moment control comes to this line, exception is raised and it goes to the catch block.
    I'm not clear what you've done here, can you show that part of your code?

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

    Default

    Is there any way that you can send me the complete code with all additional packages. So I can check it. Just looking at your code I cannot see any mistake you've done. Something wrong with the configurations.

  13. #13
    khushbu is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    The code is complete. This method postMail() will be called with an array containing list of recipients, subject of the email, message of the email, from email-id of gmail and password of from gmail account. The jar files required by this mail are javamail-1.4.3 and jaf-1.0.2 (JAF is required if jdk1.5 or lower versions are used), which could be easily downloaded from google. Kindly let me know the way to transfer these files to you, if desired.

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

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

Similar Threads

  1. error in smpt.gmail.com host
    By kib_tse in forum Networking
    Replies: 15
    Last Post: 03-10-2011, 07:25 PM
  2. Java and gmail
    By ak88 in forum Advanced Java
    Replies: 1
    Last Post: 03-25-2010, 06:05 PM
  3. error in connecting to gmail server
    By nyk976 in forum Networking
    Replies: 2
    Last Post: 03-10-2010, 03:36 PM
  4. Reading file attachments like gmail attachements
    By janu.c in forum New To Java
    Replies: 1
    Last Post: 08-05-2009, 08:07 AM
  5. 530 5.7.0 Authentication Required - JavaMail gmail
    By simon in forum Advanced Java
    Replies: 1
    Last Post: 07-15-2007, 12:52 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
  •