Jan Ulrich wrote:

Hello,
I'm trying to send a mail with a SMTP-Server, which needs SMTP-Auth.
The code attached to this posting works perfect if executed from a shell, but i need this function in a servlet running on a Tomcat 4.1 webcontainer.

The DEBUG in a shell says:
<<DEBUG SMTP: useEhlo true, useAuth true

The Tomcat debug however says:
<<DEBUG SMTP: useEhlo true, useAuth false

and the sending fails. It looks as if Tomcat does not support SMTP Auth. Is there a possibility to replace some Tomcat jars with jars from J2EE?

Any help is appreciated, thanks

Jan Ulrich


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

class sendMessageApp {
static String host = "mail.gmx.net";
static String user = "somebody";
static String password = "secret";
static boolean debug = true;
static String toAddress = "to@you";
static String fromAddress = "from@me";

public static void main(String[] argv) {
// set the host
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");
//create some properties and get the default session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);

try {
System.out.println("Getting Transport for smtp");
Transport tr = session.getTransport("smtp");
try {
tr.connect("mail.gmx.net",user, password);
}
catch (MessagingException ex) {
ex.printStackTrace();
System.exit(1);
}

// create a message
Message msg = new MimeMessage(session);

// set the from
InternetAddress from = new InternetAddress(fromAddress);
msg.setFrom(from);
InternetAddress[] address = {new InternetAddress(toAddress)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("This is a test");

msg.setContent("hello","text/plain");
msg.saveChanges();
System.out.println("Before sending...");
tr.sendMessage(msg, address);
}
catch (javax.mail.MessagingException mex) {
mex.printStackTrace();
}
}
}


--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

I played around with smtp_auth under tomcat 4.1.12 and got it to work.
 here is the key method I used which works for me...

Randy

/**
 * @author Randy Secrist
 *
 * This method is used for writing messages to an SMTP server using
 * SMTP authentication.  This uses a digest, and not TLS.
 */
public static void sendBytes(String uid, String pw, String to, String
from, String host, String msgtxt,
    String subject, boolean debug) throws MessagingException {

    // set some default parameters
    if (from == null) from = "[EMAIL PROTECTED]";
    if (host == null) host = smtpHost;
    if (msgtxt == null) msgtxt = "MessageBody";
    if (subject == null) subject = "Subject of Email";

    // Create the Java Mail Session
    // (uses mail.jar, smtp.jar, activation.jar)
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.auth", "true");
    Session session = Session.getInstance(props, null);
    session.setDebug(debug);

    try {
        // create a message
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);

        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText(msgtxt);

        // create the Multipart and its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);

        // add the Multipart to the message
        msg.setContent(mp);

        // set the Date: header
        msg.setSentDate(new Date());

        // authenticate & send the message
        Transport transport = session.getTransport("smtp");
        transport.connect(host, uid, pw);
        msg.saveChanges();
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
        transport = null;
    }
    catch (MessagingException mex) {
        mex.printStackTrace();
        Exception ex = null;
        if ((ex = mex.getNextException()) != null) {
            ex.printStackTrace();
        }
        throw mex;
    }
    finally {
        // Turn off authentication - or next non authenticated call will
fail.
        props.remove("mail.smtp.auth");
        session = Session.getInstance(props);
        session = null;
    }
}



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to