Hey, everyone.

I am writing a batch script (.bat program) to automate some tasks. And when that certain task is complete, I would like it to send an email to the admin saying if the task was completed successfully or it failed. It will figure out if the task was completed successfully or it failed by checking the log file.
Anyways, so, at the very end, I want it to send out an email. I am also trying to attach the log file, OR make make my batch script go through the log file FIND those errors and put them in a new .txt log file and THEN attach that new log file and send it to the admin.
I was trying to write the code for mail sending directly into the batch, and it kinda looked pretty messy. So, I wrote it in VBS (.vbs). I was testing it
and I keep getting the error that says that it could not connect to the server.
Here is what my .vbs script looks like:

Dim objEmail
Dim mail
Set objEmail = CreateObject("CDO.Message")
objEmail.Subject = "DDL Deployment Success Test"
objEmail.From = "my@email.com"
objEmail.To = "my@email.com"
objEmail.TextBody = "SUCCESS."
objEmail.AddAttachment "c:\example.txt"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mail.SMTPserver.com"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send

Btw, I am also, not sure how to figure out my SMTP server name and all that. If you know anything about that, please, share :)
Any help??? It does not have to be strictly .vbs or .bat

Thanks, again.

Member Avatar for CurtisUN

Hi,
Here is a sub I used for sending mail. This requires a password and email address for the sender. This was written Using VB2010 .net 4. Also attachments can be used. This works though I did not use the attachments I added a method for attachments to this code.

Imports System.Net.Mail
Imports System.Net

Public Class form1

    Private Sub SendMail()
        Using msg As New System.Net.Mail.MailMessage()
            Using SMPclint As New SmtpClient()
                Try
                    Dim NetCry As New NetworkCredential()
                    msg.IsBodyHtml = True
                    msg.[To].Add("Admin@email.com") ' to email address
                    msg.Subject = "DDL Deployment Success Test"
                    msg.Body = "SUCCESS."
                    msg.Attachments.Add(New Attachment("C:\Test.txt"))
                    'msg.IsBodyHtml = False
                    Dim hostname() As String = Split("my@email.com", "@") 'from email address
                    If hostname(1).Contains("hotmail") Then
                        hostname(1) = "live.com"
                    End If
                    SMPclint.Host = "smtp." & hostname(1)
                    SMPclint.EnableSsl = True
                    NetCry.UserName = "my@email.com" 'from email address
                    NetCry.Password = "Password" 'From email password
                    SMPclint.UseDefaultCredentials = True
                    SMPclint.Credentials = NetCry
                    Dim fromMailAddr As New MailAddress("Admin@email.com", "Admin@email.com")
                    msg.From = fromMailAddr
                    SMPclint.Port = 587
                    SMPclint.Send(msg)
                    Me.Close()
                Catch ex As Exception
                    Me.Text = "EmailSend Form (" & ex.Message & ")"
                    Me.Show()
                End Try
            End Using
        End Using
    End Sub
End Class

Curtis

Thanks for the effort you put in, CurtisUN. Really appreciate it :)
However, since, I am using my work machine, I do not really have a lot of access to things like VB, .Net and so on. If I want to download and istall them, I have to make requests and that itself is gonna take 10-20 days. That's whay I was using mainly .bat, .vbs.

But, still, did not quite understan below lines:

> Dim hostname() As String = Split("my@email.com", "@") 'from email address      
> If hostname(1).Contains("hotmail") Then
> hostname(1) = "live.com"

> Dim fromMailAddr As New MailAddress("Admin@email.com", "Admin@email.com")

> Me.Text = "EmailSend Form (" & ex.Message & ")"

Would you mind clarifying them a little bit more, please?

Thanks a lot.

Member Avatar for CurtisUN

The first line seperates the host name from the email server name.The second checks to see if the server name is a hotmail address and if it is the the third line will change the address from hotmail.com to live mail.com because the hotmail addresses ues only live.com for emails. This is indicated in a white page from MS that a portion of this code came from..
The fourth line is building a new address to use. The first part of the statement ("Admin@email.com",) is the actual address the system will use to send the mail and the second part of the statment (,"Admin@email.com") is the senders Name and can be any text.
The last line just uses the forms header to indicate if an error occured.

Curtis

Thanks a lot Curtis. Helped a lot!

hi

how to use in my environment and what changaes are reuqired what is the file ext.

commented: zombie -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.