Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: [Asking] Problem send email

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    30
    My Mood
    Cool
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default [Asking] Problem send email

    Hi all,

    I got the problem about send email If I used protocol besides smtp. I success if i was sending email with smtp protocol. But I failed if I was sending with POP, IMAP.
    It is the succed code if sending with smtp.
    import java.util.Properties;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
     
    /**
     *
     * @author user
     */
    public class SendEmail {
     
        public static void main(String[] args) {
            // Recipient's email ID needs to be mentioned.
            String to = "userTo@gmail.com";
     
            // Sender's email ID needs to be mentioned
            String from = "userFrom@gmail.com";
     
            // Assuming you are sending email from localhost
            String host = "gmail.com";
     
            // Get system properties
    //        Properties properties = System.getProperties();
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.debug", "false");
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.port", "465");
            // Setup mail server
    //        properties.setProperty("smtp.gmail.com", host);
     
            // Get the default Session object.
            Session session = Session.getDefaultInstance(properties, new Authenticator() {@Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("userFrom@gmail.com", "YourPassword");
            }});
     
            try {
                // Create a default MimeMessage object.
                MimeMessage message = new MimeMessage(session);
     
                // Set From: header field of the header.
                message.setFrom(new InternetAddress(from));
     
                // Set To: header field of the header.
                message.addRecipient(Message.RecipientType.TO,
                        new InternetAddress(to));
     
                // Set Subject: header field
                message.setSubject("This is the Subject Line!");
     
                // Now set the actual message
                message.setText("This is actual message");
     
                // Send message
                Transport.send(message);
                System.out.println("Sent message successfully....");
            } catch (MessagingException mex) {
                mex.printStackTrace();
            }
        }
    }

    I already search and found this link, but got error. The link contain these code below
    Session session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("IMAP");
    store.connect(host, username, password);
    Folder inbox = store.getFolder("INBOX");
    inbox.open(Folder.READ_WRITE);
    Message[] messages = inbox.getMessages( );

    But I got error about using that code.

    Please anyone know, share this info to me


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: [Asking] Problem send email

    I got error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Asking] Problem send email

    Your code is probably working correctly. POP3 and IMAP are used for receiving messages. SMTP is used to send messages.
    Last edited by djslavens; October 22nd, 2013 at 05:23 PM. Reason: spelling error

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    30
    My Mood
    Cool
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: [Asking] Problem send email

    Thanks for replay..

    Now I understand what pop3 and imap are. Last time I got error because I didn't add certificate to my keystore. Thanks Norm and djsiavens.

Similar Threads

  1. Problem connecting email-client
    By Mustelmia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2013, 06:01 PM
  2. Java Code Send email from email id configured in outlook
    By Hrithik in forum Java Theory & Questions
    Replies: 0
    Last Post: September 21st, 2012, 02:41 AM
  3. problem while sending email using org.apache.commons.mail.*
    By gurleen in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2012, 11:21 AM
  4. How to send a jsp page on to email
    By Rajiv in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 19th, 2011, 12:06 PM
  5. Java problem -- sending email via JSP page
    By java_beginner in forum Java Theory & Questions
    Replies: 2
    Last Post: June 28th, 2010, 02:35 AM