Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

send email after insert record

Posted on 2006-03-31
17
Medium Priority
?
346 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>
0
Comment
Question by:gianitoo
  • 8
  • 8
17 Comments
 
LVL 12

Expert Comment

by:AGBrown
ID: 16343533
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
0
 
LVL 1

Author Comment

by:gianitoo
ID: 16343546
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>
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 16343583
How are you submitting your record to be inserted?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:gianitoo
ID: 16343604
i just hit submit and it automatically runs the code i pasted above
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 16343643
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
0
 

Accepted Solution

by:
stevegingell earned 1000 total points
ID: 16344932
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)
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 16429950
gianitoo,

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

Andy
0
 
LVL 1

Author Comment

by:gianitoo
ID: 16436272
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>
0
 
LVL 12

Assisted Solution

by:AGBrown
AGBrown earned 1000 total points
ID: 16436317
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
0
 
LVL 1

Author Comment

by:gianitoo
ID: 16436414
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>
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 16436457
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?
0
 
LVL 1

Author Comment

by:gianitoo
ID: 16436468
yes
0
 
LVL 1

Author Comment

by:gianitoo
ID: 16436484
192.168.111.2    is the server address
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 16436507
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.
0
 
LVL 1

Author Comment

by:gianitoo
ID: 16436521
why do we need "; "  at the end
0
 
LVL 1

Author Comment

by:gianitoo
ID: 16436536
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>
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 16436759
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question