Le 06-12-12, à 08:08, James Cicenia a écrit :

Hello -

What do people do to automatically receive emails and then post to the database say the content of
the email depending on the subject line?

Some code example (from a app we use to manage some mailing list bounces and correct email addresses in a Oracle 9i DB).

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import com.sun.mail.imap.*;
import java.util.regex.*;

        private IMAPStore imapStore;
        private IMAPFolder inbox;
        public NSMutableArray listeErreurs;

public boolean openIMAPConnection() {
        try {
                Properties props = System.getProperties();
                props.put("mail.smtp.host","your.smtp.server");
javax.mail.Session sessionEmail = javax.mail.Session.getDefaultInstance(props,null);
                sessionEmail.setDebug(false);
                imapStore = (IMAPStore)sessionEmail.getStore("imap");
imapStore.connect("your.imap.server", "your.imap.user", "your.imap.passwd");
                inbox = (IMAPFolder)imapStore.getFolder("Retours");
                inbox.open(Folder.READ_WRITE);
                return true;
        } catch (Exception ex) {
                NSLog.out.appendln(Calendar.getInstance().getTime() + " : " + 
ex);
                return false;
                }
        }
}

/*
        To fetch the bounces from a CommuniGate Pro hosted mailing list
*/
public void fetchBounces() {
        listeErreurs = new NSMutableArray();
        EOQualifier qualifier;
        EOFetchSpecification fetchspec;
        NSMutableArray listeCourriels = new NSMutableArray();
                
        try {
for (int iterateur = 1; iterateur <= inbox.getMessageCount(); iterateur++) {
                        MimeMessage message = 
(MimeMessage)inbox.getMessage(iterateur);
                        Pattern sequence = Pattern.compile("Subscriber (.*) 
Failed");
                        Matcher regex = sequence.matcher(message.getSubject());
                        if (regex.matches()) {
                                String adresseCourriel = regex.group(1);
                                if 
(!listeCourriels.containsObject(adresseCourriel)) {
/* Here, you just have to take the email address taken from the Subject line and do a fetch in your database */
                                        NSMutableArray parametres = new 
NSMutableArray(adresseCourriel);
qualifier = EOQualifier.qualifierWithQualifierFormat("email = %@,parametres);
                                        /* Complete it */                       
                                }
                        }
                }
        } catch (Exception nspex) {
                        NSLog.out.appendln(Calendar.getInstance().getTime() + " : 
" + nspex);
        }
}

/*
        To delete a specific email on the IMAP server
*/
public boolean deleteEmail(String _mailUID) {
        try {
                if (_mailUID != null) {
                        MimeMessage message = 
(MimeMessage)inbox.getMessageByUID(_mailUID);
                        message.setFlag(Flags.Flag.DELETED,true);
                        return true;
                } else {
                        NSLog.out.appendln("info est null");
                }
        } catch (Exception ex) {
                NSLog.out.appendln(Calendar.getInstance().getTime() + " : " + 
ex);
        }
        return false;
}

/* The boolean flag tells the IMAP server to purge messages flagged as deleted, if you don't purge them, the emails will stay on the server with the "delete" flag */
public boolean closeIMAPConnection(boolean mustPurgeDeletedMail) {
        try {
                inbox.close(mustPurgeDeletedMail);
                imapStore.close();
                return true;
        } catch (Exception ex) {
                NSLog.out.appendln(Calendar.getInstance().getTime() + " : " + 
ex);
                return false;
                }
        }
}

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to