Version 18.1 by Pascal Robert on 2009/07/16 08:36

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.smtpProtocol = smtp
29 The protocol to use to connect to the mail server. Defaults to smtp.
30 * er.javamail.XMailerHeader =
31 The X-Mailer header to put into all outgoing mail messages. Defaults to nothing.
32 * er.javamail.defaultEncoding = UTF-8
33 The default character encoding to use for message content. Defaults to ?.
34 * er.javamail.WhiteListEmailAddressPatterns =
35 A comma-separated list of whitelisted email address patterns. If set, then only addresses that match one of the whitelisted
36 patterns will delivered to. Pattern syntax is the same as EOQualifier's caseInsensitiveLike.
37 * er.javamail.BlackListEmailAddressPatterns =
38 A comma-separated list of blacklisted email address patterns. If set, then any email addresses that match a blacklist pattern
39 will not be delivered to. Pattern syntax is the same as EOQualifier's caseInsensitiveLike. The blacklist filter is processed
40 last, so a blacklist pattern beats a whitelist pattern.
41
42 You need to at least specify the value for er.javamail.centralize, and if the value is "true", you also have to specify a value for er.javamail.adminEmail.
43
44 == Example Usage ==
45
46 {{code}}
47
48 // Create an instance of an ERMailDelivery subclass
49 ERMailDeliveryHTML mail = new ERMailDeliveryHTML ();
50
51 // Here ERMailDeliveryHTML needs a WOComponent to
52 // render the HTML text content.
53 mail.setComponent(mailPage);
54
55 // Here you create a new instance of the message
56 // You can loop over this fragment of code, not
57 // forgetting to use newMail ()
58 // before you set the attributes of the message.
59 try {
60 mail.newMail();
61 mail.setFromAddress(emailFrom);
62 mail.setReplyToAddress(emailReplyTo);
63 mail.setSubject(emailSubject);
64 mail.setToAddresses(new NSArray (toEmailAddresses));
65 // Send the mail. There is an optional sendMail(boolean) that
66 // optionally blocks during the send.
67 mail.sendMail();
68 } catch (Exception e) {
69 // handle the exception ...
70 }
71
72 {{/code}}
73
74 == Example of sending Mail with Attachments ==
75
76 The subclasses of ERMailDelivery will not only deliver the mail for you, but also will create the message. So ERMailDelivery is
77 in some way more like a message than a delivery mechanism. For each message you want to sent instantiate a concrete subclass of
78 ERMailDelivery (e.g. ERMailDeliveryPlainText). You can then add attachments to it. Below a simple code snippet sending of a
79 mail with an attachment. The MimeType of the attachment is parsed out of the extension of the filename.
80
81 Sending mail is per default asynchronous, you can specify a flag (true h1. shouldBlock, false should not block) to influence
82 the behaviour.
83
84 {{code}}
85
86 byte[] content; // assume this exist, same interface exists for NSData, too
87 // Create an instance of an ERMailDelivery subclass
88 ERMailDeliveryPlainText message = new ERMailDeliveryPlainText();
89 // set the text and subject
90 message.setTextContent("Some Mail text");
91 message.setSubject("The mail subject");
92 // add the attachment
93 message.addAttachment(new ERMailDataAttachment("myattachment.zip", null, content));
94 message.setToAddress("receiver@bitbucket.com");
95 message.setFromAddress("sender@bitbucket.com");
96 // send the mail assynchronously
97 message.sendMail();
98
99 {{/code}}
100
101 == Gotchas ==
102
103 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:
104
105 {{code}}
106
107 WOContext context = (WOContext) context().clone();
108 MyComponent component = (MyComponent) WOApplication.application().pageWithName(MyComponent.class.getName(), context);
109 ERMailDeliveryHTML mail = new ERMailDeliveryHTML();
110 mail.setComponent(component);
111 ...
112
113 {{/code}}
114
115 This seems to properly isolate the email to a clone of the current context rather than the actual active context. Your mileage may vary.
116
117 If you are using Wonder 3.0 or later and you don't extend ERXApplication, you need to add this line in your Application constructor:
118
119 {{code}}
120
121  ERJavaMail.sharedInstance ().finishInitialization();
122
123 {{/code}}