Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java mail smtp host prob.  RSS feed

 
Joe Angott
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The demo runs fine. It finds the SMTP server just fine. When I use the same code in my app the Transport trys to use "localhost", port 25. In both I set the props the same way. Anyone with an idea? Here's the demo:
import java.lang.Thread.*;
import java.util.*;
import java.text.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class msgsendsample {
static String msgText = "How's this for a return address?";
public static void main(String[] args) {

String to = "joseph.angott@mail.com";
String from = "justanotherperson";
String host = "xxx.xxx.xxx.xxx";
boolean debug = true;
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", host);

Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
for(int v =0 ; v < 5 ; v++ ){
try {
// create a message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Sending you an anonymous email.");
msg.setSentDate(new Date());
// If the desired charset is known, you can use
// setText(text, charset)
msg.setText(msgText);
Transport.send(msg);
} catch (MessagingException mex) {
System.out.println("\n--Exception handling in msgsendsample.java");
mex.printStackTrace();
System.out.println();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
System.out.println(" ** Invalid Addresses");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid[i]);
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
System.out.println(" ** ValidUnsent Addresses");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++)
System.out.println(" "+validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
System.out.println(" ** ValidSent Addresses");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
System.out.println(" "+validSent[i]);
}
}
}
System.out.println();
if (ex instanceof MessagingException)
ex = ((MessagingException)ex).getNextException();
else
ex = null;
} while (ex != null);
}
try{
Thread.sleep(12000);
}catch(Exception e){
}
}
}
private static void usage() {
System.out.println("usage: java msgsendsample <to> <from> <smtp> true|false");
}
}
And my app which is not functioning:
package com.lsi.cendant.mailengine;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import java.util.*;
import javax.activation.*;
import com.lsi.cendant.objects.*;
public class Mailer {
protected static Message prepareHeader(SendMailObj sendMailObj)throws IOException, AddressException,MessagingException {

Properties props = new Properties();
String host = sendMailObj.getSmtpHost();
System.out.println (host);
props.put("mail.smtp.host", host);

Session session = Session.getDefaultInstance(props,null);
session.setDebug(true);
System.out.println ("Props is : " + props.toString());
Message msg = new MimeMessage(session);

InternetAddress errorFromAddr = new InternetAddress("joseph.angott@mail.com");
msg.addRecipient(Message.RecipientType.TO,errorFromAddr);
msg.setText("I hope I get this.");


int number = 0;
for (int j = 0 ; j < sendMailObj.getToAddr().length ; j++){
if (sendMailObj.getToAddr()[j] == null)
break;
number = j + 1;
}
System.out.println("Getting ready to add the TO Addr's : " + number);
InternetAddress toAddr[] = new InternetAddress[number];
String [] toAddresses = sendMailObj.getToAddr();
for (int i = 0 ; i < number ; i++){
toAddr [i] = new InternetAddress(toAddresses[i]);
System.out.println("Added : " + toAddresses[i]);
}
msg.addRecipients(Message.RecipientType.TO,toAddr);
System.out.println("I have added the TO Addr's");
for (int j = 0 ; j < sendMailObj.getFromAddr().length ; j++){
if (sendMailObj.getFromAddr()[j] == null)
break;
number = j + 1;
}
System.out.println("Getting ready to add the FROM Addr's : " + number);
InternetAddress fromAddr[] = new InternetAddress[number];
String [] fromAddresses = sendMailObj.getFromAddr();
for (int i = 0 ; i < number ; i++){
fromAddr [i] = new InternetAddress(fromAddresses[i]);
System.out.println("Added : " + fromAddresses[i]);
}
try{
msg.addFrom(fromAddr);
}catch (Exception e){
// InternetAddress errorFromAddr [] = {new InternetAddress("EmailerProg@mail.com")};
// msg.addFrom(errorFromAddr);
}
System.out.println("I have added the FROM Addr's");
msg.setSubject(sendMailObj.getSubject());
msg.setSentDate(new Date());

System.out.println("RECIP : " + msg.getRecipients(Message.RecipientType.TO).length);
msg.setText(sendMailObj.getMessage());

return msg;
}
public static void sendMail(SendMailObj sendMailObj)throws IOException, AddressException, MessagingException {
Message msg = null;
try{
msg = prepareHeader (sendMailObj);
Transport.send(msg);
} catch (MessagingException mex) {
System.out.println("\n--Exception handling in Mailer.java");
mex.printStackTrace();
System.out.println();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
System.out.println(" ** Invalid Addresses");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid[i]);
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
System.out.println(" ** ValidUnsent Addresses");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++)
System.out.println(" "+validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
System.out.println(" ** ValidSent Addresses");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
System.out.println(" "+validSent[i]);
}
}
}
System.out.println();
if (ex instanceof MessagingException)
ex = ((MessagingException)ex).getNextException();
else
ex = null;
} while (ex != null);
}
try{
Thread.sleep(12000);
}catch(Exception e){
}
}
}

The sleep statements are there for debuging purposes in my IDE. else the dos window disappears to fast for me to read.
 
Aaron Briel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your question? port 25 is the port used for sending email according to smtp protocol...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!