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 = "smtp.gmail.com";
            final String SMTP_PORT = "465";
            final String emailMsgTxt = "Test Message Contents";
            final String emailSubjectTxt = "A test from gmail";
            final String emailFromAddress = "mygmail@gmail.com";
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            final String[] sendTo = { "toaddress@hotmail.com };

            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

            boolean debug = true;

            Properties props = new Properties();
            props.put("mail.smtp.host", 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");
                  }
            });

            session.setDebug(debug);

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

            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");
            Transport.send(msg);


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: java.io.FileNotFoundException: 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/javamail.default.address.map
DEBUG: URL jar:file:/C:/work/dir/mail/lib/smtp.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded resource: jar:file:/C:/work/dir/mail/lib/smtp.jar!/META-INF/javamail.address.map
DEBUG: not loading file: C:\Program Files\Java\jre1.6.0_03\lib\javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.6.0_03\lib\javamail.address.map (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 "smtp.gmail.com", port 465, isSSL false

Thanks
aquaswAsked:
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.

Ajay-SinghCommented:
0
aquaswAuthor Commented:
Hi

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 ?
0
aquaswAuthor Commented:
Hi

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 = "myUserName@comp.com";

            // Setup mail server
            props.put("mail.smtp.host", 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
            Transport.send(message);

      }
0

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.