帮助System.Net.Mail

时间:2011-07-13 09:15:27

标签: asp.net vb.net .net-3.5 asp.net-3.5 system.net.mail

我正在尝试从我的网页发送电子邮件,所以我试过这个:

Imports System.Net.Mail

Dim mSmtpClient As SmtpClient
Dim MMsg As MailMessage

Sub SendEmail()
        MMsg = New MailMessage()

        MMsg.From = New MailAddress(txtFrom.Text)
        MMsg.To.Add(txtTo.Text)

        MMsg.Subject = txtSubject.Text
        MMsg.Body = txtBody.Text

        MMsg.IsBodyHtml = True
        MMsg.Priority = MailPriority.Normal

        mSmtpClient = New SmtpClient("host_name_goes_here")
        mSmtpClient.Send(MMsg)
End Sub

但这不起作用。我一直收到以下错误:

Type 'SmtpClient' is not defined.

Line 7: Dim mSmtpClient As SmtpClient

任何人都知道我做错了什么?

我看过:

http://msdn.microsoft.com/en-us/library/system.net.mail(v=VS.90).aspx

但似乎无法弄明白。

2 个答案:

答案 0 :(得分:1)

这是新代码......

Dim mSmtpClient As New SmtpClient
Dim MMsg As New MailMessage

Sub SendEmail()
    MMsg = New MailMessage()

    MMsg.From = New MailAddress(txtFrom.Text)
    MMsg.To.Add(txtTo.Text)

    MMsg.Subject = txtSubject.Text
    MMsg.Body = txtBody.Text

    MMsg.IsBodyHtml = True
    MMsg.Priority = MailPriority.Normal

    mSmtpClient = New SmtpClient("host_name_goes_here")
    mSmtpClient.Send(MMsg)
End Sub

答案 1 :(得分:0)

您的代码需要位于容器中,如类或模块。所以试试这个:

Imports System.Net.Mail

public module mymail

    Dim mSmtpClient As SmtpClient
    Dim MMsg As MailMessage

    Sub SendEmail()
        MMsg = New MailMessage()

        MMsg.From = New MailAddress(txtFrom.Text)
        MMsg.To.Add(txtTo.Text)

        MMsg.Subject = txtSubject.Text
        MMsg.Body = txtBody.Text

        MMsg.IsBodyHtml = True
        MMsg.Priority = MailPriority.Normal

        mSmtpClient = New SmtpClient("host_name_goes_here")
        mSmtpClient.Send(MMsg)
    End Sub
end module

如果您愿意,也可以使用课程。

西蒙