System.Array发送电子邮件的ASP.NET应用程序错误

时间:2015-02-27 18:37:47

标签: asp.net vb.net sendmail

我在我创建的应用程序中收到一组错误,应该发送电子邮件。任何帮助将不胜感激。谢谢。 以下是一组错误:

错误1'SmtpServer'不是'System.Net.Mail.SmtpClient'的成员。 96 9 SendingEmail

错误2'To'不是'System.Array'的成员。 100 9 SendingEmail

错误3'from'不是'System.Array'的成员。 104 9 SendingEmail

错误4'isBodyHtml'不是'System.Array'的成员。 109 13发送电子邮件

错误5未声明名称“MailFormat”。 109 34 SendingEmail

错误6'BodyFormat'不是'System.Array'的成员。
111 13 SendingEmail

错误7名称'MailFormat'未声明。 111 34 SendingEmail

错误8“主题”不是“System.Array”的成员。 120 9 SendingEmail

错误9'附件'不是'System.Array'的成员。 125 13 SendingEmail

错误10“Body”不是“System.Array”的成员。 129 9 SendingEmail

错误11类型'System.Net.Mail.MailMessage的1维数组'的值无法转换为'System.Net.Mail.MailMessage'.132 18 SendingEmail

Imports System.Net.Mail

Public Class SendEmail
    Inherits System.Windows.Forms.Form
    ' Variable which will send the mail
    Dim obj As  _
    System.Net.Mail.SmtpClient

'Variable to store the attachments 
Dim Attachment As System.Net.Mail.Attachment

'Variable to create the message to send
Dim MailMsg As System.Net.Mail.MailMessage()

 Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As      System.EventArgs) Handles btnSend.Click
    Dim Counter As Integer

    'Validate the data
    If txtSMTPServer.Text = "" Then
        MsgBox("Enter the SMTP server info ...!!!", _
        MsgBoxStyle.Information, "Send Email")
        Exit Sub
    End If

    If txtFrom.Text = "" Then
        MsgBox("Enter the From email address ...!!!", _
               MsgBoxStyle.Information, "Send Email")
        Exit Sub
    End If

    If txtTo.Text = "" Then
        MsgBox("Enter the Recipient email address ...!!!", _
                 MsgBoxStyle.Information, "Send Email")
        Exit Sub
    End If

    If txtSubject.Text = "" Then
        MsgBox("Enter the Email subject ...!!!", _
                 MsgBoxStyle.Information, "Send Email")
        Exit Sub
    End If

    'Set the properties
    'Assign the SMTP server
    obj.SmtpServer = txtSMTPServer.Text

    'Multiple recepients can be specified using ; as the delimeter
    'Address of the recipient
    MailMsg.To = txtTo.Text

    'Your From Address
    'You can also use a custom header Reply-To for a different replyto address
    MailMsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"


    'Specify the body format
    If chkFormat.Checked = True Then
        MailMsg.isBodyHtml = MailFormat.Html 'Send the mail in HTML Format
    Else
        MailMsg.BodyFormat = MailFormat.Text
    End If

    'If you want you can add a reply to header 
    'Mailmsg.Headers.Add("Reply-To", "testmail@mail.com")
    'custom headersare added like this
    'Mailmsg.Headers.Add("Manoj", "TestHeader")

    'Mail Subject
    MailMsg.Subject = txtSubject.Text
    'Attach the files one by one
    For Counter = 0 To lstAttachment.Items.Count - 1
        Attachment = New Net.Mail.Attachment(lstAttachment.Items(Counter))
        'Add it to the mail message
        MailMsg.Attachments.Add(Attachment)
    Next

    'Mail Body
    MailMsg.Body = txtMessage.Text

    'Call the send method to send the mail
    obj.Send(MailMsg)
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

这一行是一个问题:Dim MailMsg As System.Net.Mail.MailMessage() 我在下面改为阅读

Dim MailMsg As New System.Net.Mail.MailMessage()

在第一个声明中,括号声明了一个MailMsg数组。添加New会产生有效的构造函数。

Imports System.Net.Mail

Public Class SendEmail
    Inherits System.Windows.Forms.Form
    ' Variable which will send the mail
    Dim obj As  _
    System.Net.Mail.SmtpClient

'Variable to store the attachments 
Dim Attachment As System.Net.Mail.Attachment

'Variable to create the message to send
Dim MailMsg As New System.Net.Mail.MailMessage()

 Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As      System.EventArgs) Handles btnSend.Click
    Dim Counter As Integer

    'Validate the data
    If txtSMTPServer.Text = "" Then
        MsgBox("Enter the SMTP server info ...!!!", _
        MsgBoxStyle.Information, "Send Email")
        Exit Sub
    End If

    If txtFrom.Text = "" Then
        MsgBox("Enter the From email address ...!!!", _
               MsgBoxStyle.Information, "Send Email")
        Exit Sub
    End If

    If txtTo.Text = "" Then
        MsgBox("Enter the Recipient email address ...!!!", _
                 MsgBoxStyle.Information, "Send Email")
        Exit Sub
    End If

    If txtSubject.Text = "" Then
        MsgBox("Enter the Email subject ...!!!", _
                 MsgBoxStyle.Information, "Send Email")
        Exit Sub
    End If

    'Set the properties
    'Assign the SMTP server
    obj.SmtpServer = txtSMTPServer.Text

    'Multiple recepients can be specified using ; as the delimeter
    'Address of the recipient
    MailMsg.To = txtTo.Text

    'Your From Address
    'You can also use a custom header Reply-To for a different replyto address
    MailMsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"


    'Specify the body format
    If chkFormat.Checked = True Then
        MailMsg.isBodyHtml = MailFormat.Html 'Send the mail in HTML Format
    Else
        MailMsg.BodyFormat = MailFormat.Text
    End If

    'If you want you can add a reply to header 
    'Mailmsg.Headers.Add("Reply-To", "testmail@mail.com")
    'custom headersare added like this
    'Mailmsg.Headers.Add("Manoj", "TestHeader")

    'Mail Subject
    MailMsg.Subject = txtSubject.Text
    'Attach the files one by one
    For Counter = 0 To lstAttachment.Items.Count - 1
        Attachment = New Net.Mail.Attachment(lstAttachment.Items(Counter))
        'Add it to the mail message
        MailMsg.Attachments.Add(Attachment)
    Next

    'Mail Body
    MailMsg.Body = txtMessage.Text

    'Call the send method to send the mail
    obj.Send(MailMsg)
End Sub
End Class