2012-04-09 77 views
0

我在此代碼指定的字符串不是一個電子郵件地址

Imports Microsoft.VisualBasic 
Imports System.Net.Mail 
Public Class SendEmail 

    Private _Mailto As String = "" 
    Public Property Mailto() As String 
     Get 
      Return _Mailto 
     End Get 
     Set(ByVal value As String) 
      _Mailto = value 
     End Set 
    End Property 
    Private _MailSub As String = "" 
    Public Property MailSub As String 
     Get 
      Return _MailSub 
     End Get 
     Set(ByVal value As String) 
      _MailSub = value 
     End Set 
    End Property 
    Private _MailBody As String = "" 
    Public Property MailBody As String 
     Get 
      Return _MailBody 
     End Get 
     Set(ByVal value As String) 
      _MailBody = value 
     End Set 
    End Property 
    Private _Msg As String = "" 
    Public ReadOnly Property Msg As String 
     Get 
      Return _Msg 
     End Get 
    End Property 


    Public Sub email() 
     Try 
      Dim mail As New MailMessage() 
      Dim SmtpServer As New SmtpClient("smtp.gmail.com") 
      mail.From = New MailAddress("[email protected]") 
      mail.[To].Add(_Mailto) 
      mail.Subject = _MailSub 
      mail.Body = _MailBody 
      ' mail.Headers.Add("In-Reply-To", 1) 
      SmtpServer.Port = 587 
      SmtpServer.Credentials = New System.Net.NetworkCredential("[email protected]", "password") 

      SmtpServer.EnableSsl = True 
      SmtpServer.Send(mail) 

      _Msg = " Check Your Mail " 
     Catch ex As Exception 
      _Msg = ex.Message 
     End Try 

    End Sub 
End Class 

和異常有問題所要求的形式是:指定的字符串不是在一封所要求的形式郵件地址:任何靈魂?

回答

0

看起來你正在初始化_MailTo爲一個空字符串(「」),然後將「To」電子郵件地址設置爲該值。除非您將該字段的值設置爲某個有效的電子郵件地址,否則電子郵件發件人將無法將空字符串識別爲有效的電子郵件地址。

(如果要設置從給定的代碼之外這個mailto屬性,您需要提供代碼來顯示你在哪裏設置它,什麼值。)

+0

這段代碼中存在的形式和組成按鈕,文本框被稱爲 「txtemail」 保護小組Send_Click的(BYVAL發件人爲對象,BYVALË作爲System.EventArgs)把手Send.Click 昏暗objemail作爲新SendEmail objemail.Mailto = txtEmail.Text objemail.email() LabelEmail.Text =「電子郵件已發送」 End Sub – 2012-04-09 03:50:32

+0

@Alaahafethburghall:請更新您剛剛發佈的問題。代碼在評論區非常難以閱讀。 – StriplingWarrior 2012-04-09 03:53:40

+1

@Alaahafethburghall:另外,在單擊發送按鈕之前,顯示您在文本框中提供的輸入內容。您是否已經在調試器中逐步瞭解了代碼運行時_MailTo值的值? – StriplingWarrior 2012-04-09 03:57:16

相關問題