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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:25:02+00:00 2026-05-26T02:25:02+00:00

This is kind of a beginner question. For now I have the implementation like

  • 0

This is kind of a beginner question.
For now I have the implementation like this:

package org.minuteware.jgun;

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

class EmailNotifier {
    static Session connect(String host) {
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.from", "from@ex.com");
        Session session = Session.getInstance(props, null);

        return session;
    }

    static void send(String to, String subject, String body) {
        try {
        MimeMessage msg = new MimeMessage(connect("mail.ex.com"));
            msg.setRecipients(Message.RecipientType.TO, to);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            msg.setText(body);
            Transport.send(msg);
        } catch (MessagingException mex) {
            System.out.println("send failed, exception: " + mex);
        }
    }
    public static void main(String[] args) {
        send("to@ex.com", "test subj", "test body");
    }
}

What I’m stuck with is passing the host parameter to the send() method so that it could create a session.
Another option could be to have only one method and pass all the parameters to it. But that’s quite ugly.
In Python I could create the connection in the class constructor, and then use this connection with prefix self throughout all the other methods of the class. But I cannot find a way to do this is Java.

  • 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-26T02:25:03+00:00Added an answer on May 26, 2026 at 2:25 am

    Your mistake is that you did not make it the object-oriented way because you created static methods. Let’s say the caller of your class should look like this:

    public static void main(String[] args) {
        EmailNotifier en = new EmailNotifier("my.smtpserver", "sender@smtpserver");
        en.send("to@ex.com", "test subj", "test body");
        en.send("to@ex.com", "test subj", "test body");
        en.send("to@ex.com", "test subj", "test body");
    }
    

    Then your notifier may look similar to this:

    class EmailNotifier {
        private final Session session;
    
        public EmailNotifier(final String host, final String sender) {
            Properties props = new Properties();
            props.put("mail.smtp.host", host);
            props.put("mail.from", sender);
            this.session = Session.getInstance(props, null);
        }
    
        public void send(String to, String subject, String body) {
            try {
                MimeMessage msg = new MimeMessage(this.session);
                msg.setRecipients(Message.RecipientType.TO, to);
                msg.setSubject(subject);
                msg.setSentDate(new Date());
                msg.setText(body);
                Transport.send(msg);
            } catch (MessagingException mex) {
                System.out.println("send failed, exception: " + mex);
            }
        }
    }
    

    Edit: Apache commons provides a nice wrapper around javax.mail which you can find here.

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

Sidebar

Related Questions

this kind of follows on from another question of mine. Basically, once I have
I have ask this kind of question before, but it seems my previous question
This is kind of a beginner's question but the code I am looking at
I know questions of this kind have been asked before, but my situation differs
I have this kind of code in some applications (from microsoft) [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Naming,
I know this kind of questions have been asked already many times before. The
I am sure that this kind of questions must have been asked before, but
I have this kind of node in my xml document: <parent ...> <a .../>
Maybe kind of a stupid question, but i'm a beginner in jQuery and i'm
I know this is kind of a beginners question but my books aren't explaining

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.