Version 3.1 by tobias on 2007/09/01 10:30

Show last authors
1 == Overview ==
2
3 ERJavaMail provides a simple and powerful API for sending component-based emails from a WebObjects application. ERJavaMail does not depend on any other pieces of Project Wonder, and is a good way to get your feet wet using Wonder.
4
5 Kieran Kelleher has written a great [[ERJavaMail Quick Start Guide>>http://homepage.mac.com/kelleherk/iblog/C183784902/E2093239404/index.html]] on his blog.
6
7 == Configuration ==
8
9 * er.javamail.centralize = true
10 Centralize sends all emails to the er.javamail.adminEmail user.
11 * er.javamail.debugEnabled = true
12 Determines whether or not email debugging is displayed. This contains protocol-level debug information.
13 * er.javamail.adminEmail = user@domain.com
14 The email address of the admin user to send centralized emails to. This is a required property.
15 * er.javamail.smtpHost = smtp.domain.com
16 The SMTP host name to use. If this isn't set, mail.smtp.host will be checked and ultimately WOHost will be used.
17 * er.javamail.senderQueue.size = 50
18 The number of messages that the sender queue can hold. Defaults to 50.
19 * er.javamail.milliSecondsWaitIfSenderOverflowed = 6000
20 The number of milliseconds to wait if the sender queue is full. Default is 6000.
21 * er.javamail.smtpAuth = true
22 Sets whether or not Authenticated SMTP is used to send outgoing mail. If set, er.javamail.smtpUser MUST
23 also be set (and preferably er.javamail.smtpPassword).
24 * er.javamail.smtpUser = smtpusername
25 The username to use to login to the authenticated SMTP server.
26 * er.javamail.smtpPassword = smtppassword
27 The password to use to login to the authenticated SMTP server.
28 * er.javamail.XMailerHeader =
29 The X-Mailer header to put into all outgoing mail messages. Defaults to nothing.
30 * er.javamail.defaultEncoding = UTF-8
31 The default character encoding to use for message content. Defaults to ?.
32 * er.javamail.WhiteListEmailAddressPatterns =
33 A comma-separated list of whitelisted email address patterns. If set, then only addresses that match one of the whitelisted
34 patterns will delivered to. Pattern syntax is the same as EOQualifier's caseInsensitiveLike.
35 * er.javamail.BlackListEmailAddressPatterns =
36 A comma-separated list of blacklisted email address patterns. If set, then any email addresses that match a blacklist pattern
37 will not be delivered to. Pattern syntax is the same as EOQualifier's caseInsensitiveLike. The blacklist filter is processed
38 last, so a blacklist pattern beats a whitelist pattern.
39
40 == Example Usage ==
41
42 {{panel}}
43
44 // Create an instance of an ERMailDelivery subclass
45 ERMailDeliveryHTML mail = new ERMailDeliveryHTML ();
46
47 // Here ERMailDeliveryHTML needs a WOComponent to
48 // render the HTML text content.
49 mail.setComponent(mailPage);
50
51 // Here you create a new instance of the message
52 // You can loop over this fragment of code, not
53 / forgetting to use newMail ()
54 // before you set the attributes of the message.
55 try \{
56   mail.newMail();
57   mail.setFromAddress(emailFrom);
58   mail.setReplyToAddress(emailReplyTo);
59   mail.setSubject(emailSubject);
60   mail.setToAddresses(new NSArray (toEmailAddresses));
61
62   // Send the mail. There is an optional sendMail(boolean) that
63   // optionally blocks during the send.
64   mail.sendMail();
65
66 } catch (Exception e) \{
67   // handle the exception ...
68 }
69
70 {{/panel}}
71
72 == Example of sending Mail with Attachments ==
73
74 The subclasses of ERMailDelivery will not only deliver the mail for you, but also will create the message. So ERMailDelivery is
75 in some way more like a message than a delivery mechanism. For each message you want to sent instantiate a concrete subclass of
76 ERMailDelivery (e.g. ERMailDeliveryPlainText). You can then add attachments to it. Below a simple code snippet sending of a
77 mail with an attachment. The MimeType of the attachment is parsed out of the extension of the filename.
78
79 Sending mail is per default asynchronous, you can specify a flag (true h1. shouldBlock, false should not block) to influence
80 the behaviour.
81
82 {{panel}}
83
84 byte\[\] content; // assume this exist, same interface exists for NSData, too
85 // Create an instance of an ERMailDelivery subclass
86 ERMailDeliveryPlainText message = new ERMailDeliveryPlainText();
87 // set the text and subject
88 message.setTextContent("Some Mail text");
89 message.setSubject("The mail subject");
90 // add the attachment
91 message.addAttachment(new ERMailDataAttachment("myattachment.zip", null, content));
92 message.setToAddress("receiver@bitbucket.com");
93 message.setFromAddress("sender@bitbucket.com");
94 // send the mail assynchronously
95 message.sendMail();
96
97 {{/panel}}
98
99 == Gotchas ==
100
101 Be careful of the WOContext that contains the component you are sending. If you use ERMailDeliveryHTML inside of the normal request-response loop with the default WOContext, it is very likely that the next page that is sent to the user will be the emailed component rather than the page you WANTED to send. There are several possible workarounds for this. One is to return a specific component rather than null from your action method. I have had better and more consistent success with the following code:
102
103 {{panel}}
104
105 WOContext context = (WOContext) context().clone();
106 MyComponent component = (MyComponent) WOApplication.application().pageWithName(MyComponent.class.getName(), context);
107 ERMailDeliveryHTML mail = new ERMailDeliveryHTML();
108 mail.setComponent(component);
109 ...
110
111 {{/panel}}
112
113 This seems to properly isolate the email to a clone of the current context rather than the actual active context. Your mileage may vary.
114
115 Category:WebObjects