2014-03-29 89 views
0

好吧,基本上我的網站上有一個評論表。人們只是在評論框中發表評論,並將評論通過電子郵件發送給我的電子郵件。電子郵件部分工作正常,並通過。但是,當我嘗試清除表單域時出現錯誤。它給我一個錯誤說:參數'from'不能是空字符串。參數名稱:從

錯誤:參數'從'不能是一個空字符串。 參數名稱:從

所以任何人有任何想法?就像我說的電子郵件部分工作正常,它只是清除場是給我找麻煩

這裏是代碼:

Imports System.Web.Mail 
Imports System.Text 

Partial Class Pages_ContactUs : Inherits System.Web.UI.Page 

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    Dim body As String = "From: " + YourName.Text + " " + Environment.NewLine + "Email: " + YourEmail.Text + Environment.NewLine + Environment.NewLine + "Message: " + Environment.NewLine + Comments.Text 

    Dim MM As New System.Net.Mail.SmtpClient 
    MM.EnableSsl = True 
    MM.Host = "smtp.gmail.com" 
    Dim cred As New System.Net.NetworkCredential("[email protected]", "myPassword") 
    MM.Credentials = cred 
    MM.Send(YourEmail.Text, "[email protected]", Subject.Text, body) 
    ClearFields() 
    lblEmail.ForeColor = Drawing.Color.Green 
    lblEmail.Text = "Your message has been sent successfuly" 
    lblEmail.Visible = True 

End Sub 

Protected Sub ClearFields() 
    YourName.Text = "" 
    YourEmail.Text = "" 
    Comments.Text = "" 
    Subject.Text = "" 
End Sub 
End Class 

回答

0

你必須提供它,只是把它變成你。基本模式:

Using SmtpServer As New SmtpClient() 
    Using mail As New System.Web.Mail.MailMessage() 
    Try 
     SmtpServer.UseDefaultCredentials = True 
     SmtpServer.EnableSsl = True 
     SmtpServer.Credentials = New Net.NetworkCredential(un, pw) 
     SmtpServer.Port = 25 
     SmtpServer.Host = "smtp.address" 
     mail.From = New MailAddress(sender) 
     mail.To.Add(some address) 
     mail.Subject = subject 
     mail.Body = body 
     mail.Sender = New MailAddress(sender) 
     SmtpServer.Send(mail) 
    Catch 
    End Try 
    End Using 
End Using 
+0

沒有爲我工作。它表示'MailMessage'不明確,從命名空間或類型'System.Net.Mail',System.Web.Mail' – user2821331

+0

導入使用完全限定名稱空間。 – OneFineDay

+0

現在確定它說我的用戶名和密碼沒有聲明。它可能無法訪問,由於其保護級別 – user2821331

相關問題