2017-05-05 98 views
0

這裏是我使用電子郵件的用戶名和密碼自動附加文件的電子郵件報告程序vb.net

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 

我的問題是,如何設置的config.ini在特定的時手動自動附件的電子郵件程序附件是自動選擇的?我想根據當前時間自動附加文件。

例如:我要附加

C:\ testing1.xlsx

C:\ testing2.xlsx

自動。如果xlsx中的文件內容每天都發生更改,請刷新該文件。

回答

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 

結束子

在該測試TimeKickedOff到要附加到電子郵件的每個附件,例如日期時間,在kirim子,其中SomeTimekirim(TimeKickedOff as Date)子代碼添加=你想要的日期時間的文件附:

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 

要測試文件是否已改變了,你可以在計時器事件處理這個問題。昏暗一個包含文件內容的靜態變量,並檢查內容是否每隔一段時間都會更改一次,如果內容發生更改,則會在發生這種情況時執行您需要執行的操作,並將新內容加載到靜態變量中,以便您可以繼續檢查用於更新的更改。

相關問題