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.
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:
Then your notifier may look similar to this:
Edit: Apache commons provides a nice wrapper around
javax.mailwhich you can find here.