VB.NET - Automatically send mail in vb.net

Asked By moorthi on 15-Feb-10 07:02 AM
Hai,

    i want send mail automatically every day at particular time with using the send button, once i send it want countioune every day, simply i want create the email schedular in vb.net.

Plz help me immediatly

Thanks

DVMoorthi

Just put your exe in windows Task Scheduler

Robbe Morris replied to moorthi on 15-Feb-10 08:05 AM
end of post
Sagar P replied to moorthi on 15-Feb-10 08:13 AM
You need to devide your application in two parts.
You can write Windows Service which will always running on your machine which will send mails.
And from your VB.net code you can start that service.

So you need to write following steps;
Step1: Write one windows service which will send mail automatically every day on timer.
Just go thr these links which will tell you how to create windows service;
http://www.developer.com/article.php/2173801
http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx
Step2: You need to write code for sending mail in that windows service. Check out this simple code for sending mails;

Dim mailmsg As New System.Net.Mail.MailMessage()

mailmsg.From = New MailAddress("fromMaiId@gmail.com")

mailmsg.To.Add("toMaiId @gmail.com")

mailmsg.Subject = "Mail From .Net Application"

mailmsg.IsBodyHtml = True

mailmsg.Body = "Hi this is a mail from .net appl i have used same code......"

mailmsg.Priority = MailPriority.Normal

Dim client As New System.Net.Mail.SmtpClient()

 

client.UseDefaultCredentials = False

client.Credentials = New System.Net.NetworkCredential("fromMaiId @gmail.com", "mypassword")

client.Port = "587"

client.Host = "smtp.gmail.com"

client.EnableSsl = True

Dim userstate As Object = mailmsg

 

client.Send(mailmsg)

Step3: You need to start this service from your application on button click. Check this code for that;

Imports System.ServiceProcess

dim controller

as new ServiceController
controller.MachineName =
"."
controller.ServiceName = "IISADMIN"
dim status as string = controller.Status.ToString

'Start the service
controller.Start()

Try these steps.

Do these.

[)ia6l0 iii replied to moorthi on 15-Feb-10 08:25 AM
1. Create a command line version of your application.

2. In the Start method, call your datasource (db, or anything that provides you the data that you need to send as body of the email)

3. Use the .Net System.Net.Mail assembly, and write the following code:
  
     Sub Main()
    
    Dim message As New MailMessage()
    message.From = New MailAddress(_fromAddress, "alias")
    message.Subject = "specify your subject"
    message.Body = "specify the body"
    
    For Each toEmailId As String In _toAddress
        message.[To].Add(New MailAddress(toEmailId))
    Next
    
    Dim client As New SmtpClient("specify the smtp address")
    
    AddHandler client.SendCompleted, AddressOf client_SendCompleted
    client.SendAsync(message, "Sending..")
        
    message = Nothing
End Sub

Private Shared Sub client_SendCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    ' Get the unique identifier for this asynchronous operation.
    Dim token As [String] = DirectCast(e.UserState, String)
    
    If e.[Error] IsNot Nothing Then
        'handle the error
    Else
        'message delivered
    End If
End Sub

4. Use Task Scheduler (type 'mstasks' in your run menu and press enter). And schedule your command line utility..

5. Fix the interval.
  
You don't need the button too.

moorthi replied to Robbe Morris on 23-Feb-10 05:31 AM
Hai

       thnx for u reply, but my pblm is i want send mail from microsoft outlook everyday at particular time, i want restrict on saturday and sunday. I try mail send in outlook using windows schedular, but i don't know how to check if saturday and sunday.

Thanks
DVMoorthi
You set the job to only run on the desired days and times
Robbe Morris replied to moorthi on 23-Feb-10 07:42 AM
That's why they call it windows "scheduler".  .NET also has a Weekday property on its DataTime.Now object.