kurdirektor
Goto Top

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

Content-Key: 263599

Url: https://administrator.de/contentid/263599

Printed on: April 23, 2024 at 14:04 o'clock

Member: emeriks
Solution emeriks Feb 16, 2015 updated at 12:44:49 (UTC)
Goto Top
Hi,
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.
Member: Kurdirektor
Kurdirektor Feb 16, 2015 at 11:59:27 (UTC)
Goto Top
Da kommt immer noch eine Fehlermeldung wenn in der Listbox kein Eintrag vorhanden ist:


01. If ListBox1.Items.Item(9.1).ToString <> "" Then // Fehlerausgabe: InvalidArgument=Value mit dem Wert 0 ist für index ungültig.

02. Dim Attach10 As Attachment

03. Attach10 = New System.Net.Mail.Attachment(ListBox1.Items.Item(9.1))

04. Msg.Attachments.Add(Attach10)

05. end if
Mitglied: 114757
Solution 114757 Feb 16, 2015 updated at 12:33:00 (UTC)
Goto Top
Item(9.1)
das kann ja nicht funktionieren! Das ist ja gar kein Integer-Wert als Parameter face-wink

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
Member: Kurdirektor
Kurdirektor Feb 16, 2015 at 12:33:03 (UTC)
Goto Top
Richtig, Anfänger ja face-smile

das mir der Foreeach SChleife hat funktioniert. Danke

Gelöst