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!

transport.sendMessage failure

843834Dec 30 2009 — edited Jan 4 2010
I realize this problem has probably been answered a hundred times, but I am unable to find a detailed solution. I am a VB programmer ** yes yes, save the boo's :) ** and over the last month I have been working with Java, (using Eclipse compiler)

I've been tasked with sending outlook emails using our SMTP exchange server, I have the server address as well as the port it uses. I'd like to be able to send emails using my own email account and password, or whatever i specify.. here is my code and a description of the current error I am getting. **we use the same SMTP address in our VB apps and it works fine :-\** any help would be appreciated :) thanks

::error message::
javax.mail.MessagingException: Could not connect to SMTP host: 192.168.1.217, port: 25
package com.app.bo.ejb.actions;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class sendOutlookMessage {

	private String user = "myUserNameHere";
	private String pass = "myPasswordHere";
	private String toEmail = "toOutlookEmailHere@provider.com";
	private String subject = "testing";
	private String body = "will this show up";
	private String fromEmail = "fromOutlookEmailHere@provider.com";
	private String fromDisplay = "Testing";
	
	/**
	 * The address the email is sent from, and the name displayed
	 * @param pFromEmail
	 * @param pFromDisplay
	 */
	public void setFromEmail(String pFromEmail, String pFromDisplay){
		fromEmail = pFromEmail;
		fromDisplay = pFromDisplay;
	}
	/**
	 * The address to send the email to
	 * @param pToEmail
	 */
	public void setToEmail(String pToEmail){
		toEmail = pToEmail;
	}
	/**
	 * The subject of the message
	 * @param pSubject
	 */
	public void setSubject(String pSubject){
		subject = pSubject;
	}
	/**
	 * The body of the message
	 * @param pBody
	 */
	public void setBody(String pBody){
		body = pBody;
	}
	/**
	 * Send the outlook message
	 * @return
	 */
	public boolean sendMail(){

		try  
		{ 
			String mailserver = "192.168.1.217";
			String authentication = "false";
			String port = "25";
			
			String username = user;
			String password = pass;
								

			Properties props = System.getProperties(); 
			props.put("mail.smtp.host", mailserver); 
			props.put("mail.smtp.from", fromEmail); 
			props.put("mail.smtp.auth", authentication); 
			props.put("mail.smtp.port", port); 
			Session session = Session.getDefaultInstance(props, null);           

			// -- Create a new message -- 
			MimeMessage message = new MimeMessage(session); 

			// -- Set the FROM and TO fields -- 
			message.setFrom(new InternetAddress(fromEmail, fromDisplay)); 
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false)); 

			MimeMultipart content = new MimeMultipart(); 
			MimeBodyPart text = new MimeBodyPart(); 

			text.setText(body); 
			text.setHeader("MIME-Version" , "1.0" ); 
			text.setHeader("Content-Type" , text.getContentType() ); 

			content.addBodyPart(text); 

			message.setContent( content ); 
			message.setHeader("MIME-Version" , "1.0" ); 
			message.setHeader("Content-Type" , content.getContentType() ); 
			message.setHeader("X-Mailer", "My own custom mailer"); 

			// -- Set the subject -- 
			message.setSubject(subject); 

			// -- Set some other header information -- 
			message.setSentDate(new Date()); 

			// INFO: only SMTP protocol is supported for now... 
			Transport transport = session.getTransport("smtp"); 

			//transport.connect(mailserver, null, null);  
			transport.connect(mailserver, username, password);  <--- ERROR HERE

			message.saveChanges(); 

			// -- Send the message -- 
			transport.sendMessage(message, message.getAllRecipients()); 
			transport.close(); 

			return true; 

		} catch (Exception e) { 
			String failure = e.toString();
		}
		return false; 

	}

	
	
}
Edited by: DebugANoob on Dec 30, 2009 2:31 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 1 2010
Added on Dec 30 2009
9 comments
2,106 views