james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Danny Angus" <da...@apache.org>
Subject RE: mail client bean that does auth
Date Mon, 03 Feb 2003 16:31:46 GMT
Jason,
> I'm looking a for bean that can send mail to an smtp server that handles
> authorization

To be honest I've never needed to do this, but I suspect that your best bet would be to use
Java Mail, I've quickly run through relevant parts of James code and put together a method
outlining the key steps involved, I make no representations as to its fitness for any purpose
;-) but I hope its a good clue. As with HTTP AUTH, SMTP AUTH is complicated by having to agree
to use the strongest common auth encryption with the server, so most simple SMTP clients probably
sidestep the issue altogether.

Be warned that while James uses the default mail session with no authenticator, doing this
will allow other apps in the same JVM to access your usernames and passwords.

d.

----------------------------------------------------------------
package uk.co.killerbees.mailets;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author $author$
 * @version $Revision$
 */
public class JavaMailSendExample {
    /**
     * Example code. WYSIWYG
     *
     * @param args .
     */
    public static void main(String[] args) {
        try {
            new JavaMailSendExample().send();
        } catch (AddressException e) {
		//ignore for the sake of readability
        } catch (MessagingException e) {
		//ignore for the sake of readability
        }
    }


    public void send() throws AddressException,MessagingException {
        // first some basic information about our message
        String user            = "danny";
        String password        = "passme";
        String sender          = "sender@domain.tld";
        String destinationHost = "127.0.0.1";

        // next
        // set some system defaults, not really needed for this example
        // as we will be actively setting all the relevant properties
	  // but safer to set something than nothing at all

        Properties mailProps = new Properties();
        mailProps.put("mail.smtp.user",sender);
        mailProps.put("mail.smtp.from",sender);

        // technically this should refer to your default upstream SMTP relay
        mailProps.put("mail.host",destinationHost);

        // this is the URL of the machine we want to connect to to deliver our mail:
        URLName destinationURL = new URLName("smtp://"+destinationHost);

        // now lets get the javamail default instance
        Session session = Session.getDefaultInstance(mailProps);

        // tell the session what credentials to use for this host
        session.setPasswordAuthentication(destinationURL,new PasswordAuthentication(user,password));

        // create a simple mail message
        MimeMessage message = new MimeMessage(session);
        message.setRecipients(Message.RecipientType.TO,"recipient@domain.tld");
        message.setSender(new InternetAddress(sender));
        message.setSubject("Oh la la, its an email");
        message.setText("This is the body");

        // get the transporter to our host, this will be responsible for calling 
	  // the PasswordAuthenticator we passed to the session to get the credentials
	  // for this URL
        Transport trans = session.getTransport(destinationURL);

        // connect and send
        trans.connect();
        trans.send(message);
        trans.close();
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: james-dev-help@jakarta.apache.org


Mime
View raw message