how to send email with gmail using javamail

Hi Experts

I am trying to use javamail to send message via gmail

this is the code

      final String SMTP_HOST_NAME = "";
            final String SMTP_PORT = "465";
            final String emailMsgTxt = "Test Message Contents";
            final String emailSubjectTxt = "A test from gmail";
            final String emailFromAddress = "";
            final String SSL_FACTORY = "";
            final String[] sendTo = { " };


            boolean debug = true;

            Properties props = new Properties();
            props.put("", SMTP_HOST_NAME);
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "true");
            props.put("mail.smtp.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.put("mail.smtp.socketFactory.fallback", "false");

            Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

                  protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("mygmail_username", "mygmail_password");


            Message msg = new MimeMessage(session);
            InternetAddress addressFrom = new InternetAddress(/* from */emailFromAddress);

            InternetAddress[] addressTo = new InternetAddress[/* recipients */sendTo.length];
            for (int i = 0; i < /* recipients */sendTo.length; i++) {
                  addressTo[i] = new InternetAddress(/* recipients */sendTo[i]);
            msg.setRecipients(Message.RecipientType.TO, addressTo);

            // Setting the Subject and Content Type
            msg.setSubject(/* subject */emailSubjectTxt);
            msg.setContent(/* message */emailMsgTxt, "text/plain");

and this is the output

DEBUG: JavaMail version 1.4.1
DEBUG: not loading file: C:\Program Files\Java\jre1.6.0_03\lib\javamail.providers
DEBUG: C:\Program Files\Java\jre1.6.0_03\lib\javamail.providers (The system cannot find the file specified)
DEBUG: URL jar:file:/C:/work/dir/mail/lib/imap.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/work/dir/mail/lib/imap.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/C:/work/dir/mail/lib/pop3.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/work/dir/mail/lib/pop3.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/C:/work/dir/mail/lib/smtp.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/work/dir/mail/lib/smtp.jar!/META-INF/javamail.providers
DEBUG: not loading resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsy stems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsy stems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc]}
DEBUG: not loading resource: /META-INF/
DEBUG: URL jar:file:/C:/work/dir/mail/lib/smtp.jar!/META-INF/
DEBUG: successfully loaded resource: jar:file:/C:/work/dir/mail/lib/smtp.jar!/META-INF/
DEBUG: not loading file: C:\Program Files\Java\jre1.6.0_03\lib\
DEBUG: C:\Program Files\Java\jre1.6.0_03\lib\ (The system cannot find the file specified)
DEBUG: setDebug: JavaMail version 1.4.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "", port 465, isSSL false

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aquaswAuthor Commented:

I didn't succeed to send mail to gmail.
may be I have firewall problems here.
do you know how can I send email to exchange server ?
do you have example of code ?
aquaswAuthor Commented:

this is the code that worked for me to send email via the exchange server.

      private void sendEmail(String to) throws Exception {
            Properties props = System.getProperties();

            InternetAddress[] toArr = new InternetAddress[to.split(";").length];
            for (int i = 0; i < toArr.length; i++) {
                  toArr[i] = new InternetAddress(to.split(";")[i]);

            String mailHost = "my mail host";
            String mailFrom = "";

            // Setup mail server
            props.put("", mailHost);

            // Get session
            Session session = Session.getDefaultInstance(props, null);
            props.put("mail.debug", "true");
            // Define message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(mailFrom));

            message.addRecipients(Message.RecipientType.TO, toArr);

            message.setSubject("subject of the message");
            message.setText("text of the message");

            // Send message


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.