aspose file tools*
The moose likes JSP and the fly likes Auto e-mail from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Auto e-mail from JSP" Watch "Auto e-mail from JSP" New topic
Author

Auto e-mail from JSP

V. Keating
Greenhorn

Joined: May 14, 2012
Posts: 5
Hello,

I am attempting to program an auto-email feature into a JSP page.
The user logs into to an application where they type information from an image.
When there is a problem with the image they click the "comment" button which takes them to a popup,
where they can comment about the image problems(separate JSP file).
I want to add a feature in the popup that allows them the option to send an e-mail with the comment as the body.
I have seen examples of how to set up an e-mail form but I want the feature to auto send the e-mail and eliminate the human error.
I can look up the username and user address for the FROM: and I can look up the appropriate e-mail for the TO:.
Since I will already have the body of the message in the popup "comment" form I would like to know if it is possible to
program the app to send an e-mail automatically when the comment is submitted.
If, for instance, a check box "send email" is checked, then I would like the application to submit the comment to the db as usual and then send the e-mail before returning to the original application.
I am not looking for specific code just guidelines to help me build this. Any help would be appreciated.

Thank you in advance.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

Don't do this in a JSP. Post the form data (comment) to a servlet and handle it there. In the servlet can you initialize the storage of the comment in the database and the sending of the e-mail, in whichever way you like. You can use JavaMail directly or a wrapper like Simple Java Mail to send e-mail from your application.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61196
    
  66

You can use the JavaMail API to send emails.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
V. Keating
Greenhorn

Joined: May 14, 2012
Posts: 5
I believe we are currently using AJAX dojo.xhrPost as the mean of sending data to the server for the comments. I have attached my JavaScript function. I have included the form where the JavaScript function is called as well. Would I need to change this to use the servlet? or is there another way that is better suited for this layout? I just want to make sure I am taking the most efficient path. Thank you for your responses so far.


function addComment(form)
{
//add to the database before displaying...
var comment = form.elements.namedItem("itemComment").value;
if (comment == null || comment == "")
{
createDojoAlert("Add Comment","<li>Comment required</li>");
}
else
{
var commentType = form.elements.namedItem("commentType").value;
var itemID = "<s:property value="itemID" />";
var itemRowNum = "<s:property value="itemRowNum" />";

dojo.xhrPost
({
url: "<s:url action="AddComment" namespace="/ImageTyper" includeParams="none"/>",
handleAs: "json", //return type (text, xml, json, etc...)
timeout: 10000, //Time in milliseconds (10 secs)
content: {"itemID" : itemID, "itemRowNum" : itemRowNum, "comment" : comment, "commentType" : commentType},
load: function(data, evt)
{
try
{
if (!data.success)
{
createDojoAlert("Add Comment","<li>Error: " + data.error + "</li>");
return;
}
}
catch (Exception)
{
createDojoAlert("Add Comment (catch block)","<li>Error: " + Exception + "</li>");
}
},
error: function(error)
{
createDojoAlert("Add Comment (dojo error)","<li>Error: " + error.reason + "</li>");
}
});

var tr = document.createElement("tr");
tr.align="left";
tr.className="odd";

var td = document.createElement("td");
td.style.borderBottom="1px solid #000000";
td.style.verticalAlign="top";
td.style.fontSize="11px";

var span = document.createElement("span");
span.style.textDecoration="underline";
span.appendChild(document.createTextNode("<s:date name="#request.now" format="MM-dd-yyyy"/>"));
td.appendChild(span);

td.appendChild(document.createElement("br"));

td.appendChild(document.createTextNode("<s:property value="#session.this_user.loginName"/>"));

tr.appendChild(td);

td = document.createElement("td");
td.style.borderBottom="1px solid #000000";
td.style.verticalAlign="top";
td.style.fontSize="11px";
td.appendChild(document.createTextNode(comment));

tr.appendChild(td);

/* Do some magic to find the correct row to add this comment */
var tbody = dojo.byId("commentsTbl").tBodies.item(0);
var trIdx=0;
var trCnt=0;
for (var i=0; i<tbody.childNodes.length; i++)
{
if (tbody.childNodes.item(i).tagName == "TR")
{
trCnt++;
}
if (trCnt == 2)
{
trIdx=i;
}
}

tbody.insertBefore(tr, tbody.childNodes.item(trIdx));

toggleDisplay(dojo.byId('addCommentDiv'));
toggleButtonValue();
form.elements.namedItem("itemComment").value = "";
}
}




<form name="addCommentForm" style="padding:0px; margin:0px;">
<table>
<tbody>
<tr>
<td>
<input type="hidden" name="commentType" value="UserComment" />
<textarea rows="3" cols="70" name="itemComment"></textarea>
</td>
</tr>
<tr>
<td>
<hr />
<input type="button" class="button" onclick="addComment(this.form); return false;" name="SaveComment" value="Save Comment" />
</td>
</tr>
</tbody>
</table>
</form>

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61196
    
  66

Any Java code, including sending mail, needs to be executed on the server.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

V. Keating wrote:I believe we are currently using AJAX dojo.xhrPost as the mean of sending data to the server for the comments. I have attached my JavaScript function. I have included the form where the JavaScript function is called as well. Would I need to change this to use the servlet? or is there another way that is better suited for this layout? I just want to make sure I am taking the most efficient path. Thank you for your responses so far.


I assumed you were using a basic JSP / Servlet approach, but the application seems to be using Struts 2. So instead of a Servlet, some Action class will be handling the form data - in JSON format apperently. Same difference really

Still a server-side component, and like Bear said, that's where you'll need the e-mail sending logic to execute.
V. Keating
Greenhorn

Joined: May 14, 2012
Posts: 5
Thank you for all your help. I think I can figure out how to move forward from here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Auto e-mail from JSP