Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:34:33+00:00 2026-06-11T14:34:33+00:00

I am building an email application in java, so far I tried using this,

  • 0

I am building an email application in java, so far I tried using this, I am getting no errors on running this code, but the problem after I compile and run the code is that the net beans IDE just shows me ,the program is running, but when I check the mailbox, there are no mails received as such. Can anyone explain me as why this is happening?

Also I’ve printed a message “successfully sent” message at the end, but somehow the message is not getting printed, I can’t figure out what the error is ,any help would be appreciated, thanks.

    public class Email 
    public static void main(String[] args) {
    // TODO code application logic here


    String[] to = {"pqr@gmail.com"};
    String from = "abc@gmail.com";
    String host = "smtp.gmail.com";
            String user_name = "abc@gmail.com";
            String password = "xyz";

    try{
    Properties properties = System.getProperties();
    properties.setProperty("smtp.gmail.com", "imaps");
            properties.put("smtp.starttls.enable", "true");
            properties.put("mail.smtp.host",host);
            properties.put("mail.smtp.user",user_name);
            properties.put("mail.smtp.password", password);
            properties.put("mail.smtp.port", "465");
            properties.put("mail.smtp.auth", "true");


            //properties.put
    Session session = Session.getDefaultInstance(properties,null);
            //Store store = session.getStore("imaps");



        MimeMessage message = new MimeMessage(session);
        // basically stores one or more addresses , whom the mail has to be       sent
        //InternetAddress[] send_to = { new InternetAddress(to) };
        //InternetAddress[] send_from = { new InternetAddress(from) };
                    InternetAddress[] toAddress = new InternetAddress[to.length];

                    for(int i = 0; i < to.length ;i++)
                    {
                        toAddress[i] = new InternetAddress(to[i]);
                    }

                    for(int i = 0 ; i < toAddress.length ; i++){
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
                    }

        message.setFrom(new InternetAddress(from));
        message.setSubject("Testing email");
        message.setText("this is a test");
                    Transport transport = session.getTransport("smtp");
                    transport.connect(host, user_name, password);
        transport.send(message);
                    transport.close();


                    System.out.println("Message successfully sent");


    }catch(Exception e){
        e.printStackTrace();
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:34:34+00:00Added an answer on June 11, 2026 at 2:34 pm

    Try this code

    public class SendMail {
    
        String host, port, emailid,username, password;
        Properties props = System.getProperties();
        Session l_session = null;
    
        public SendMail() {
            host = "smtp.mail.yahoo.com";
            port = "587";
            emailid = "a@yahoo.com";
            username = "a";
            password = "pwd";
    
            emailSettings();
            createSession();
            sendMessage("a@yahoo.com", "rahul@gmail.com","Test","test Mail");
        }
    
        public void emailSettings() {
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "false");
            props.put("mail.smtp.port", port);
    //        props.put("mail.smtp.socketFactory.port", port);
    //        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    //        props.put("mail.smtp.socketFactory.fallback", "false");
    
        }
    
        public void createSession() {
    
            l_session = Session.getInstance(props,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);
                        }
                    });
    
            l_session.setDebug(true); // Enable the debug mode
    
        }
    
        public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) {
            //System.out.println("Inside sendMessage 2 :: >> ");
            try {
                //System.out.println("Sending Message *********************************** ");
                MimeMessage message = new MimeMessage(l_session);
                emailid = emailFromUser;
                //System.out.println("mail id in property ============= >>>>>>>>>>>>>> " + emailid);
                //message.setFrom(new InternetAddress(emailid));
                message.setFrom(new InternetAddress(this.emailid));
    
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
                message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail));
                message.setSubject(subject);
                message.setContent(msg, "text/html");
    
                //message.setText(msg);
                Transport.send(message);
                System.out.println("Message Sent");
            } catch (MessagingException mex) {
                mex.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }//end catch block
            return true;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to send email notifications in my Java web application. I'm using Apache
I'm building a simple FB application but I'm getting a weird error. I have
I am working on inbuild email application in this application. I am getting error
I was building an simple application using spring roo. After doing certain job, I
I'm building a application that sends a test message to another email, the program
We're building a module for generating HTML for email newsletters. We've looked into using
I want to unit test a Java application that fetches mails from an email
I am building an iPhone application that generates an email with an attachment. The
I'm building a Rails application using Devise. Devise is all set up even omniauth.
I am building an intial-data.yml file to test my Play! application, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.