src/org/ohmage/util/MailUtils.java
public static void sendMailMessage(Session smtpSession, Message message) throws ServiceException {

		// If use Transport.send(), no need to save the message. 
		// Otherwise, save the message before sending.  
		try {
			message.saveChanges();
		}
		catch(MessagingException e) {
			throw new ServiceException(
					"Could not save the changes to the message.",
					e);
		}
		

		// Get the transport from the session.
		SMTPTransport transport;
		try {
			transport = 
					(SMTPTransport) smtpSession.getTransport(MAIL_PROTOCOL);
		}
		catch(NoSuchProviderException e) {
			throw new ServiceException(
					"There is no provider for SMTP. " +
						"This means the library has changed as it has built-in support for SMTP.",
					e);
		}

		Boolean auth = null;
		try {
			auth = StringUtils.decodeBoolean(
					PreferenceCache.instance().lookup(
						PreferenceCache.KEY_MAIL_AUTH));
		}
		catch(CacheMissException e) {
			// This is acceptable. It simply tells JavaMail to use the
			// default.
		}
		
		if((auth != null) && auth) {
			String mailUsername;
			try {
				mailUsername = 
						PreferenceCache.instance().lookup(
							PreferenceCache.KEY_MAIL_USERNAME);
			}
			catch(CacheMissException e) {
				throw new ServiceException(
					"The mail property is not in the preference table: " +
						PreferenceCache.KEY_MAIL_USERNAME,
					e);
			}
			
			String mailPassword;
			try {
				mailPassword = 
						PreferenceCache.instance().lookup(
							PreferenceCache.KEY_MAIL_PASSWORD);
			}
			catch(CacheMissException e) {
				throw new ServiceException(
					"The mail property is not in the preference table: " +
						PreferenceCache.KEY_MAIL_PASSWORD,
					e);
			}
			
			try {
				transport.connect(
						smtpSession.getProperty(MAIL_PROPERTY_HOST), 
						mailUsername, 
						mailPassword);
			}
			catch(MessagingException e) {
				throw new ServiceException(
						"Could not authenticate with or connect to the mail server.",
						e);
			}
		}
		else {
			try {
				transport.connect();
			}
			catch(MessagingException e) {
				throw new ServiceException(
						"Could not connect to the mail server.",
						e);
			}
		}
		

		try {
			transport.sendMessage(message, message.getAllRecipients());
		}
		catch(SendFailedException e) {
			throw new ServiceException(
					"Failed to send the message.",
					e);
		}
		catch(MessagingException e) {
			throw new ServiceException(
					"There was a problem while sending the message.",
					e);
		}
		
		try {
			transport.close();
		}
		catch(MessagingException e) {
			throw new ServiceException(
					"After sending the message there was an error closing the connection.",
					e);
		}
	}
/**
	 * Sends a mail message.
	 * 
	 * @param smtpSession The session used to create the message.
	 * 
	 * @param message The message to be sent.
	 * 
	 * @throws ServiceException There was a problem creating the connection to
	 * 							the mail server or sending the message.
	 */
Sends a mail message.

@param smtpSession The session used to create the message.

@param message The message to be sent.

@throws ServiceException There was a problem creating the connection to
the mail server or sending the message.
Sends a mail message . @param smtpSession The session used to create the message . @param message The message to be sent . @throws ServiceException There was a problem creating the connection to the mail server or sending the message .
public static void sendMailMessage(Session smtpSession, Message message) throws ServiceException {
		try {
			message.saveChanges();
		}
		catch(MessagingException e) {
			throw new ServiceException(
					"Could not save the changes to the message.",
					e);
		}
		SMTPTransport transport;
		try {
			transport = 
					(SMTPTransport) smtpSession.getTransport(MAIL_PROTOCOL);
		}
		catch(NoSuchProviderException e) {
			throw new ServiceException(
					"There is no provider for SMTP. " +
						"This means the library has changed as it has built-in support for SMTP.",
					e);
		}
		Boolean auth = null;
		try {
			auth = StringUtils.decodeBoolean(
					PreferenceCache.instance().lookup(
						PreferenceCache.KEY_MAIL_AUTH));
		}
		catch(CacheMissException e) {
		}
		if((auth != null) && auth) {
			String mailUsername;
			try {
				mailUsername = 
						PreferenceCache.instance().lookup(
							PreferenceCache.KEY_MAIL_USERNAME);
			}
			catch(CacheMissException e) {
				throw new ServiceException(
					"The mail property is not in the preference table: " +
						PreferenceCache.KEY_MAIL_USERNAME,
					e);
			}
			String mailPassword;
			try {
				mailPassword = 
						PreferenceCache.instance().lookup(
							PreferenceCache.KEY_MAIL_PASSWORD);
			}
			catch(CacheMissException e) {
				throw new ServiceException(
					"The mail property is not in the preference table: " +
						PreferenceCache.KEY_MAIL_PASSWORD,
					e);
			}
			try {
				transport.connect(
						smtpSession.getProperty(MAIL_PROPERTY_HOST), 
						mailUsername, 
						mailPassword);
			}
			catch(MessagingException e) {
				throw new ServiceException(
						"Could not authenticate with or connect to the mail server.",
						e);
			}
		}
		else {
			try {
				transport.connect();
			}
			catch(MessagingException e) {
				throw new ServiceException(
						"Could not connect to the mail server.",
						e);
			}
		}
		try {
			transport.sendMessage(message, message.getAllRecipients());
		}
		catch(SendFailedException e) {
			throw new ServiceException(
					"Failed to send the message.",
					e);
		}
		catch(MessagingException e) {
			throw new ServiceException(
					"There was a problem while sending the message.",
					e);
		}
		try {
			transport.close();
		}
		catch(MessagingException e) {
			throw new ServiceException(
					"After sending the message there was an error closing the connection.",
					e);
		}
	}
public static void sendMailMessage ( Session smtpSession , Message message ) throws ServiceException { try { message . saveChanges ( ) ; } catch ( MessagingException e ) { throw new ServiceException ( "Could not save the changes to the message." , e ) ; } SMTPTransport transport ; try { transport = ( SMTPTransport ) smtpSession . getTransport ( MAIL_PROTOCOL ) ; } catch ( NoSuchProviderException e ) { throw new ServiceException ( "There is no provider for SMTP. " + "This means the library has changed as it has built-in support for SMTP." , e ) ; } Boolean auth = null ; try { auth = StringUtils . decodeBoolean ( PreferenceCache . instance ( ) . lookup ( PreferenceCache . KEY_MAIL_AUTH ) ) ; } catch ( CacheMissException e ) { } if ( ( auth != null ) && auth ) { String mailUsername ; try { mailUsername = PreferenceCache . instance ( ) . lookup ( PreferenceCache . KEY_MAIL_USERNAME ) ; } catch ( CacheMissException e ) { throw new ServiceException ( "The mail property is not in the preference table: " + PreferenceCache . KEY_MAIL_USERNAME , e ) ; } String mailPassword ; try { mailPassword = PreferenceCache . instance ( ) . lookup ( PreferenceCache . KEY_MAIL_PASSWORD ) ; } catch ( CacheMissException e ) { throw new ServiceException ( "The mail property is not in the preference table: " + PreferenceCache . KEY_MAIL_PASSWORD , e ) ; } try { transport . connect ( smtpSession . getProperty ( MAIL_PROPERTY_HOST ) , mailUsername , mailPassword ) ; } catch ( MessagingException e ) { throw new ServiceException ( "Could not authenticate with or connect to the mail server." , e ) ; } } else { try { transport . connect ( ) ; } catch ( MessagingException e ) { throw new ServiceException ( "Could not connect to the mail server." , e ) ; } } try { transport . sendMessage ( message , message . getAllRecipients ( ) ) ; } catch ( SendFailedException e ) { throw new ServiceException ( "Failed to send the message." , e ) ; } catch ( MessagingException e ) { throw new ServiceException ( "There was a problem while sending the message." , e ) ; } try { transport . close ( ) ; } catch ( MessagingException e ) { throw new ServiceException ( "After sending the message there was an error closing the connection." , e ) ; } }
Sends a mail message.
Sends a mail message .
// If use Transport.send(), no need to save the message.  // Otherwise, save the message before sending.   // Get the transport from the session. // This is acceptable. It simply tells JavaMail to use the // default.