Send email with a webform link through a WebService in ASP.Net using C# and VB.Net

Last Reply one year ago By kalpesh

Posted one year ago

I need to send data like emp id and device id with a webform link through a condition of webservice.asmx

Posted one year ago

Explain in detail and also share your code and let us know the problem.

Posted one year ago


Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Serialization
Imports System.Web.Script.Services
Imports System.Data.SqlClient

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class WebService1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function GPSData(Data As String) As String
        Dim Message As New ServiceResult

        Dim djs As New System.Web.Script.Serialization.JavaScriptSerializer
        Dim attData As clsAttendanceData
        attData = djs.Deserialize(Of clsAttendanceData)(Data)
        Dim constr As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings.Item("constr").ConnectionString)
        Dim RowsAffected As Integer = 0
        Dim d As DateTime = DateTime.Now.ToString("dd MM yyyy HH:mm:ss tt")

        Using cmd1 As New SqlCommand("Select DeviceId from EmployeeTable where Staff_No = " & attData.EmpId, constr)

            Dim sdr = cmd1.ExecuteReader()
            If sdr.HasRows = True Then
                Dim tempDeviceId = cmd1.ExecuteScalar
                If tempDeviceId = attData.DeviceId Then
                    For Each dt As clsGpsData In
                        If d < dt.gpsDateTime Then
                            'constr.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("constr")
                            Using cmd As New SqlCommand("INSERT INTO EmployeeLocationDetails (StaffId, DeviceId, gpsDateTime, Latitude, Longitude, Battery) VALUES ( @StaffId, @DeviceId, @gpsDateTime, @Latitude, @Longitude, @Battery)")

                                cmd.Parameters.AddWithValue("@StaffId", attData.EmpId)
                                cmd.Parameters.AddWithValue("@DeviceId", attData.DeviceId)
                                cmd.Parameters.AddWithValue("@gpsDateTime", dt.gpsDateTime)
                                cmd.Parameters.AddWithValue("@Longitude", dt.lng)
                                cmd.Parameters.AddWithValue("@Battery", dt.battery)

                                cmd.Connection = constr

                                RowsAffected = RowsAffected + cmd.ExecuteNonQuery()
                            End Using
                            Message.Result = -3
                            Message.Reason = dt.gpsDateTime & " is not a valid DateTime"

                            Return djs.Serialize(Message)
                        End If

                    Message.Result = 0
                    Message.Reason = RowsAffected & " Records Inserted"

                    Return djs.Serialize(Message)
                    Message.Result = -1
                    Message.Reason = attData.DeviceId & " !!!!Is not Register!!!!"

                    Return djs.Serialize(Message)
                End If
                Message.Result = -2
                Message.Reason = attData.EmpId & " is not a valid Employee ID"

                Return djs.Serialize(Message)
            End If
        End Using

    End Function

End Class

I need to send an email when it satisfies below else condition my motive is to store device id which is not registered and notify to the manager that someone try to hit some data from so and so device id

                    Message.Result = -1
                    Message.Reason = attData.DeviceId & " !!!!Is not Register!!!!"

                    Return djs.Serialize(Message)
                End If

Posted one year ago

Refer below article link for your reference and implement it as per your code logic just call the SendEmail Method in your else condition where you need to send Email with the device details.

Send HTML Page (File) as Email Body in ASP.Net using C# and VB.Net