We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

send email after insert record

gianitoo
gianitoo asked
on
Medium Priority
358 Views
Last Modified: 2012-05-05
i have this code and would like to send email after submiting this.   i am used to asp classic and dont know how to send email in dot net.

any ideas?


<MM:Insert
runat="server"
CommandText='<%# "INSERT INTO dbo.coachjobs (ClubName, Info, ContactName, ContactPhone, ContactEmail, Submitted, approval) VALUES (@ClubName, @Info, @ContactName, @ContactPhone, @ContactEmail, @Submitted, @approval)" %>'
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_iysa") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_iysa") %>'
Expression='<%# Request.Form("MM_insert") = "form1" %>'
CreateDataSet="false"
SuccessURL='<%# "coachingjobs.aspx?message=The Job Request has been added for approval" %>'
Debug="true"
><Parameters>
  <Parameter Name="@ClubName" Value='<%# IIf((Request.Form("ClubName") <> Nothing), Request.Form("ClubName"), "") %>' Type="VarChar" />  
  <Parameter Name="@Info" Value='<%# IIf((Request.Form("Info") <> Nothing), Request.Form("Info"), "") %>' Type="VarChar" />  
  <Parameter Name="@ContactName" Value='<%# IIf((Request.Form("ContactName") <> Nothing), Request.Form("ContactName"), "") %>' Type="VarChar" />  
  <Parameter Name="@ContactPhone" Value='<%# IIf((Request.Form("ContactPhone") <> Nothing), Request.Form("ContactPhone"), "") %>' Type="VarChar" />  
  <Parameter Name="@ContactEmail" Value='<%# IIf((Request.Form("ContactEmail") <> Nothing), Request.Form("ContactEmail"), "") %>' Type="VarChar" />  
  <Parameter Name="@Submitted" Value='<%# IIf((Request.Form("submitted") <> Nothing), Request.Form("submitted"), "") %>' Type="DateTime" />  
  <Parameter Name="@approval" Value='<%# IIf((Request.Form("approval") <> Nothing), Request.Form("approval"), "") %>' Type="Int" />  
</Parameters>
</MM:Insert>
Comment
Watch Question

Commented:
Have a look at System.Mail, its a .NET wrapper for CDONTS, I think.

You would use something like:

      MailMessage msg;
      msg = new MailMessage();
      msg.To = "you@you.com";
      msg.From = "me@me.com";
      msg.Subject = "message title";
      msg.Body = "message";
      msg.BodyFormat = MailFormat.Text;
      SmtpMail.SmtpServer = "127.0.0.1";
      SmtpMail.Send(msg);

You would then put this in the appropriate event handler for the click event or button press that you are using.

Andy

Author

Commented:
this is how my form looks like.....do i call it from here

<form method="post" name="form1" runat="server">
                        <table align="center">
                          <tr valign="baseline">
                            <td align="right" nowrap class="aaaa">ClubName:</td>
                            <td><asp:textbox id="ClubName" TextMode="SingleLine" Columns="32" runat="server" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td align="right" valign="top" nowrap class="aaaa">Info:</td>
                            <td><asp:textbox id="Info" TextMode="MultiLine" Columns="32" runat="server" Rows="5" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td align="right" nowrap class="aaaa">ContactName:</td>
                            <td><asp:textbox id="ContactName" TextMode="SingleLine" Columns="32" runat="server" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td align="right" nowrap class="aaaa">ContactPhone:</td>
                            <td><asp:textbox id="ContactPhone" TextMode="SingleLine" Columns="32" runat="server" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td align="right" nowrap class="aaaa">ContactEmail:</td>
                            <td><asp:textbox id="ContactEmail" TextMode="SingleLine" Columns="32" runat="server" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td nowrap align="right">&nbsp;</td>
                            <td><input name="approval" type="hidden" id="approval" value="0" /></td>
                          </tr>
                          <tr valign="baseline">
                            <td nowrap align="right">&nbsp;</td>
                            <td><input type="submit" value="Insert record"></td>
                          </tr>
                        </table>
                        <input type="hidden" name="MM_insert" value="form1">
                        <input name="submitted" type="hidden" id="submitted" value="<%=now%>" />
                      </form>

Commented:
How are you submitting your record to be inserted?

Author

Commented:
i just hit submit and it automatically runs the code i pasted above

Commented:
I think you are going to have to intercept the submit event, run the insert code yourself, and then send the email.

What development environment are you using? Is it .NET 2.0 in Visual Studio 2005 (Web Developer, Professional?)?

Andy
All you have to do is put the mailing process after you're done inserting the records into the database. Like AGBrown mentioned you need a click event when you hit submit. The event wuld process the database code and email the results.

Make sure to import this namespace. So it ould go something like this:
Imports System.Web.Mail

Then inside your AddToDatabase method add the following code:

Dim objMail As New MailMessage
        objMail.From = "you@domain.com"
        objMail.To = "someone@domain.com"
        objMail.Subject = "Your subject header"
        objMail.BodyFormat = MailFormat.Html
        objMail.Body &= "<p><p><font face=Verdana, size=2, color=black>Dear Whoever,</p><p><p>"
        ................
        SmtpMail.SmtpServer = "mail server ip"
        SmtpMail.Send(objMail)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
gianitoo,

Did you have any success? Let us know if you want any more help.

Andy

Author

Commented:
i could not get it working...is there a way that from this code you can insert your code and get it working?

<%@ Register TagPrefix="cspb" Namespace="Coalesys.PanelBar"
Assembly="Coalesys.PanelBar" %>
<%@ Page language="VB" %>

<% @Import Namespace="System.Web.Mail" %>


<%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls" Assembly="DreamweaverCtrls,version=1.0.0.0,publicKeyToken=836f606ede05d46a,culture=neutral" %><MM:Insert
runat="server"
CommandText='<%# "INSERT INTO dbo.coachjobs (ClubName, Info, ContactName, ContactPhone, ContactEmail, Submitted, approval) VALUES (@ClubName, @Info, @ContactName, @ContactPhone, @ContactEmail, @Submitted, @approval)" %>'
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_iysa") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_iysa") %>'
Expression='<%# Request.Form("MM_insert") = "form1" %>'
CreateDataSet="false"
SuccessURL='<%# "coachingjobs.aspx?message=The Job Request has been added for approval" %>'
Debug="true"
><Parameters>
  <Parameter Name="@ClubName" Value='<%# IIf((Request.Form("ClubName") <> Nothing), Request.Form("ClubName"), "") %>' Type="VarChar" />  
  <Parameter Name="@Info" Value='<%# IIf((Request.Form("Info") <> Nothing), Request.Form("Info"), "") %>' Type="VarChar" />  
  <Parameter Name="@ContactName" Value='<%# IIf((Request.Form("ContactName") <> Nothing), Request.Form("ContactName"), "") %>' Type="VarChar" />  
  <Parameter Name="@ContactPhone" Value='<%# IIf((Request.Form("ContactPhone") <> Nothing), Request.Form("ContactPhone"), "") %>' Type="VarChar" />  
  <Parameter Name="@ContactEmail" Value='<%# IIf((Request.Form("ContactEmail") <> Nothing), Request.Form("ContactEmail"), "") %>' Type="VarChar" />  
  <Parameter Name="@Submitted" Value='<%# IIf((Request.Form("submitted") <> Nothing), Request.Form("submitted"), "") %>' Type="DateTime" />  
  <Parameter Name="@approval" Value='<%# IIf((Request.Form("approval") <> Nothing), Request.Form("approval"), "") %>' Type="Int" />  
</Parameters>
</MM:Insert>
<MM:DataSet
id="general"
runat="Server"
IsStoredProcedure="false"
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_iysa") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_DATABASETYPE_iysa") %>'
CommandText='<%# "SELECT *  FROM dbo.coachjobs  WHERE approval = 1 AND Submitted <= GETDATE() AND Submitted >= DATEADD(d, -100, GETDATE())  ORDER BY Submitted DESC" %>'
Debug="true"
></MM:DataSet>
<MM:PageBind runat="server" PostBackBind="true" />
<script runat="server">

Sub Page_Load(Sender As Object, E As EventArgs)

LeftNav.LoadState(Server.MapPath("./") + "\panel.pbs")

If LeftNav.DetectedBrowser = Coalesys.PanelBar.GenerateConstants.NN4DHTML
Response.ContentEncoding = Encoding.Default
End If

End Sub
</script>


<form method="post" name="form1" runat="server">
                        <table align="center">
                          <tr valign="baseline">
                            <td align="right" nowrap class="aaaa">ClubName:</td>
                            <td><asp:textbox id="ClubName" TextMode="SingleLine" Columns="32" runat="server" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td align="right" valign="top" nowrap class="aaaa">Info:</td>
                            <td><asp:textbox id="Info" TextMode="MultiLine" Columns="32" runat="server" Rows="5" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td align="right" nowrap class="aaaa">ContactName:</td>
                            <td><asp:textbox id="ContactName" TextMode="SingleLine" Columns="32" runat="server" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td align="right" nowrap class="aaaa">ContactPhone:</td>
                            <td><asp:textbox id="ContactPhone" TextMode="SingleLine" Columns="32" runat="server" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td align="right" nowrap class="aaaa">ContactEmail:</td>
                            <td><asp:textbox id="ContactEmail" TextMode="SingleLine" Columns="32" runat="server" />                      
                            </td>
                          </tr>
                          <tr valign="baseline">
                            <td nowrap align="right">&nbsp;</td>
                            <td><input name="approval" type="hidden" id="approval" value="0" /></td>
                          </tr>
                          <tr valign="baseline">
                            <td nowrap align="right">&nbsp;</td>
                            <td><input type="submit" OnClick="EmailDataGrid" value="Insert record"></td>
                          </tr>
                        </table>
                        <input type="hidden" name="MM_insert" value="form1">
                        <input name="submitted" type="hidden" id="submitted" value="<%=now%>" />
                      </form>
Commented:
Possibly - it looks like you are working in Dreamweaver though - is that right? I don't have Dreamweaver set up to test with, so we might struggle a little.

Second question, did you put this in, and do you have a method called EmailDataGrid somewhere?
    <input type="submit" OnClick="EmailDataGrid" value="Insert record">

Andy

Author

Commented:
i tried this  but it did not send

  <script runat="server">
 Sub EmailDataGrid(sender as Object, e as EventArgs)


        'System.Web.Mail.MailMessage Class
        Dim message As MailMessage = New MailMessage()

        message.From = "giani@indianayouthsoccer.org"
        message.To = "giani@indianayouthsoccer.org"
        message.Subject = "new job for approval"
        message.Body = "approve it or u will die"

        '------------------------------------------
        ' Also, if you are going to 'hard-code' the
        ' message text, use ControlChars.NewLine
        ' to create a new line (paragraph) in your
        ' mail message.
        '------------------------------------------

        'System.Web.Mail.SmtpMail Class
        SmtpMail.Send(message)

     

    End Sub </script>

Commented:
Well, that's pretty close, but you need to specify an SMTP server in that code just before the SmtpMail.Send(message) line:

SmtpMail.SmtpServer = "enter mail server ip here";

Do you have an SMTP server to use?

Author

Commented:
yes

Author

Commented:
192.168.111.2    is the server address

Commented:
well, I would try wacking that into the line I posted so that you have:

SmtpMail.SmtpServer = "x.x.x.x";
SmtpMail.Send(message);

However, I would set up a test page to test this so that you can be sure that the mail server send it working independently of this, more complex, page.

Author

Commented:
why do we need "; "  at the end

Author

Commented:
i tried this and it did not send   also no errors

  <script runat="server">
 Sub EmailDataGrid(sender as Object, e as EventArgs)


        'System.Web.Mail.MailMessage Class
        Dim message As MailMessage = New MailMessage()

        message.From = "giani@indianayouthsoccer.org"
        message.To = "giani@indianayouthsoccer.org"
        message.Subject = "new job for approval"
        message.Body = "approve it or u will die"

        '------------------------------------------
        ' Also, if you are going to 'hard-code' the
        ' message text, use ControlChars.NewLine
        ' to create a new line (paragraph) in your
        ' mail message.
        '------------------------------------------

        'System.Web.Mail.SmtpMail Class
SmtpMail.SmtpServer = "192.168.11.2"
        SmtpMail.Send(message)

     

    End Sub </script>

Commented:
Sorry about the ; - I forgot you were in VB and slipped back into C#.

Well, this is the code that you will need to send email through an SMTP server. There is nothing wrote with the code as you have it (you could try setting the objMail.BodyFormat = MailFormat.Text but I don't think that will make much difference), which suggests that either the code is not firing, or that the smtp server is not accepting your send. If you could include the line

Me.Label1.Text = DateTime.Now.ToLongTimeString()

at the bottom of your code (you will need to add a label called Label1 to your page) you will know that the method has fired, in which case it will just be a communication problem with your smtp server.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.