Oct 12

Googles Gmail SMTP server supports the use of TLS (Transport Layer Security). When using a Gmail SMTP server with Javamail we can configure Javamail to use TLS using the session properties.

	    Properties props = new Properties();
	    props.put("mail.transport.protocol", "smtp"); 
	    props.put("mail.smtp.starttls.enable", "true"); 
	    props.put("mail.smtp.host", "smtp.gmail.com"); 
	    props.put("mail.smtp.auth", "true"); 
    // use your gmail account username here
	    props.put("mail.smtp.user", "username@gmail.com");
	    props.put("mail.smtp.port", "465");
	    props.put("mail.mime.charset", "ISO-8859-1");
	    props.put("mail.smtp.socketFactory.port", "465");
	    props.put("mail.smtp.socketFactory.fallback", "false");
	    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

You’ll also need to specify an Authenticator when creating the session.

You can then use the session to send your mail.

Putting it all together we get this


// Create session
    
Properties sessionProperties = System.getProperties();
props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.auth", "true"); 
// use your gmail account username here
props.put("mail.smtp.user", "username@gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.mime.charset", "ISO-8859-1");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = 
   Session.getDefaultInstance(sessionProperties, new PasswordAuthenticator());
    
// Create message
    
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, 
   InternetAddress.parse(to, false));
message.setSubject(subject);
message.setText(body);

// Send the message

Transport.send(message); 

written by objects \\ tags: , , , , , ,

Mar 27

To use anonymous access for an LDAP search or query set the value of Context.SECURITY_AUTHENTICATION to “none” in the environment used to create the initial context.

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, providerUrl);

// Use 'none' for anonymous access
env.put(Context.SECURITY_AUTHENTICATION, "none");

// Create the initial context
DirContext ctx = new InitialDirContext(env);

written by objects \\ tags: , , ,

Mar 06
import java.util.Hashtable;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class SimpleLdapAuthentication
{
	public static void main(String[] args)
	{
		String username = "user";
		String password = "password";
		String base = "ou=People,dc=objects,dc=com,dc=au";
		String dn = "uid=" + username + "," + base;
		String ldapURL = "ldap://ldap.example.com:389";

		// Setup environment for authenticating
		
		Hashtable<String, String> environment = 
			new Hashtable<String, String>();
		environment.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		environment.put(Context.PROVIDER_URL, ldapURL);
		environment.put(Context.SECURITY_AUTHENTICATION, "simple");
		environment.put(Context.SECURITY_PRINCIPAL, dn);
		environment.put(Context.SECURITY_CREDENTIALS, password);

		try
		{
			DirContext authContext = 
				new InitialDirContext(environment);
			
			// user is authenticated
			
		}
		catch (AuthenticationException ex)
		{
			
			// Authentication failed

		}
		catch (NamingException ex)
		{
			ex.printStackTrace();
		}
	}
}

written by objects \\ tags: , , ,