Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending emails - Errors finding email server!

Status
Not open for further replies.

MarkAA

Programmer
Nov 29, 2001
6
GB
I have the following code, which I used on a training course and I know works, but when I try to use it at work it fails with errors relating to not finding the server!

I could be wrong but I think it is because Im not using the correct server id or something similiar. Please help!!!!

The ERRORS I get depending on the different settings I use for the server are as follows:

Couldn't send e-mail: javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.MessagingException: Could not connect to SMTP host: 10.100.1.136, port: 25

HELP! Any ideas on what I need to set the server to would be great! Ive tried server name (its an NT 4.0 server)

The SOURCE CODE is as follows:

package rain;

import javax.mail.*;
import javax.mail.internet.*;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

/**
* E-mailer for sending messages to a SMTP mail server.
*
* <p> <b> To Send a Simple Text Message </b> </p>
*
* Usage Example:
*
* <pre>
*
* String smtpHost = &quot;smtp.server.ltree&quot;;
* String from = &quot;subscription-dept@server.ltree&quot;;
* String to = &quot;ltree8@server.ltree&quot;;
* String subject = &quot;Subscription Confirmation&quot;;
* String text = &quot;Thanks for subscribing to our magazine!&quot;;
*
* try {
* EzMailer.sendMessage(smtpHost, from, to, subject, text);
*
* System.out.println(&quot;Message successfully sent to: &quot; + to);
* }
* catch (javax.mail.MessagingException exc) {
* System.out.println(&quot;Couldn't send e-mail: &quot; + exc.toString());
* }
*
* </pre>
*
* <hr>
* <p> <b> Servlet/JSP Example: Sending an Attachment </b> </p>
*
* Usage Example:
*
* <pre>
*
* String smtpHost = &quot;smtp.server.ltree&quot;;
* String from = &quot;subscription-dept@server.ltree&quot;;
* String to = &quot;ltree8@server.ltree&quot;;
* String subject = &quot;Subscription Confirmation&quot;;
* String text = &quot;Thanks for subscribing to our magazine!&quot;;
*
* String[] fileNamesList = new String[2];
* ServletContext context = getServletContext(); // inherited from GenericServlet
*
* // Attach two files stored in our web app directory &quot;/pubs/&quot;
* //
* // Need to retrieve the complete filesystem path for the files
* // (ie c:\dev\demoapp\website\pubs\java_courses.pdf)
* // Accomplished with context.getRealPath(...).
* //
* // The method context.getRealPath(...) will return the root directory for
* // your web application, for example &quot;c:\crs570\website&quot;.
* // It will also append the String parameter that is passed in.
* //
* fileNamesList[0] = context.getRealPath(&quot;/pubs/java_courses.pdf&quot;);
* fileNamesList[1] = context.getRealPath(&quot;/pubs/xml_courses.pdf&quot;);
*
* try {
* EzMailer.sendMessageAttach(smtpHost, from, to, subject, text, fileNamesList);
* log(&quot;Sent message to: &quot; + to);
* }
* catch (javax.mail.MessagingException exc) {
* log(&quot;Problems sending e-mail: &quot; + exc.toString());
* }
*
* </pre>
*
* @author 570 Development Team
*/
public class EzMailer {


/**
* Sends a simple text e-mail message.
*
* @param smtpHost the SMTP mail server
* @param fromEmail address of the sender (ie maxwell@grandpuba.com)
* @param toEmail e-mail address of the recipient (ie eric@eazye.com)
* @param subject the subject
* @param messageText the message text
*
* @throws javax.mail.MessagingException errors sending message
*/
public static void sendMessage(String smtpHost,
String fromEmail, String toEmail,
String subject, String messageText)
throws javax.mail.MessagingException {

String[] toEmailList = {toEmail};

sendMessage(smtpHost, fromEmail, toEmailList, subject, messageText);
}

/**
* Sends a simple text e-mail message to a list of recipients
*
* @param smtpHost the SMTP mail server
* @param fromEmail address of the sender (ie maxwell@grandpuba.com)
* @param toEmailList e-mail addresses of the recipients
* @param subject the subject
* @param messageText the message text
*
* @throws javax.mail.MessagingException errors sending message
*/
public static void sendMessage(String smtpHost,
String fromEmail, String[] toEmailList,
String subject, String messageText)
throws javax.mail.MessagingException {


// start a session with given properties
java.util.Properties props = new java.util.Properties();
props.put(&quot;mail.smtp.host&quot;, smtpHost);
Session mailSession = Session.getDefaultInstance(props);

InternetAddress fromAddress = new InternetAddress(fromEmail);

// construct a message
MimeMessage myMessage = new MimeMessage(mailSession);
myMessage.setFrom(fromAddress);

for (int i=0; i < toEmailList.length; i++) {
myMessage.addRecipient(javax.mail.Message.RecipientType.TO,
new InternetAddress(toEmailList));
}

myMessage.setSentDate(new java.util.Date());
myMessage.setSubject(subject);
myMessage.setText(messageText);

// now send the message!
Transport.send(myMessage);
}

/**
* Sends an e-mail message to a list of recipients with attachment(s)
*
* @param smtpHost name of the SMTP mail server
* @param from e-mail address of the sender
* @param to e-mailList addresses of the recipients
* @param subject subject of the e-mail message
* @param messageText the text of the message
* @param fileNameList the names of the files to attach. Each file name is the complete filesystem path of the file to attach (ie c:\dev\demoapp\website\pubs\atari.pdf). If you are developing a servlet/JSP then see the usage example at the beginning of this file
*
* @throws javax.mail.MessagingFormatException problems sending message
*/
public static void sendMessageAttach(String smtpHost,
String from, String[] toEmailList,
String subject, String messageText,
String[] fileNameList)
throws MessagingException {

// Configure the mail session
java.util.Properties props = new java.util.Properties();
props.put(&quot;mail.smtp.host&quot;, smtpHost);
Session mailSession = Session.getDefaultInstance(props);

// Construct the message
InternetAddress fromAddress = new InternetAddress(from);

MimeMessage testMessage = new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);

for (int i=0; i < toEmailList.length; i++) {
testMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toEmailList));
}

testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(subject);

// Create a body part to hold the &quot;text&quot; portion of the message
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText(messageText);

// Create a Multipart/container and add the text parts
Multipart container = new MimeMultipart();
container.addBodyPart(textBodyPart);

// Loop thru the file names
// Create a body part to hold the &quot;file&quot; portion of the message
MimeBodyPart fileBodyPart;
FileDataSource fds;

for (int j=0; j < fileNameList.length; j++) {
fileBodyPart = new MimeBodyPart();
fds = new FileDataSource(fileNameList[j]);
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(fds.getName());
container.addBodyPart(fileBodyPart);
}

// Add the Multipart to the actual message
testMessage.setContent(container);

// Now send the message
Transport.send(testMessage);
}

/**
* Sends an e-mail message with attachment(s)
*
* @param smtpHost name of the SMTP mail server
* @param from e-mail address of the sender
* @param toEmail e-mail address of the recipient
* @param subject subject of the e-mail message
* @param messageText the text of the message
* @param fileNameList the names of the files to attach. Each file name is the complete filesystem path of the file to attach (ie c:\dev\demoapp\website\pubs\atari.pdf). If you are developing a servlet/JSP then see the usage example at the beginning of this file.
*
* @throws javax.mail.MessagingFormatException problems sending message
*/
public static void sendMessageAttach(String smtpHost,
String from, String toEmail,
String subject, String messageText,
String[] fileNameList)
throws MessagingException {

String[] toEmailList = {toEmail};
sendMessageAttach(smtpHost, from, toEmailList, subject, messageText, fileNameList);
}

/**
* Sends an e-mail message with an attachment
*
* @param smtpHost name of the SMTP mail server
* @param from e-mail address of the sender
* @param toEmail e-mail address of the recipient
* @param subject subject of the e-mail message
* @param messageText the text of the message
* @param fileName the complete filesystem path of the file to attach (ie c:\dev\demoapp\website\pubs\atari.pdf).
*
* @throws javax.mail.MessagingFormatException problems sending message
*/
public static void sendMessageAttach(String smtpHost,
String from, String toEmail,
String subject, String messageText,
String fileName)
throws MessagingException {

String[] toEmailList = {toEmail};
String[] fileNameList = {fileName};
sendMessageAttach(smtpHost, from, toEmailList, subject, messageText, fileNameList);
}

}

The errors I get depending on the different settings I use for the server are as follows:

Couldn't send e-mail: javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.MessagingException: Could not connect to SMTP host: 10.100.1.136, port: 25
 
The IP 10.100.1.136 was an SMTP mail server that was accessible from wherever you did your training (was it offsite?). If you did the training offsite, it was probably a box the trainer had set up for your use.

Open a command prompt window and try to ping the server like this:

ping 10.100.1.136

If ping can't find the server, neither will your java program.

If your work has an email server, you need to find out from your support techies what the SMTP address is that you can use. Alternately, if you have direct internet access and an ISP that publishes an SMTP address, you can try to use that.

If none of this is helpful, your network people can probably help you out.

&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
Are you behind a firewall at work that is blocking port 23(SMTP)?

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top