我正在努力弄清楚如何在 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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c2a2d27292e393f2522293f3f7c7d0c2b212d2520622f2321" rel="noreferrer noopener nofollow">[email protected]</a>", "*****")
client.Port = 587
client.EnableSsl = True
client.Host = "smtp.gmail.com"
email = New MailMessage()
email.From = New MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa9c9b919f988f8993949f8989cacbba9d979b9396d4999597" rel="noreferrer noopener nofollow">[email protected]</a>")
email.To.Add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0661636863746f656b676f6a363746616b676f6a2865696b" rel="noreferrer noopener nofollow">[email protected]</a>")
email.Subject = "Ping Results"
email.IsBodyHtml = False
email.Body = "The pings were successful, attached is the ping log."
client.Send(email)
End Sub
结束模块
最佳答案
您的 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
关于vb.net - 需要帮助了解如何在 VB.NET 中向电子邮件添加附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26112841/