电子邮件报告程序vb.net上的自动附加文件

时间:2017-05-05 07:52:01

标签: vb.net visual-studio-2017

这是我的程序,用于在特定时间使用config.ini手动附加电子邮件用户名和密码的手动附件

Imports System.Net.Mail
Imports System.Timers

Public Class Form1
Dim file(2) As String   
Dim pesan As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.Text = "Water Monitoring"    
    Timer1.Start()
End Sub

Public Sub kirim()  'step send e-mail manual'
    Try
        Dim Smtp_Server As New SmtpClient
        Dim e_mail As New MailMessage()
        Dim txtEmail As String
        Dim txtPassword As String
        txtEmail = Module1.Read_INI("GENERAL", "Email")
        txtPassword = Module1.Read_INI("GENERAL", "Password")
        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential(txtEmail, txtPassword) 'login email'
        Smtp_Server.Port = 587
        Smtp_Server.Timeout = 3000000
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        e_mail = New MailMessage()
        e_mail.From = New MailAddress(txtEmail)
        e_mail.To.Add(txtTo.Text)
        e_mail.Subject = txtSubject.Text
        e_mail.IsBodyHtml = False
        e_mail.Body = pesan
        If Not txtFile1.Text = Nothing Then
            Dim attach As New Attachment(txtFile1.Text)
            e_mail.Attachments.Add(attach)  'attach attachment 1
        End If
        If Not txtFile2.Text = Nothing Then
            Dim attach As New Attachment(txtFile2.Text)
            e_mail.Attachments.Add(attach)  'attach attachment 2
        End If
        If Not txtFile3.Text = Nothing Then
            Dim attach As New Attachment(txtFile3.Text)
            e_mail.Attachments.Add(attach)  'attach attachment 3
        End If
        Smtp_Server.Send(e_mail)
        MsgBox("Mail Sent")

    Catch error_t As Exception
        MsgBox(error_t.ToString)    'message box error
    End Try

End Sub

Private Sub chckboxAuto30s_CheckedChanged(sender As Object, e As EventArgs) Handles chckboxAuto30s.CheckedChanged
    If chckboxAuto30s.Checked = True Then
        btnSend.Visible = False
    Else
        btnSend.Visible = True  
    End If
End Sub

Private Sub txtMessage_TextChanged(sender As Object, e As EventArgs) Handles txtMessage.TextChanged
    pesan = txtMessage.Text
End Sub

Private Sub btnCancelAllAttachments_Click(sender As Object, e As EventArgs) Handles btnCancelAllAttachments.Click
    txtFile1.Text = ""
    txtFile2.Text = ""
    txtFile3.Text = ""
    file = Nothing  
End Sub

Private Sub btnAddAttachments_Click(sender As Object, e As EventArgs) Handles btnAddAttachments.Click
    file = Nothing
    OpenFileDialog1.ShowDialog()
    file = OpenFileDialog1.FileNames

    txtFile1.Text = file(0)
    Try
        txtFile2.Text = file(1)
    Catch ex As IndexOutOfRangeException
    End Try
    Try
        txtFile3.Text = file(2)
    Catch ex As IndexOutOfRangeException    'attach file attachment'
    End Try
End Sub

Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
    kirim() 'send e-mail manual'
End Sub

Private Sub btnClearText_Click(sender As Object, e As EventArgs) Handles btnClearText.Click
    txtTo.Text = ""
    txtSubject.Text = ""
    txtMessage.Text = ""    
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim timerforAuto As Date
    timerforAuto = CDate(timeAuto.Text)
    If timerforAuto.Hour = Now.Hour And timerforAuto.Minute = Now.Minute And timerforAuto.Second = Now.Second Then
        kirim()
    End If
End Sub
End Class

我的问题是,如何自动选择设置附件?我想根据当前时间自动附加文件。

例如:我想附上

C:\ testing1.xlsx

C:\ testing2.xlsx

自动。如果xlsx中的文件内容每天都在变化,请刷新文件。

1 个答案:

答案 0 :(得分:0)

更改kirim()中的timer1_tick来电,接受日期参数。

Public Sub kirim(TimeKickedOff as Date)  'step send e-mail manual'
    Try
        Dim Smtp_Server As New SmtpClient
        Dim e_mail As New MailMessage()
        Dim txtEmail As String
        Dim txtPassword As String
        txtEmail = Module1.Read_INI("GENERAL", "Email")
        txtPassword = Module1.Read_INI("GENERAL", "Password")
        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential(txtEmail, txtPassword) 'login email'
        Smtp_Server.Port = 587
        Smtp_Server.Timeout = 3000000
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        e_mail = New MailMessage()
        e_mail.From = New MailAddress(txtEmail)
        e_mail.To.Add(txtTo.Text)
        e_mail.Subject = txtSubject.Text
        e_mail.IsBodyHtml = False
        e_mail.Body = pesan
        If Not txtFile1.Text = Nothing Then
            Dim attach As New Attachment(txtFile1.Text)
            e_mail.Attachments.Add(attach)  'attach attachment 1
        End If
        If Not txtFile2.Text = Nothing Then
            Dim attach As New Attachment(txtFile2.Text)
            e_mail.Attachments.Add(attach)  'attach attachment 2
        End If
        If Not txtFile3.Text = Nothing Then
            Dim attach As New Attachment(txtFile3.Text)
            e_mail.Attachments.Add(attach)  'attach attachment 3
        End If
        Smtp_Server.Send(e_mail)
        MsgBox("Mail Sent")

    Catch error_t As Exception
        MsgBox(error_t.ToString)    'message box error
    End Try

End Sub

更改timer1_tick中的来电,将其开始的时间传递给kirim

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim timerforAuto As Date
    timerforAuto = CDate(timeAuto.Text)
    If timerforAuto.Hour = Now.Hour And timerforAuto.Minute = Now.Minute And timerforAuto.Second = Now.Second Then
        kirim(timerforAuto)
    End If

End Sub

kirim(TimeKickedOff as Date)子添加代码中测试TimeKickedOff日期时间,您希望每个附件都附加到电子邮件中,例如kirimSomeTime =您希望附加文件的日期时间:

    If Not txtFile3.Text = Nothing Then
        if TimeKickedOff.Hour = SomeTime.Hour And TimeKickedOff.Minute = SomeTime.Minute And TimeKickedOff.Second = SomeTime.Second Then
            Dim attach As New Attachment(txtFile3.Text)
            e_mail.Attachments.Add(attach)  'attach attachment 3
        End If
    End If

要测试文件是否已更改,您可以在计时器事件中处理此问题。将包含文件内容的静态变量调暗,并检查内容是否经常更改,以及内容更改是否发生了需要执行的操作,并将新内容加载到静态变量中,以便您可以继续检查对于更新的更改。