我正在尝试从我的网页发送电子邮件,所以我试过这个:
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
但似乎无法弄明白。
答案 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
如果您愿意,也可以使用课程。
西蒙