VB - Mail Attachments - Anhang mittels Listbox
Hallo,
ich habe folgenden Code geschrieben...
Dieser funktioniert soweit auch ganz gut aber leider nur dann wenn auch alle aufgeführten Bereiche der Listbox mit "Attachment links" gefüllt sind.
Ein versenden von weniger, als die im Programm vorgegebenen Attachments ist nicht möglich. Das Programm wird dann beendet mit dem Hinweis das ein leerer Eintrag in ListBox1.Items.Item(x.1) nicht erlaubt ist.
Kennt jemand eine Möglichkeit wie man das Problem lösen könnte?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Msg As New MailMessage
Dim myCredentials As New System.Net.NetworkCredential
myCredentials.UserName = "XXXXXXXX"
myCredentials.Password = "XXXXX"
Msg.IsBodyHtml = False
Dim mySmtpsvr As New SmtpClient()
mySmtpsvr.Host = "smtp.gmx.net"
mySmtpsvr.Port = 587
mySmtpsvr.UseDefaultCredentials = False
mySmtpsvr.Credentials = myCredentials
Msg.From = New MailAddress("XXXXXXXX")
Msg.To.Add(TextBox2.Text)
Msg.Subject = TextBox1.Text
Msg.Body = TextBox4.Text + ";" + MA9 + ";" + MA10 + ";" + MA11 + ";" + MA12 + ";" + MA13 + ";" + MA16 + ";" + MA15 + ";" + MA14 + ";" + MA1 + ";" + MA2 + ";" + MA3 + ";" + MA4 + ";" + MA7 + ";" + MA6 + ";" + MA5 + ";" + MA8 + ";" + TextBox3.Text
Dim Attach As Attachment
Attach = New System.Net.Mail.Attachment(ListBox1.Items.Item(0.1))
If ListBox1.Items.Item(0.1).ToString <> "" Then Msg.Attachments.Add(Attach)
Dim Attach2 As Attachment
Attach2 = New System.Net.Mail.Attachment(ListBox1.Items.Item(1.1))
If ListBox1.Items.Item(1.1).ToString <> "" Then Msg.Attachments.Add(Attach2)
Dim Attach3 As Attachment
Attach3 = New System.Net.Mail.Attachment(ListBox1.Items.Item(2.1))
If ListBox1.Items.Item(2.1).ToString <> "" Then Msg.Attachments.Add(Attach3)
Dim Attach4 As Attachment
Attach4 = New System.Net.Mail.Attachment(ListBox1.Items.Item(3.1))
If ListBox1.Items.Item(3.1).ToString <> "" Then Msg.Attachments.Add(Attach4)
Dim Attach5 As Attachment
Attach5 = New System.Net.Mail.Attachment(ListBox1.Items.Item(4.1))
If ListBox1.Items.Item(4.1).ToString <> "" Then Msg.Attachments.Add(Attach5)
Dim Attach6 As Attachment
Attach6 = New System.Net.Mail.Attachment(ListBox1.Items.Item(5.1))
If ListBox1.Items.Item(5.1).ToString <> "" Then Msg.Attachments.Add(Attach6)
Dim Attach7 As Attachment
Attach7 = New System.Net.Mail.Attachment(ListBox1.Items.Item(6.1))
If ListBox1.Items.Item(6.1).ToString <> "" Then Msg.Attachments.Add(Attach7)
Dim Attach8 As Attachment
Attach8 = New System.Net.Mail.Attachment(ListBox1.Items.Item(7.1))
If ListBox1.Items.Item(7.1).ToString <> "" Then Msg.Attachments.Add(Attach8)
Dim Attach9 As Attachment
Attach9 = New System.Net.Mail.Attachment(ListBox1.Items.Item(8.1))
If ListBox1.Items.Item(8.1).ToString <> "" Then Msg.Attachments.Add(Attach9)
Dim Attach10 As Attachment
Attach10 = New System.Net.Mail.Attachment(ListBox1.Items.Item(9.1))
If ListBox1.Items.Item(9.1).ToString <> "" Then Msg.Attachments.Add(Attach10)
mySmtpsvr.Send(Msg)
MsgBox("Erfolgreich ausgeführt.", MsgBoxStyle.Information, Title:="Information")
End Sub
ich habe folgenden Code geschrieben...
Dieser funktioniert soweit auch ganz gut aber leider nur dann wenn auch alle aufgeführten Bereiche der Listbox mit "Attachment links" gefüllt sind.
Ein versenden von weniger, als die im Programm vorgegebenen Attachments ist nicht möglich. Das Programm wird dann beendet mit dem Hinweis das ein leerer Eintrag in ListBox1.Items.Item(x.1) nicht erlaubt ist.
Kennt jemand eine Möglichkeit wie man das Problem lösen könnte?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Msg As New MailMessage
Dim myCredentials As New System.Net.NetworkCredential
myCredentials.UserName = "XXXXXXXX"
myCredentials.Password = "XXXXX"
Msg.IsBodyHtml = False
Dim mySmtpsvr As New SmtpClient()
mySmtpsvr.Host = "smtp.gmx.net"
mySmtpsvr.Port = 587
mySmtpsvr.UseDefaultCredentials = False
mySmtpsvr.Credentials = myCredentials
Msg.From = New MailAddress("XXXXXXXX")
Msg.To.Add(TextBox2.Text)
Msg.Subject = TextBox1.Text
Msg.Body = TextBox4.Text + ";" + MA9 + ";" + MA10 + ";" + MA11 + ";" + MA12 + ";" + MA13 + ";" + MA16 + ";" + MA15 + ";" + MA14 + ";" + MA1 + ";" + MA2 + ";" + MA3 + ";" + MA4 + ";" + MA7 + ";" + MA6 + ";" + MA5 + ";" + MA8 + ";" + TextBox3.Text
Dim Attach As Attachment
Attach = New System.Net.Mail.Attachment(ListBox1.Items.Item(0.1))
If ListBox1.Items.Item(0.1).ToString <> "" Then Msg.Attachments.Add(Attach)
Dim Attach2 As Attachment
Attach2 = New System.Net.Mail.Attachment(ListBox1.Items.Item(1.1))
If ListBox1.Items.Item(1.1).ToString <> "" Then Msg.Attachments.Add(Attach2)
Dim Attach3 As Attachment
Attach3 = New System.Net.Mail.Attachment(ListBox1.Items.Item(2.1))
If ListBox1.Items.Item(2.1).ToString <> "" Then Msg.Attachments.Add(Attach3)
Dim Attach4 As Attachment
Attach4 = New System.Net.Mail.Attachment(ListBox1.Items.Item(3.1))
If ListBox1.Items.Item(3.1).ToString <> "" Then Msg.Attachments.Add(Attach4)
Dim Attach5 As Attachment
Attach5 = New System.Net.Mail.Attachment(ListBox1.Items.Item(4.1))
If ListBox1.Items.Item(4.1).ToString <> "" Then Msg.Attachments.Add(Attach5)
Dim Attach6 As Attachment
Attach6 = New System.Net.Mail.Attachment(ListBox1.Items.Item(5.1))
If ListBox1.Items.Item(5.1).ToString <> "" Then Msg.Attachments.Add(Attach6)
Dim Attach7 As Attachment
Attach7 = New System.Net.Mail.Attachment(ListBox1.Items.Item(6.1))
If ListBox1.Items.Item(6.1).ToString <> "" Then Msg.Attachments.Add(Attach7)
Dim Attach8 As Attachment
Attach8 = New System.Net.Mail.Attachment(ListBox1.Items.Item(7.1))
If ListBox1.Items.Item(7.1).ToString <> "" Then Msg.Attachments.Add(Attach8)
Dim Attach9 As Attachment
Attach9 = New System.Net.Mail.Attachment(ListBox1.Items.Item(8.1))
If ListBox1.Items.Item(8.1).ToString <> "" Then Msg.Attachments.Add(Attach9)
Dim Attach10 As Attachment
Attach10 = New System.Net.Mail.Attachment(ListBox1.Items.Item(9.1))
If ListBox1.Items.Item(9.1).ToString <> "" Then Msg.Attachments.Add(Attach10)
mySmtpsvr.Send(Msg)
MsgBox("Erfolgreich ausgeführt.", MsgBoxStyle.Information, Title:="Information")
End Sub
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 263599
Url: https://administrator.de/forum/vb-mail-attachments-anhang-mittels-listbox-263599.html
Ausgedruckt am: 03.05.2025 um 22:05 Uhr
4 Kommentare
Neuester Kommentar
Hi,
ohne dass ich mich jetzt konkret auf die SMTP-Sache beziehe:
Müsset das nicht so ausehen?
Oder gleich:
E.
ohne dass ich mich jetzt konkret auf die SMTP-Sache beziehe:
Müsset das nicht so ausehen?
If ListBox1.Items.Item(9.1).ToString <> "" Then
Dim Attach10 As Attachment
Attach10 = New System.Net.Mail.Attachment(ListBox1.Items.Item(9.1))
Msg.Attachments.Add(Attach10)
end if
Oder gleich:
for each xItem in ListBox1.Items
If xItem.ToString <> "" Then
Dim A As Attachment
A = New System.Net.Mail.Attachment(xItem)
Msg.Attachments.Add(A)
end if
next
E.

Item(9.1)
das kann ja nicht funktionieren! Das ist ja gar kein Integer-Wert als Parameter Mach das mit der Foreach-Schleife von E. . Denn wenn du keine Elemente in deiner Listbox hast wird dein Code sowieso in eine Exception laufen !
Das einzelne definieren und Abfragen ist absoluter Anfänger Codemist ...
Gruß jodel