如何使用VB.net向电子邮件添加附件

时间:2015-03-16 11:30:12

标签: vb.net

我不知道如何在我的电子邮件中添加附件,当用户按下发送请求按钮时,该附件将被发送出去。附件只是一个PDF文件。我尝试了很多东西,但似乎都没有。

这是我用来创建邮件的代码:

Dim _strStyle As String = Nothing
Dim _objMSG As New System.Net.Mail.MailMessage()
Dim _FromAddress As New System.Net.Mail.MailAddress("fakeemail@fake.com")

_strStyle += "<style>"
_strStyle += ".css_bubbles__EMP_home"
_strStyle += "{      "
_strStyle += "float:left;"
_strStyle += "margin:10px 10px 10px 10px;"
_strStyle += "padding:10px 10px 10px 10px;"
_strStyle += "border-top-left-radius:15px;"
_strStyle += "border-top-right-radius:15px;"
_strStyle += "border-bottom-left-radius:15px;"
_strStyle += "border-bottom-right-radius:15px;"
_strStyle += "background-color:#ffffff;"
_strStyle += "border: solid;"
_strStyle += "border-width:2px;"
_strStyle += "border-color:#7FAA01;"
_strStyle += "}"
_strStyle += "</style>"

_objMSG.IsBodyHtml = True
_objMSG.Subject = "RE: You have been invited to attend a meeting."
_objMSG.From = _FromAddress
_objMSG.To.Add(txb_Email.Text)

_objMSG.Body += "<html><head>"
_objMSG.Body += _strStyle
_objMSG.Body += "</head><body>"
_objMSG.Body += "<div class='css_bubbles__EMP_home'>"
_objMSG.Body += "You have a new enquiry:"
_objMSG.Body += "<br /><br />"

_objMSG.Body += "Name: "
_objMSG.Body += Me.txb_Name.Text
_objMSG.Body += "<br /><br />"

_objMSG.Body += "Email Address: "
_objMSG.Body += Me.txb_Email.Text
_objMSG.Body += "<br /><br />"

_objMSG.Body += "Telephone Number: "
_objMSG.Body += Me.txb_Telephone.Text
_objMSG.Body += "<br /><br />"

_objMSG.Body += "Postcode: "
_objMSG.Body += Me.txb_Postcode.Text
_objMSG.Body += "<br /><br />"

_objMSG.Body += "First Line of Address: "
_objMSG.Body += Me.txb_Address.Text
_objMSG.Body += "<br /><br />"

_objMSG.Body += "City/Town: "
_objMSG.Body += Me.txb_City.Text
_objMSG.Body += "<br /><br />"

_objMSG.Body += "</div>"
_objMSG.Body += "</body></html>"
_objMSG.Body += "<br /><br />"

以下是我用来发送消息的代码:

Private Sub Send(ByVal p_objMSG As System.Net.Mail.MailMessage)
    Dim _objCredentials As New System.Net.NetworkCredential
    _objCredentials.UserName = "fakename"
    _objCredentials.Password = "fakepassword"

    Dim _objSMTPCLIENT As New System.Net.Mail.SmtpClient("smtp.sendgrid.net", 25)
    _objSMTPCLIENT.Credentials = _objCredentials
    _objSMTPCLIENT.Host = "smtp.sendgrid.net"
    _objSMTPCLIENT.Send(p_objMSG)
End Sub

1 个答案:

答案 0 :(得分:0)

我能想到的最简单的方法就是这样......

Dim filePath as String
Dim attach As New Net.Mail.Attachment(filePath)
mailMsg.Attachments.Add(attach)