?
Solved

how to send email with gmail using javamail

Posted on 2008-02-06
3
Medium Priority
?
887 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:aquasw
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20831157
0
 

Author Comment

by:aquasw
ID: 20839641
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
 

Accepted Solution

by:
aquasw earned 0 total points
ID: 20892384
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month6 days, 20 hours left to enroll

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question