Imports System.Data
Imports System.Data.Odbc
Imports Microsoft.VisualBasic
Imports System.Web.Mail
Partial Class Contacto_Default
Inherits System.Web.UI.Page
Public Function SQLInsert(ByVal SQLcmd As String) As Integer
Dim sql As String
sql = "select last_insert_id() as ultimo"
Dim ConnStr As String = ConfigurationManager.AppSettings("ODBCConnection")
Dim con As OdbcConnection = New OdbcConnection(ConnStr)
Dim cmd As OdbcCommand = New OdbcCommand(SQLcmd, con)
Dim cmd2 As OdbcCommand = New OdbcCommand(sql, con)
Dim rs As OdbcDataReader
' ejecuta el insert
con.Open()
cmd.ExecuteNonQuery()
rs = cmd2.ExecuteReader(CommandBehavior.CloseConnection Or CommandBehavior.SingleResult)
rs.Read()
SQLInsert = rs("ultimo").ToString()
rs.Close()
con.Close()
rs = Nothing
con = Nothing
End Function
Private Sub SendMail(ByVal Message As String, ByVal Subject As String, ByVal Email As String)
Dim smtp As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("mail.dominio.com", 25)
smtp.Send("correo@dominio.com", Email, Subject, Message)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Subject As String = "Correo enviado"
Dim Sigue As Boolean = True
Dim myMail As New MailMessage()
If TextBox1.Text = "" Then
Label1.Text = "Indique su Nombre"
Sigue = False
End If
If Not Sigue Then
Label1.Visible = True
Else
Dim sql As String
Dim numsol As Integer
sql = "insert into contacto (nombre, direccion, cuando) values ("
sql &= "'" & QuitaC(TextBox1.Text) & "', "
sql &= "'" & QuitaC(TextBox2.Text) & "', now())"
numsol = SQLInsert(sql)
Dim mensaje As String
mensaje &= "<html>"
mensaje &= "<body topmargin=""0"" leftmargin=""0"" rightmargin=""0"" bottommargin=""0"">"
mensaje &= "<table style=""width: 800px"">"
mensaje &= "<tr>"
mensaje &= "<td class=""auto-style3"" colspan=""2""><strong>Correo generado</strong></td>"
mensaje &= "</tr>"
mensaje &= "<tr>"
mensaje &= "<td class=""auto-style1"" style=""width: 119px"" valign=""top""><strong>"
mensaje &= "Nombre:</strong></td>"
mensaje &= "<td class=""auto-style2"" valign=""top"">"
mensaje &= "<div style=""width: 669px"">"
mensaje &= " sasas"
mensaje &= "</div>"
mensaje &= "</td>"
mensaje &= "</tr>"
mensaje &= "<tr>"
mensaje &= "<td class=""auto-style1"" style=""width: 119px"" valign=""top""><strong>"
mensaje &= "Dirección:</strong></td>"
mensaje &= "<td class=""auto-style2"" valign=""top"">"
mensaje &= " ddd"
mensaje &= "</td>"
mensaje &= "</tr>"
mensaje &= "</table>"
mensaje &= "</body>"
mensaje &= "</html>"
myMail.BodyFormat = MailFormat.HTML
myMail.Body = mensaje
SendMail(mensaje, Subject, "mail@destino.com")
Response.Redirect("enviado.aspx")
End If
End Sub
End Class