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

  • Home
  • SEARCH
  • 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 6173765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:37:55+00:00 2026-05-23T23:37:55+00:00

I am writing a standalone code for Sending mail from java. In this program

  • 0

I am writing a standalone code for Sending mail from java. In this program I am taking all info by user on console . but here problem is with Authentication part.
I am passing user name and password which is actually mail id and passwrd of sender. but it is showing error that can.t refer to non final variable Password and From.

if I do it final then I can’t take it from user. Plz help me what should I do?

package mypackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
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;

public class SendMailSSL {
    public static void main(String[] args) throws IOException {
        Properties props = new Properties();
        String host="";
        String port="";
        String s_port="";
        String to ="";
        final String from="";
        final String password="";
        String subject="";
        String context="";
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("CONFIGURATION.... ");
        System.out.println("mail.smtp.host=");
        host = in.readLine();
        System.out.println("mail.smtp.socketfactoryport=");
        s_port=in.readLine();
        props.put("mail.smtp.host", host);
        props.put("mail.debug", "true");
        props.put("mail.smtp.socketFactory.port", s_port);
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        System.out.println("mail.smtp.port=");
        port=in.readLine();
        props.put("mail.smtp.port", port);

        System.out.println("AUTHENTICATION....");
        System.out.println("Username=");
        from=in.readLine();
        System.out.println("Password=");
        password = in.readLine();
        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator()
        {
                String from = "";
                String password="";
                protected PasswordAuthentication getPasswordAuthentication()
                {
                    return new PasswordAuthentication(from,password);
                }
            });

        try {
            System.out.println("Mail Sending Process..");
            System.out.println("To=");
            to=in.readLine();

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));
            System.out.println("Subject=");
            subject=in.readLine();
            message.setSubject(subject);
            System.out.println("Context=");
            context = in.readLine();
            message.setText(context);

            Transport.send(message);
            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            System.out.println("in catch blk");
            throw new RuntimeException(e);
        }
    }
}

Your help would be precious for me.
Thanks in advance.

  • 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-23T23:37:56+00:00Added an answer on May 23, 2026 at 11:37 pm

    So the thing about the final keyword is that it makes your variables write-once/read-only. It also means that inner classes can reference them. What you should do is modify the code that reads the info as follows:

    System.out.println("AUTHENTICATION....");
    System.out.println("Username=");
    final String from = in.readLine();
    System.out.println("Password=");
    final String password = in.readLine();
    

    Then remove the variable declarations in your anonymous Authenticator instance. It will now be reading the final variables we just declared in the outer code block. Make sense?

    By the way, are you more accustomed to programming in C? It looks like what’s fouling you up is trying to have all your variables declared before starting in on your logic, but that’s not at all necessary in java. In fact, it often makes your code harder to read!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing the code for the user authentication portion of a web site (including account
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
I writing a report in Visual Studio that takes a user input parameter and
When writing production-quality VC++ code, is the use of recursion acceptable? Why or why
Writing/reading code seems less stress then preparing a deploy scripts such as ./configure then
Writing some test scripts in IronPython, I want to verify whether a window is
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing my first Linq application, and I'm trying to find the best way to
When writing a switch statement, there appears to be two limitations on what you
When writing multi-threaded applications, one of the most common problems experienced are deadlocks. My

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.