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 6117727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:21:14+00:00 2026-05-23T15:21:14+00:00

The following is the program, which I am trying to send e-mail. The code

  • 0

The following is the program, which I am trying to send e-mail. The code is error free and I don’t get any run time exception. But the code is unable to send e-mail. I have revised this code a lot but can’t get what is actually wrong.
The sender and the receiver both have GMail accounts. The sender has 2-step verification process disabled. (I don’t think it matters for the receiver. Does it?)

The code :

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

class tester {
   public static void main(String args[]) {
      Properties props = new Properties();
      props.put("mail.smtp.host" , "smtp.gmail.com");
      props.put("mail.stmp.user" , "username"); // username or complete address ! Have tried both
      Session session  = Session.getDefaultInstance( props , null);
      String to = "me@gmail.com";
      String from = "from@gmail.com";
      String subject = "Testing...";
      Message msg = new MimeMessage(session);
         try {
           msg.setFrom(new InternetAddress(from));
           msg.setRecipient(Message.RecipientType.TO , new InternetAddress(to));
           msg.setSubject(subject);
           msg.setText("Working fine..!");
           System.out.println("fine!!??");
         }  catch(Exception exc) {
                System.out.println(exc);
            }
    }
}
  • 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-05-23T15:21:15+00:00Added an answer on May 23, 2026 at 3:21 pm

    First of all, you forgot to call Transport.send() to send your MimeMessage.

    Secondly, GMail needs to be configured to use TLS or SSL connection. The following needs to be added to your Properties (props):

    //To use TLS
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    
    //To use SSL
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    

    To connect to GMail SMTP, use the Transport.connect() method. I see you are not using any Transport at all in your code, so add this:

    Transport transport = session.getTransport();
    
    //Connect to GMail
    transport.connect("smtp.gmail.com", 465, "USERNAME_HERE", "PASSWORD_HERE");
    transport.send(msg);
    

    Alternatively, you can create a Session by including a javax.mail.Authenticator as a parameter.

    Example:

    Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("USERNAME_HERE", "PASSWORD_HERE");
            }
        });
    

    I hope this helps you.

    Resources:

    • Send email with SMTPS (eg. Google GMail) (Javamail)
    • JavaMail API – Sending email via Gmail SMTP example
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone, I am trying to run the following program, but am getting a
In my simple OpenGL program I get the following error about exit redefinition: 1>c:\program
As an experiment i am trying to write the following program which allows me
I'm getting following linker error when I compile my program with VS2008 solution which
let us have a situation in which the following program prints some 10 lines
If yes, on which operating system, shell or whatever? Consider the following Java program
Am trying to implement a Java program which sends an Apple Push Notification to
I'm trying to implement a program which runs a function limited by a fixed
I'm trying to compile this program under Windows (it's a program which turns bootable
I need to solve the following question which i can't get to work by

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.