• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Auto e-mail from JSP

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the JavaMail API to send emails.
 
V. Keating
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any Java code, including sending mail, needs to be executed on the server.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for all your help. I think I can figure out how to move forward from here.
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic