简体   繁体   English

如何在vb.net中向电子邮件添加一个或多个附件

[英]How to add an attachment or attachments to an email in vb.net

I've searched on this issue for a few days and referenced many questions on this site and others, all to no avail. 我在这个问题上搜索了几天,并在此站点和其他站点上引用了许多问题,但都无济于事。 I have every other feature working, except for this. 除此以外,我还有其他所有功能。 I've also referenced the tooltips that appear in Visual Studio, but nothing seems to work. 我还引用了Visual Studio中显示的工具提示,但似乎没有任何效果。 My code doesn't generate an error, but the email doesn't have the attachment(s), either. 我的代码没有产生错误,但是电子邮件也没有附件。 Here is a piece of my code to use as a reference. 这是我的一部分代码,可以用作参考。

'Add Attachments from alist array'
If alist.Count > 1 Then
    For x As Integer = 1 To alist.Count - 1
        Quick_Mail.email.Attachments.Add(New System.Net.Mail.Attachment(alist(x).ToString))
    Next
End If

I have it this way because I have a separate form that adds the attachments. 之所以这样,是因为我有一个单独的表单来添加附件。 The path to the attachment is stored as a string in the alist array. 附件的路径以字符串形式存储在alist数组中。 Every time a new attachment is added alist is preserved. 每次添加新附件时,都会保留一个列表。 This causes the first position to be empty which is why I initialize x to 1. 这导致第一个位置为空,这就是为什么我将x初始化为1的原因。

Here is my code for the rest of the email setup 这是我其余电子邮件设置的代码

Public Shared email As New MailMessage()
Dim smtp As SmtpClient = New SmtpClient
smtp.Credentials = New Net.NetworkCredential(user, pass)
smtp.Host = host
smtp.Port = port
smtp.EnableSsl = False
email = New MailMessage()
email.From = New MailAddress(user)
email.To = txt_to.text
email.CC = txt_cc.text
email.BCC = txt_bcc.text
email.Subject = txt_subject.Text
email.Body = txt_message.Text

I have email as public and shared so the attachment form can reference it. 我有公开和共享的电子邮件,因此附件表格可以引用它。

Any assistance is greatly appreciated. 非常感谢您的协助。 Thanks in advance. 提前致谢。

I suppose that you call the loop to add the attachments before the code to send the email. 我想您调用循环以在发送电子邮件的代码之前添加附件。 If this is the case then you reinitialize the variable email again and you lose the previous attachments 在这种情况下,请重新初始化变量电子邮件,并且丢失先前的附件

Public Shared email As New MailMessage()
Dim smtp As SmtpClient = New SmtpClient
smtp.Credentials = New Net.NetworkCredential(user, pass)
smtp.Host = host
smtp.Port = port
smtp.EnableSsl = False
' THIS LINE REINITIALIZE THE EMAIL AND KILLS THE ATTACHMENTS 
' email = New MailMessage()
email.From = New MailAddress(user)
email.To = txt_to.text
email.CC = txt_cc.text
email.BCC = txt_bcc.text
email.Subject = txt_subject.Text
email.Body = txt_message.Text

However keeping a Shared variable for this kind of problem will be always a problem. 但是,为此类问题保留Shared变量始终是一个问题。 Why don't you isolate this code in a sub and pass the list of attachments to this sub avoiding to keep the Shared variable exposed? 为什么不在子代码中隔离此代码,然后将附件列表传递给该子代码,以避免使Shared变量暴露出来?

Public Sub SendMail(alist as List(Of String))
    Dim email = New MailMessage()
    Dim smtp = New SmtpClient()

    For x As Integer = 1 To alist.Count - 1
        email.Attachments.Add(New Attachment(alist(x).ToString))
    Next

    smtp.Credentials = New Net.NetworkCredential(user, pass)
    smtp.Host = host
    smtp.Port = port
    smtp.EnableSsl = False
    email.From = New MailAddress(user)
    email.To = txt_to.text
    email.CC = txt_cc.text
    email.BCC = txt_bcc.text
    email.Subject = txt_subject.Text
    email.Body = txt_message.Text
    smtp.Send(email)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM