开发者

Help with System.Net.Mail

I'm trying to send emails from my webpage, so I have tried this:

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

But this is not working. I keep getting the following error:

Type 'SmtpClient' is not defined.

Line 7: Dim mSmtpClie开发者_JAVA百科nt As SmtpClient

Anyone know what I am doing wrong?

I have looked at:

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

But can't seem to figure it out.


here is new code...

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


Your code needs to be in a container, like a class or a module. So try this:

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

You could also use a class if you wanted.

Simon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜