- import java.util.*;
- import java.text.*;
- //import javax.ejb.*;
- import javax.mail.*;
- //import javax.activation.*;
- import javax.mail.internet.*;
- import javax.naming.*;
-
- public class MailerBean {
-
- private static final String mailer = "JavaMailer";
-
- // bean attributes
- private String _mailSessionName = "";
- private String _text ="Default message.\nIf you see this message, please contact web applications administrator.";
- private String _subject="Default subject";
- private String _recipient="";
- private String _from="";
-
- // Constructor
- public MailerBean() {}
-
- // Setters
- public void setText(String theText) { _text=theText; }
- public void setSubject(String theSubject) { _subject=theSubject; }
- public void setRecipient(String theRecipient) { _recipient=theRecipient; }
- public void setFrom(String theFrom) { _from=theFrom; }
- public void setMailSessionName(String newMailSessionName) { _mailSessionName = newMailSessionName; }
-
- // Getters
- public String getText() { return _text; }
- public String getSubject() { return _subject; }
- public String getRecipient() { return _recipient; }
- public String getFrom() {return _from; }
- public String getMailSessionName() { return _mailSessionName; }
-
- // Methods
- public boolean sendMessage() {
-
- try {
- Context initial = new InitialContext();
- Session session = (Session) initial.lookup(_mailSessionName);
-
- DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
- Date timeStamp = new Date();
-
- Message msg = new MimeMessage(session);
-
- // recuperation des informations a envoyer
- msg.setFrom(new InternetAddress(this.getFrom()));
- msg.setHeader("X-Mailer", mailer);
- msg.setSentDate(timeStamp);
- msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.getRecipient() , false));
- msg.setSubject(this.getSubject());
-
- msg.setText(this.getText() );
- // envoie du mail
- Transport.send(msg);
-
- System.out.println("Mail sent");
- return true;
- } catch(Exception e) {
- System.out.println("Exception occured in mailer bean : " + e.getMessage() );
- return false;
- }
- }
-
-
-
-
- }
import java.util.*;
import java.text.*;
//import javax.ejb.*;
import javax.mail.*;
//import javax.activation.*;
import javax.mail.internet.*;
import javax.naming.*;
public class MailerBean {
private static final String mailer = "JavaMailer";
// bean attributes
private String _mailSessionName = "";
private String _text ="Default message.\nIf you see this message, please contact web applications administrator.";
private String _subject="Default subject";
private String _recipient="";
private String _from="";
// Constructor
public MailerBean() {}
// Setters
public void setText(String theText) { _text=theText; }
public void setSubject(String theSubject) { _subject=theSubject; }
public void setRecipient(String theRecipient) { _recipient=theRecipient; }
public void setFrom(String theFrom) { _from=theFrom; }
public void setMailSessionName(String newMailSessionName) { _mailSessionName = newMailSessionName; }
// Getters
public String getText() { return _text; }
public String getSubject() { return _subject; }
public String getRecipient() { return _recipient; }
public String getFrom() {return _from; }
public String getMailSessionName() { return _mailSessionName; }
// Methods
public boolean sendMessage() {
try {
Context initial = new InitialContext();
Session session = (Session) initial.lookup(_mailSessionName);
DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
Date timeStamp = new Date();
Message msg = new MimeMessage(session);
// recuperation des informations a envoyer
msg.setFrom(new InternetAddress(this.getFrom()));
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(timeStamp);
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.getRecipient() , false));
msg.setSubject(this.getSubject());
msg.setText(this.getText() );
// envoie du mail
Transport.send(msg);
System.out.println("Mail sent");
return true;
} catch(Exception e) {
System.out.println("Exception occured in mailer bean : " + e.getMessage() );
return false;
}
}
}