Version 11.1 by Johan Henselmans on 2010/01/07 05:34

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 * er.javamail.smtpPort =
42 To specify the TCP port number of your SMTP server. Default is 25, you can also try 587, who is the "submission" port.
43
44 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.
45
46 == Example Usage ==
47
48 {{code}}
49
50 // Create an instance of an ERMailDelivery subclass
51 ERMailDeliveryHTML mail = new ERMailDeliveryHTML ();
52
53 // Here ERMailDeliveryHTML needs a WOComponent to
54 // render the HTML text content.
55 mail.setComponent(mailPage);
56
57 // Here you create a new instance of the message
58 // You can loop over this fragment of code, not
59 // forgetting to use newMail ()
60 // before you set the attributes of the message.
61 try {
62 mail.newMail();
63 mail.setFromAddress(emailFrom);
64 mail.setReplyToAddress(emailReplyTo);
65 mail.setSubject(emailSubject);
66 mail.setToAddresses(new NSArray (toEmailAddresses));
67 // Send the mail. There is an optional sendMail(boolean) that
68 // optionally blocks during the send.
69 mail.sendMail();
70 } catch (Exception e) {
71 // handle the exception ...
72 }
73
74 {{/code}}
75
76 == Example of sending Mail with Attachments ==
77
78 The subclasses of ERMailDelivery will not only deliver the mail for you, but also will create the message. So ERMailDelivery is
79 in some way more like a message than a delivery mechanism. For each message you want to sent instantiate a concrete subclass of
80 ERMailDelivery (e.g. ERMailDeliveryPlainText). You can then add attachments to it. Below a simple code snippet sending of a
81 mail with an attachment. The MimeType of the attachment is parsed out of the extension of the filename.
82
83 Sending mail is per default asynchronous, you can specify a flag (true h1. shouldBlock, false should not block) to influence
84 the behaviour.
85
86 {{code}}
87
88 byte[] content; // assume this exist, same interface exists for NSData, too
89 // Create an instance of an ERMailDelivery subclass
90 ERMailDeliveryPlainText message = new ERMailDeliveryPlainText();
91 // set the text and subject
92 message.setTextContent("Some Mail text");
93 message.setSubject("The mail subject");
94 // add the attachment
95 message.addAttachment(new ERMailDataAttachment("myattachment.zip", null, content));
96 message.setToAddress("receiver@bitbucket.com");
97 message.setFromAddress("sender@bitbucket.com");
98 // send the mail assynchronously
99 message.sendMail();
100
101 {{/code}}
102
103 == Inline Attachments ==
104
105 If you have a WebObjects Component and you want to send inline images with the component, you have to make use of a trick, that was mentioned in Fabian Peters mail on the Wonder Discussion mailinglist on 6 june 2009. I could not find an online reference.
106
107 First of all, in the Component HTML one includes something like this:
108
109 {{code}}
110
111 <img src="cid:image0" alt="Logo" />
112
113 {{/code}}
114
115 The code that accompanies this cid:image0 is:
116
117 {{code}}
118
119 URL logoURL = myApp().resourceManager().pathURLForResourceNamed("logo.png", null, null);
120 File logo = new File(logoURL.toURI());
121 ERMailAttachment imageLogo = new ERMailFileAttachment("logo.png","<image0>",logo);
122
123 {{/code}}
124
125 Note that the file "logo.png" is included in the Resources folder of you WebObjects project.
126 The "cid:image0" is linked to the <image0> in the ImageAttachment.
127
128 == Gotchas ==
129
130 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:
131
132 {{code}}
133
134 WOContext context = (WOContext) context().clone();
135 MyComponent component = (MyComponent) WOApplication.application().pageWithName(MyComponent.class.getName(), context);
136 ERMailDeliveryHTML mail = new ERMailDeliveryHTML();
137 mail.setComponent(component);
138 ...
139
140 {{/code}}
141
142 This seems to properly isolate the email to a clone of the current context rather than the actual active context. Your mileage may vary.
143
144 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:
145
146 {{code}}
147
148  ERJavaMail.sharedInstance ().finishInitialization();
149
150 {{/code}}