Solved

how can i Run-once code from asp.net web service without calling the function from on_start?

Posted on 2009-06-26
1
448 Views
Last Modified: 2012-05-07
Here i am calling the SendMails() event from onstart event. while i am adding this one to web service taking more time to start the webservice.

now i want to call the sendmail event only once after the web service started.

or is any other method to send the mails other than using webservice.
Imports System.ServiceProcess
Imports System.Data.SqlClient
Imports System.Web.Mail
 
Public Class service1
    Inherits System.ServiceProcess.ServiceBase
	
	Dim sql As String
 
    Dim MyCon As SqlConnection
    Dim MyCmd As SqlCommand
    Dim MyRd As SqlDataReader
 
    Public conStr As String = "connection string"
 
    Protected Overrides Sub OnStart(ByVal args() As String)
        SendMails()
    End Sub
 
    Protected Overrides Sub OnStop()
 
    End Sub
 
    Public Function SendMails()
        Dim sno As Int64
        Dim strMailBdy As String
        Dim msgMail As MailMessage
        Dim strAryEmail(5000) As String
        Try
            sql = "select Email from TUser where Sno between 1 and 50000"
            MyCon = New SqlConnection(conStr)
            MyCon.Open()
            MyCmd = New SqlCommand(sql, MyCon)
            MyRd = MyCmd.ExecuteReader
            sno = 0
            While MyRd.Read
                strAryEmail(sno) = MyRd("Email")
                sno = sno + 1
                If sno > 4999 Then
                    ReDim Preserve strAryEmail(sno + 1)
                End If
            End While
            MyRd.Close()
            MyCmd = Nothing
            MyCon.Close()
 
            Dim intCustLen As Int64
            intCustLen = sno - 1
 
            For sno = 0 To intCustLen Step 1
                If strAryEmail(sno).ToString <> "" Then
            
                    msgMail = New MailMessage
                    msgMail.To = strAryEmail(sno).ToString.Trim
                    msgMail.From = "From EmailID"
                    msgMail.Subject = "Subject"
                    msgMail.Body = "Email Body"
                    msgMail.BodyFormat = MailFormat.Html
                    msgMail.Priority = MailPriority.Normal
					
                    Try
                        SmtpMail.Send(msgMail)
                    Catch ex As Exception
 
                    End Try
                    System.Threading.Thread.Sleep(2000)
                End If
            Next
        Catch ex As Exception
            If MyCon.State = ConnectionState.Open Then MyCon.Close()
        End Try
    End Function
 
End Class

Open in new window

0
Comment
Question by:Ranjith_MCAD
1 Comment
 
LVL 11

Accepted Solution

by:
Salim Fayad earned 500 total points
ID: 24727251
You can send emails from your web application directly without passing thrugh a webservice. But if you do want to do it from your webservice, you can write a function in your webserice that you call it from your web application.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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