WinTr raporları otomatik olarak mail ile gönderiliyor. Bazen raporlar toplu olarak oluşturulduktan bir süre sonra gönderilmek istenebilir. Aşağıdaki script ile Rapor klasöründeki tüm raporlar, Yıl-Ay-Gün şeklinde oluşturulan bir klasöre taşınır, şıkıştırılır ve mail ile gönderilir.

Not: Örneğimizde mail server olarak gmail kullanılmıştır. Gmail kullanacaksanız gmail ayarlarında 3rd yazılımlar için izin oluşturmalısınız. Farklı mail hesapları kullanacaksanız script içerisinde bazı ayarları değiştirmeniz gerekebilir.

Bu script 5.5.1 versiyonu ve sonrasında çalışır.

Imports System
Imports System.IO
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Net.Mail

Namespace WinTr
Public Class MainClass
Public Sub Load
Dim Files() As String = IO.Directory.GetFiles(“C:\Users\Scada\Desktop\NewSCADA\ReportFolder”)
If Files.Length > 0 Then
‘Move
Dim MyPath As String = “C:\Users\Scada\Desktop\NewSCADA\ReportFolder\” & Now.Year & “-” & Now.Month & “-” & Now.Day
If (Not System.IO.Directory.Exists(MyPath)) Then
System.IO.Directory.CreateDirectory(MyPath)
End If
For Each FileName As String In Files
Dim Path() As String = Split(FileName, “\”)
File.Copy(FileName, MyPath & “\” & Path(Path.Length – 1), True)
File.Delete(FileName)
Next
‘Zip
Dim FileList() As String = IO.Directory.GetFiles(MyPath)
If FileList.Length > 0 Then
Using zip As Ionic.Zip.ZipFile = New Ionic.Zip.ZipFile
zip.UseUnicodeAsNecessary = True
For Each FileName As String In FileList
zip.AddFile(FileName , “”)
Next
zip.Save(MyPath & “\MyZipFile.zip”)
End Using
End If
‘E-mail
Try
Dim Mail As New MailMessage
Mail.Subject = “SCADA Report.”
Mail.To.Add(“xxx@fultek.com.tr”)
Mail.From = New MailAddress(“xxx@gmail.com”)
Mail.Body = “Please find report file attached.”
Dim Attachment As Attachment
Attachment = New Attachment(MyPath & “\MyZipFile.zip”)
Mail.Attachments.Add(Attachment)
Dim SMTP As New SmtpClient(“smtp.gmail.com”)
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential(“xxx@gmail.com”, “Password”)
SMTP.Port = 587
SMTP.Send(Mail)
MsgBox(“Email sent successfully!”)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
End Class
End Namespace