I'm using a nifty little Tapestry component that Adam Greene wrote a
while back called MailBlock. It has worked pretty well because you can
just embed your email in your page template. Now, I won't get into an
argument about whether people think this is good style, but I really
like it. It works like this:
<span jwcid="@MailBlock" from="[EMAIL PROTECTED]" smtp="localhost"
subject="Title illustrations submitted for publication"
to="[EMAIL PROTECTED]" html="ognl:false">
Blah blah this is the message text.
</span>
I don't know if Adam is still around the list, but the code he sent me
has an ASL, so I don't think he would mind if I shared it. It requires
mail.jar (the standard java mail extensions) to be on the classpath.
Hope that helps,
Danny
MailBlock.jwc
----------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE component-specification
PUBLIC "-//Howard Lewis Ship//Tapestry Specification 1.3//EN"
"http://tapestry.sf.net/dtd/Tapestry_1_3.dtd">
<!--
Copyright 2004 Adam Greene
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<component-specification class="net.sf.tapestry.components.MailBlock"
allow-body="yes" allow-informal-parameters="no">
<parameter
name="from"
java-type="java.lang.String"
direction="in"
required="yes">
<description>
<![CDATA[ the email address that this email is being sent
from (ie. [EMAIL PROTECTED]) ]]>
</description>
</parameter>
<parameter
name="html"
java-type="boolean"
direction="in"
required="no">
<description>
<![CDATA[ Is this email going to be sent as an HTML email
(true) or plain text (false). Default: True. ]]>
</description>
</parameter>
<parameter
name="smtp"
java-type="java.lang.String"
direction="in"
required="yes">
<description>
<![CDATA[ the domain name / IP of the SMTP server (ie.
mail.mycompany.com) ]]>
</description>
</parameter>
<parameter
name="subject"
java-type="java.lang.String"
direction="in"
required="yes">
<description><![CDATA[ The subject of the email.
]]></description>
</parameter>
<parameter
name="to"
java-type="java.util.Collection"
direction="in"
required="yes">
<description>
<![CDATA[ A collection of strings containing the email
address to send the email to. ]]>
</description>
</parameter>
</component-specification>
----------------------------------------
MailBlock.java
----------------------------------------
//Copyright 2004 Adam Greene
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package net.sf.tapestry.components;
import java.io.ByteArrayOutputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.tapestry.AbstractComponent;
import org.apache.tapestry.ApplicationRuntimeException;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.html.HTMLWriter;
public class MailBlock extends AbstractComponent {
private String from;
private Collection to;
private String subject;
private String smtp;
private boolean html = true;
/**
* @see
net.sf.tapestry.AbstractComponent#renderComponent(IMarkupWriter,
IRequestCycle)
*/
protected void renderComponent(IMarkupWriter writer, IRequestCycle
cycle)
throws ApplicationRuntimeException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
IMarkupWriter subwriter = new HTMLWriter(bout);
renderBody(subwriter, cycle);
subwriter.flush();
String body = new String(bout.toByteArray());
try {
doMail(body);
} catch (Exception e) {
System.out.println("Could send email titled \"" + subject +
"\":");
e.printStackTrace();
}
}
/**
* @param body the content of the email body
* @throws Exception
*/
protected void doMail(String body) throws Exception {
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", smtp);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
Iterator i = to.iterator();
// Load the recipients into the mail message
while (i.hasNext()) {
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress("" + i.next()));
}
// Set the subject of the email
message.setSubject(subject);
if (html)
message.setContent(body, "text/html");
else
message.setContent(body, "text/plain");
// Send message
Transport.send(message);
}
/**
* Returns the who the email is from
* @return String
*/
public String getFrom() {
return from;
}
/**
* Returns the subject of the email
* @return String
*/
public String getSubject() {
return subject;
}
/**
* Returns the collection of recipients that the email is being sent to
* @return Collection
*/
public Collection getTo() {
return to;
}
/**
* Sets who the email is from
* @param from The from to set
*/
public void setFrom(String from) {
this.from = from;
}
/**
* Sets the subject of the email
* @param subject The subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* Sets to whom the email is being sent
* @param to The to to set
*/
public void setTo(Collection to) {
this.to = to;
}
/**
* @return the name of the smtp server
*/
public String getSmtp() {
return smtp;
}
/**
* @param smtp the domain / ip of the SMTP server to use
*/
public void setSmtp(String smtp) {
this.smtp = smtp;
}
/**
* @return is the email being sent as HTML email or plain text
*/
public boolean isHtml() {
return html;
}
/**
* @param ishtml is this email going to be sent as an HTML email?
*/
public void setHtml(boolean ishtml) {
html = ishtml;
}
}
--------------------------------------------------------
Ron Piterman wrote:
Hi,
I need some tips on sending mails from my application, which uses a
very simple tomcat-apache configuration.
What I need is some simple system, with template support (including
i18n), which will send mails to users -
will be most greatfull for tips,
Cheers,
Ron
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]