Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems on sending mail on Websphere Application Server Community Edition

843830Apr 21 2007 — edited Apr 21 2007
Hi, i am using the following class for sending mail:-
/**This class encapsulates mailing to any person. it defines method that takes parametres
 * such as mail server address,sender and recipient address, userid and password of the 
 *  SMTP account
 **/
package src;


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


public class Mailer extends javax.mail.Authenticator{
/** The javamail session object. */
protected Session session;
/** The sender's email address */
protected String from;
/**the Username of the account used for authentication*/
protected String user;
/**The password of the mail account*/
protected String password;
/** The subject of the message. */
protected String subject;
/** The recipient ("To:"), as Strings. */
protected ArrayList toList = new ArrayList( );
/** The CC list, as Strings. */
protected ArrayList ccList = new ArrayList( );
/** The BCC list, as Strings. */
protected ArrayList bccList = new ArrayList( );
/** The text of the message. */
protected String body;
/** The SMTP relay host */
protected String mailHost;
/** Get from */
public String getFrom( ) {
return from;
}
/** Set from */
public void setFrom(String fm) {
from = fm;
}
/** Get username */
public String getUser( ) {
return user;
}
/** Set username */
public void setUser(String us) {
user=us;
}

/** Set password*/
public void setPassword(String password){
    this.password=password;
}
/**Get Password*/
public String getPassword(){
    return password;
}
/** Get subject */
public String getSubject( ) {
return subject;
}
/** Set subject */
public void setSubject(String subj) {
subject = subj;
}
// SETTERS/GETTERS FOR TO: LIST
/** Get tolist, as an array of Strings */
public ArrayList getToList( ) {
return toList;
}
/** Set to list to an ArrayList of Strings */
public void setToList(ArrayList to) {
toList = to;
}

public void setToList(String s) {
toList = tokenize(s);
}
/** Add one "to" recipient */
public void addTo(String to) {
toList.add(to);
}
// SETTERS/GETTERS FOR CC: LIST
/** Get cclist, as an array of Strings */
public ArrayList getCcList( ) {
return ccList;
}
/** Set cc list to an ArrayList of Strings */
public void setCcList(ArrayList cc) {
ccList = cc;
}
/** Set cc as a string like "tom, mary, robin@host". Loses any
* previously-set values. */
public void setCcList(String s) {
ccList = tokenize(s);
}
/** Add one "cc" recipient */
public void addCc(String cc) {
ccList.add(cc);
}
// SETTERS/GETTERS FOR BCC: LIST
/** Get bcclist, as an array of Strings */
public ArrayList getBccList( ) {
return bccList;
}
/** Set bcc list to an ArrayList of Strings */
public void setBccList(ArrayList bcc) {
bccList = bcc;
}
/** Set bcc as a string like "tom, mary, robin@host". Loses any
* previously-set values. */
public void setBccList(String s) {
bccList = tokenize(s);
}
/** Add one "bcc" recipient */
public void addBcc(String bcc) {
bccList.add(bcc);
}
// SETTER/GETTER FOR MESSAGE BODY
/** Get message */
public String getBody( ) {
return body;
}
/** Set message */
public void setBody(String text) {
body = text;
}

/** Check if all required fields have been set before sending.
* Normally called e.g., by a JSP before calling doSend.
* Is also called by doSend for verification.
*/
public boolean isComplete( ) {
if (from == null || from.length( )==0) {
System.err.println("doSend: no FROM");
return false;
}
if (subject == null || subject.length( )==0) {
System.err.println("doSend: no SUBJECT");
return false;
}
if (toList.size( )==0) {
System.err.println("doSend: no recipients");
return false;
}
if (body == null || body.length( )==0) {
System.err.println("doSend: no body");
return false;
}
if (mailHost == null || mailHost.length( )==0) {
System.err.println("doSend: no server host");
return false;
}
return true;
}
public void setServer(String s) {
mailHost = s;
}
/** Send the message.
*/
public synchronized void doSend( ) throws MessagingException {
if (!isComplete())
throw new IllegalArgumentException("doSend called before message was complete");
/** Properties object used to pass props into the MAIL API */
Properties props = new Properties( );
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.user",user);
props.put("mail.smtp.auth", "true");

if (session == null) {
session = Session.getDefaultInstance(props,null);

 // create a message
final Message mesg = new MimeMessage(session);
InternetAddress[] addresses;
// TO Address list
addresses = new InternetAddress[toList.size( )];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)toList.get(i));
mesg.setRecipients(Message.RecipientType.TO, addresses);
// From Address
mesg.setFrom(new InternetAddress(from));
// CC Address list
addresses = new InternetAddress[ccList.size( )];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)ccList.get(i));
mesg.setRecipients(Message.RecipientType.CC, addresses);
// BCC Address list
addresses = new InternetAddress[bccList.size( )];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)bccList.get(i));
mesg.setRecipients(Message.RecipientType.BCC, addresses);
// The Subject
mesg.setSubject(subject);
// Now the message body.
mesg.setText(body);
// Finally, send the message! 
new Thread( ) {
public void run( ) {
try {
String protocol = "smtp";
    
   Transport t = session.getTransport(protocol);
    try {
        t.connect(mailHost,user,password);
        t.sendMessage(mesg,mesg.getAllRecipients());
        
    } finally {
        t.close();
    }
} catch (MessagingException e) {
throw new IllegalArgumentException(
"Transport.send() threw: " + e.toString( ));
}
}
}.start( );
}
}
/** Convenience method that does it all with one call. */
public static void send(String mailhost,String recipient, String sender,String user,String password, String subject, String message) throws MessagingException {

Mailer m = new Mailer( );
m.setServer(mailhost);
m.addTo(recipient);
m.setFrom(sender);
m.setUser(user);
m.setPassword(password);
m.setSubject(subject);
m.setBody(message);
m.doSend( );
}
/** Convert a list of addresses to an ArrayList. This will work
* for simple names like "tom, mary@foo.com, 123.45@c$.com"
* but will fail on certain complex (but RFC-valid) names like
* "(harish, munish) <ian@darwinsys.com>".
* Or even "Ian Darwin <ian@darwinsys.com>".
*/
protected ArrayList tokenize(String s) {
ArrayList al = new ArrayList( );
StringTokenizer tf = new StringTokenizer(s, ",");
// For each word found in the line
while (tf.hasMoreTokens( )) {
// trim blanks, and add to list.
al.add(tf.nextToken().trim( ));
}
return al;
}
     
}
Then i am using the following code snippet in a servlet to send mail by using the static send function of the above class.
try {
            src.Mailer.send("smtp.mail.yahoo.co.in","gargyagotra@gmail.com","gaurav_s_pandey@yahoo.co.in","gaurav_s_pandey","tinGSPyahootin","Subject of Servlet Mail","This is the mail sent by servlet run on geronimo");
            out.println("Request sent Successfully!!!");
        } catch (MessagingException ex) {
            out.println("Error Sending Request!!! Check Network Settings!!!");
        }
My problem is that it is not working on Websphere ApplicationServer Community Edition ,but the same code and servlet is running and sending mails as desired on Apache Tomcat...

Can some one help me plzzz as i have to make it run on WAS CE only and not tomcat....
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 19 2007
Added on Apr 21 2007
3 comments
765 views