更正用于将文件附加到ASP.NET联系表单上的电子邮件的VB.NET

时间:2014-02-04 11:21:28

标签: asp.net vb.net webforms

我有一个用htmlASP.NET写的联系表。我想使用<asp:FileUpload>将文件附加到联系表单生成的电子邮件中,但我根本不知道VB.NET要做什么。我不是一个自然的VB呃所以它不是我说的语言,我在语言中所做的一切都来自例子。 VB我已经在下面了,谁能告诉我我需要什么VB.NET以及把它放在我已有的代码中? (我在名为Uploads的httpdocs文件中放置了一个文件夹

提前致谢。

我的代码:

Imports System.Net.Mail

Partial Class Contact_xxxxxxxxxxxxxxx
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim combinedBodyString As String

    combinedBodyString = txtDetails.Text & vbCrLf & vbCrLf
    combinedBodyString = combinedBodyString & "---------Details-------- " & vbCrLf & vbCrLf
    combinedBodyString = combinedBodyString & "Org Name:        " & vbTab
    combinedBodyString = combinedBodyString & txtOrg.Text & vbCrLf
    combinedBodyString = combinedBodyString & "Name: " & vbTab
    combinedBodyString = combinedBodyString & txtName.Text & vbCrLf

    combinedBodyString = combinedBodyString & "Tel No: " & vbTab
    combinedBodyString = combinedBodyString & txtTelephoneNumber.Text & vbCrLf
    combinedBodyString = combinedBodyString & "eMail: " & vbTab
    combinedBodyString = combinedBodyString & txtEmailAddress.Text & vbCrLf



    sendmail(txtEmailAddress.Text, combinedBodyString)

    Response.Redirect("~/Enquiry-Complete.aspx")

End Sub
Private Sub sendmail(ByVal from As String, ByVal body As String)
    Dim mailservername As String = "relay.hostinguk.net"
    Dim message As MailMessage = New MailMessage(from, "xxxx@xxxxxxxx.co.uk", "General Enquiry", body)
    Dim mailclient As New SmtpClient

    mailclient.Host = mailservername

    mailclient.Send(message)
    message.Dispose()
End Sub
End Class

1 个答案:

答案 0 :(得分:1)

取自...... How to send mail with attachment in asp.net

//Attach file using FileUpload Control and put the file in memory stream
If fileUpload1.HasFile Then
  message.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName))
End If

mailclient.Host = mailservernamemailclient.Send(message)

之间添加此内容

我还建议使用stringbuilder而不是将stings连接在一起。