怎样在asp.net(VB)应用程序中加入电子邮件功能

goincsdn 2003-07-24 09:37:15
我的问题是这样的:
在页面中填写好了表单之后点击“发送”(先不考虑验证表单)
就将用户填写的全部表单的内容发送给特定的邮件接收者
我的意思是不调用电子邮件程序(outlook,foxmail......)而直接发送给特定的邮件接收者
谢谢各位!!
...全文
79 12 打赏 收藏 举报
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
goincsdn 2003-07-26
  • 打赏
  • 举报
回复
up
goincsdn 2003-07-25
  • 打赏
  • 举报
回复
不解????
goincsdn 2003-07-25
  • 打赏
  • 举报
回复
webform1.aspx文件如下:————————————————————————————
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WA1.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
</HEAD>
<body bgColor="silver">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<P><asp:button id="btnsend" runat="server" Text="发送"></asp:button></P>
</FONT>
</form>
</body>
</HTML>

webform1.aspx.vb文件如下:————————————————————————————
Imports System.Web.Mail
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents btnsend As System.Web.UI.WebControls.Button

'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region

Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
Dim email As New MailMessage
email.From = "sombody@some.com"
email.To = "somebody@some.com"
email.Subject = "hello!!!"
email.Body = "测试邮件"
email.BodyFormat = MailFormat.Html
SmtpMail.Send(email)
End Sub
End Class

返回如下错误————————————————————————————————
[NullReferenceException: 未将对象引用设置到对象的实例。]
WA1.WebForm1.btnsend_Click(Object sender, EventArgs e)
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
cgmx 2003-07-25
  • 打赏
  • 举报
回复
关注
goody9807 2003-07-25
  • 打赏
  • 举报
回复
Imports System.web.Mail

objmessage= new MailMessage

objmessage.From="you@somewhere.com"

objmessage.To="he@sina.com"

objmessage.Body="hello!"

objmessage.Subject="I love you!"

SmtpMail.Send(objMailMessage)
Haiding 2003-07-25
  • 打赏
  • 举报
回复
up
xmxjh 2003-07-25
  • 打赏
  • 举报
回复
用system.web.mail命名空间,以下是MS 101 例程中的片段

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
' Perform validation on the To and From email address fields, which are
' both required for sending an email.
Try
ValidateEmailAddress(txtFrom)
Catch ex As Exception
txtFrom.Select(0, txtFrom.Text.Length)

' Set the ErrorProvider error with the text to display.
erpEmailAddresses.SetError(txtFrom, ex.Message)
Exit Sub
End Try

Try
ValidateEmailAddress(txtTo)
Catch exp As Exception
txtTo.Select(0, txtTo.Text.Length)

' Set the ErrorProvider error with the text to display.
erpEmailAddresses.SetError(txtTo, exp.Message)
Exit Sub
End Try

' Use the StringBuilder class instead of traditional string concatenation.
' It is optimized for building strings because it is capable of modifying
' the underlying buffer instead of having to make a copy of the string for
' each concatenation. Consult the SDK documentation for more information on
' this new class.
Dim sb As New StringBuilder()

' Build the email message body.
sb.Append("The following email was sent to you from the Send Mail How-To " & _
"sample application:")
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("SUBJECT: ")
sb.Append(Trim(txtSubject.Text))
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("MESSAGE: ")
sb.Append(Trim(txtBody.Text))
sb.Append(vbCrLf)

' Creating a mail message is as simple as instantiating a class and
' setting a few properties.
Dim mailMsg As New MailMessage()
With mailMsg
.From = txtFrom.Text.Trim
.To = txtTo.Text.Trim
.Cc = txtCC.Text.Trim
.Bcc = txtBCC.Text.Trim
.Subject = txtSubject.Text.Trim
.Body = sb.ToString
.Priority = CType(cboPriority.SelectedIndex, MailPriority)

If Not IsNothing(arlAttachments) Then
Dim mailAttachment As Object
For Each mailAttachment In arlAttachments
.Attachments.Add(mailAttachment)
Next
End If
End With

' Set the SmtpServer name. This can be any of the following depending on
' your local security settings:

' a) Local IP Address (assuming your local machine's SMTP server has the
' right to send messages through a local firewall (if present).

' b) 127.0.0.1 the loopback of the local machine.

' c) "smarthost" or the name or the IP address of the exchange server you
' utilize for messaging. This is usually what is needed if you are behind
' a corporate firewall.

' See the Readme file for more information.
SmtpMail.SmtpServer = "smarthost"

' Use structured error handling to attempt to send the email message and
' provide feedback to the user about the success or failure of their
' attempt.
Try
SmtpMail.Send(mailMsg)
lstAttachments.Items.Clear()
lstAttachments.Items.Add("(No Attachments)")

MessageBox.Show("Your email has been successfully sent!", _
"Email Send Status", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch exp As Exception
MessageBox.Show("The following problem occurred when attempting to " & _
"send your email: " & exp.Message, _
Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
mynewpc 2003-07-25
  • 打赏
  • 举报
回复
这有你要的:http://www.csdn.net/cnshare/soft/15/15377.shtm
pgame2000 2003-07-25
  • 打赏
  • 举报
回复
得需要本机上安装好SMTP吧?
fmj1980 2003-07-25
  • 打赏
  • 举报
回复
Imports System.web.Mail
Dim mail As New MailMessage()
mail.From = EditorsEmail
mail.To = strEmail
mail.Subject = "File Change Notification"
mail.Body = strBody
mail.BodyFormat = MailFormat.Html
SmtpMail.Send(mail)
goincsdn 2003-07-25
  • 打赏
  • 举报
回复
用goody9807的方法产生以下错误
“System.Web.Mail.SmtpMail.Private Sub New()”是“Private”,因此它在此上下文中无法访问。
不解??
ruyedian 2003-07-24
  • 打赏
  • 举报
回复
gz
发帖
.NET社区

6.1w+

社区成员

.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
帖子事件
创建了帖子
2003-07-24 09:37
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。