2014-09-30 43 views
2

我很想弄清楚如何在VB.NET中添加附件到電子郵件。這是我的代碼到目前爲止,我不知道如何包含附件。這是我第一次使用命令提示符和電子郵件系統。需要幫助搞清楚如何在VB.NET中添加附件到電子郵件

Imports System.Net.Mail 

模塊模塊1

Sub Main() 
    Dim client As New SmtpClient 
    Dim email As New MailMessage 
    Dim seconds As Integer 
    Dim interval As Integer 
    Dim ip As String = 0 
    Dim counter As Integer 
    Dim desktop As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
    input(seconds, interval, ip, counter) 
    ping(interval, ip, desktop, counter) 
    Console.WriteLine("Program successfully executed.") 
    Console.ReadLine() 

End Sub 
Sub input(ByRef seconds As Integer, ByRef interval As Integer, ByRef ip As String, ByRef counter As Integer) 
    Console.WriteLine("Please enter the amount of seconds you would like between pings.") 
    Console.WriteLine("Please enter no fewer than five seconds.") 
    seconds = Console.ReadLine() 
    Console.Clear() 
    interval = seconds * 1000 
    Console.WriteLine("Please enter the IP you will be pinging.") 
    ip = Console.ReadLine() 
    Console.Clear() 
    Console.WriteLine("How many times would you like to ping?") 
    counter = Console.ReadLine() 
    Console.Clear() 
End Sub 
Sub ping(ByVal interval As Integer, ByVal ip As String, ByVal desktop As String, ByRef counter As Integer) 
    Do Until counter = 0 
     Process.Start("CMD", "/c ping " & ip & " >> " & desktop & "\log.txt") 
     System.Threading.Thread.Sleep(interval) 
     counter = counter - 1 
    Loop 
End Sub 
Sub email(ByRef client As SmtpClient, ByRef email As MailMessage, ByVal desktop As String) 
    client.UseDefaultCredentials = False 
    client.Credentials = New Net.NetworkCredential("[email protected]", "*****") 
    client.Port = 587 
    client.EnableSsl = True 
    client.Host = "smtp.gmail.com" 
    email = New MailMessage() 
    email.From = New MailAddress("[email protected]") 
    email.To.Add("[email protected]") 
    email.Subject = "Ping Results" 
    email.IsBodyHtml = False 
    email.Body = "The pings were successful, attached is the ping log." 

    client.Send(email) 
End Sub 

前端模塊

+0

'Attachment'類的文檔有一個代碼示例。它在C#中,但它很容易閱讀。如果你不是100%確定,那麼使用在線轉換器將其轉換爲VB。其中沒有任何困難:創建一個'Attachment'對象,調用'Add'方法,調用'Dispose'。而已。 – jmcilhinney 2014-09-30 04:49:55

+0

這是一個很好的例子 http://stackoverflow.com/questions/1962511/how-to-send-an-email-with-attachment-in-vb-net – ilans 2014-09-30 04:51:42

+0

那麼,這是非常明顯的。我覺得自己不是一個白癡! 謝謝你們的幫助=) – Jason 2014-09-30 04:53:12

回答

2

你已經在你的MailMessage對象的屬性Attachments。您只需檢查是否存在要附加的文件以避免任何異常,如下所示;

Dim sFile as String = "Full_File_Path" 
Dim Attachment = New System.Net.Mail.Attachment(sFile) 

    If IO.File.Exists(sFile) Then _ 
    email.Attachments.Add(Attachment) 

如果您連接多個文件;

' Assuming AttachmentFiles is an ArrayList holding your files 
Dim iCountFiles as integer 

If AttachmentFiles IsNot Nothing Then 

    iCountFiles = AttachmentFiles.Count - 1 

    For index = 0 To iCountFiles 

     Dim Attachment = New System.Net.Mail.Attachment(AttachmentFiles(index)) 

     If IO.File.Exists(AttachmentFiles(index)) Then _ 
      email.Attachments.Add(Attachment) 
    Next 

    End If 
相關問題