• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
JavaMail in WO
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

JavaMail in WO


  • Subject: JavaMail in WO
  • From: "Jonathan Fleming" <email@hidden>
  • Date: Sat, 07 Feb 2004 02:39:50 +0000

I'm using javaMail quite successfully but I don't know how to set it up to return an unsent error message. Can anyone tell me how or help me out with completing the code below to do this?
Eg. like this that hotmail does when an address is not valid or reponding.


From :  <email@hidden>
Sent :  07 February 2004 01:41:39
To :  email@hidden
Subject :  Delivery Status Notification (Failure)

This is an automatically generated Delivery Status Notification.

Delivery to the following recipients failed.

      email@hidden
      email@hidden
      email@hidden


================================

This is the code i'm using:
/* SMTPMailSender.java created by Jonathan on Tue 29-Jul-2003 */

import com.webobjects.foundation.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;
import com.webobjects.appserver.*;

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SMTPMailSender {

public static boolean send(String subject, String to, String from, String body, String host, String user, String pass) {
NSLog.out.appendln( " host: " + host );
NSLog.out.appendln( " user: " + user!=null?" got user":" null" );
NSLog.out.appendln( " pass: " + pass!=null?" got pass":" null" );


       String mailHost = host;
       String mailSender = user;
       String mailPwd = pass;

if ((from != null) && (to != null) && (subject != null) && (body != null)) {
try {
NSLog.out.appendln( "===\r started trying to send mail" );


//Get system properties
Properties props = System.getProperties();
//Specify the desired SMTP server
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.auth", "true");
// create a new Session object
javax.mail.Session session = javax.mail.Session.getInstance(props,null);
session.setDebug(true);


try {
// create a new MimeMessage object (using the Session created above)
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(to) });
message.setSubject(subject);
message.setContent(body, "text/plain");
message.setSentDate(new Date());
message.saveChanges();


Transport transport = session.getTransport("smtp");
transport.connect(mailHost,mailSender,mailPwd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
NSLog.out.appendln( " mail was sent through: " + transport );
return true;
}
catch (MessagingException mex) {
mex.printStackTrace();
System.out.println();
Exception ex = mex;


do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException) ex;
Address[] invalid = sfex.getInvalidAddresses();


                           if (invalid != null) {
                               for (int i = 0; i < invalid.length; i++) {
                                   System.out.println("Invalid address: " +
                                                      invalid[i]);
                               }
                           }
                           Address[] validUnsent =
                               sfex.getValidUnsentAddresses();

if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++) {
System.out.println("Valid unsent address: " +
validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
for (int i = 0; i < validSent.length; i++) {
System.out.println("Valid sent address: " +
validSent[i]);
}
}
}


if (ex instanceof MessagingException) {
ex = ((MessagingException) ex).getNextException();
} else {
ex = null;
}
} while (ex != null);
return false;
}
}
catch (Throwable t) {
System.err.println("SMTP: " + t);
return false;
}
}
return false;
}
}





Regards Jonathan :^)

_________________________________________________________________
Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: JavaMail in WO
      • From: Michael Engelhart <email@hidden>
  • Prev by Date: Re: Globally unique IDs
  • Next by Date: Re: Globally unique IDs
  • Previous by thread: Re: Globally unique IDs
  • Next by thread: Re: JavaMail in WO
  • Index(es):
    • Date
    • Thread