转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
Send Email and Attachments with ASP.Net...         ★★★★

Send Email and Attachments with ASP.Net...

作者:闵涛 文章来源:闵涛的学习笔记 点击数:857 更新时间:2009/4/23 10:44:58

By: John Kilgo Date: December 10, 2002 Download the code. Printer Friendly Version

In this example you will see how to send email and one way of sending attachments from within an .aspx file. The text boxes for the various components of the email are wrapped in a panel control, so if you have not used panels, you can learn a little about that .net control.

The example uses an aspx page as well as a code-behind page. We will first look at the aspx page. Notice the opening tag of the panel control near the top of the program. Its properties should be pretty much self explanatory. The closing tag for the panel is near the bottom of the page. Notice also the enctype attribute on the <form> line. The enctype is necessary because we are using the htmlinput control to locate attachments. HtmlInput is normally used for uploading files from your file system to a server. Here, however, we are using only its browsing component to locate attachments you may want to include.

The rest of the page are simple TextBoxes to hold the elements of an email such as From (which needs to be in email format), To, Subject, etc. A button is also included to initiate the send operation. The code-behind page is mainly the click event code for the submit button.

<html>
<body>
<h3>Email with .NET</h3>
<asp:Panel ID="panel1" Runat="Server" Font-Name="Verdana"
          BorderStyle="Ridge" BorderColor="MidnightBlue" BackColor="Ivory"
          width="50%">
<form id="Form1" runat="server" enctype="multipart/form-data">
<table>
  <tr>
    <td align="right">From:</td>
    <td align="left"><asp:TextBox ID="txtFrom" Runat="Server" /></td>
  </tr>
  <tr>
    <td align="right">To:</td>
    <td align="left"><asp:TextBox ID="txtTo" Runat="Server" /></td>
  </tr>
  <tr>
    <td align="right">Subject:</td>
    <td align="left"><asp:TextBox ID="txtSubject" Runat="Server" /></td>
  </tr>
  <tr>
    <td align="right">Message Body:</td>
    <td align="left">
      <asp:TextBox ID="txtMessage" TextMode="MultiLine" rows="6"
        Runat="Server" />
    </td>
  </tr>
  <tr>
    <td align="right">Priority:</td>
    <td align="left">
      <asp:DropDownList ID="ddlPriority" Runat="Server">
        <asp:ListItem>Low</asp:ListItem>
        <asp:ListItem>Normal</asp:ListItem>
        <asp:ListItem>High</asp:ListItem>
      </asp:DropDownList>
    <td>
  </tr>
  <tr>
    <td align="right">CC:</td>
    <td align="left"><asp:TextBox ID="txtCC" Runat="Server" /></td>
  </tr>
  <tr>
    <td align="right">BCC:</td>
    <td align="left"><asp:TextBox ID="txtBCC" Runat="Server" /></td>
  </tr>
  <tr>
    <td align="right">Select File to Attach:</td>
    <td align="left">
      <INPUT type="file" id="txtAttachment" runat="server" NAME="txtAttachment">
    </td>
  </tr>
  <tr>
    <td colspan="2" align="center">
      <asp:Button ID="btnSend" Text="Send" Type="Submit"
                  OnClick="btnSend_Click" runat="Server" />
    </td>
  </tr>
</table>
</form>
</asp:Panel>
</body>
</html> Now for the code-behind file. First the NameSpace Imports. Notice that System.Web.Mail is included. Also notice that System.Web.UI.HtmlControls is also included. We must have that to work with the HtmlInput control that we use for browsing for an attachment. System.IO is needed because we must determine the full directory path for the attachment. Imports System
Imports System.Web.Mail
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.IO Next is the declaration of the main class and declaration of the html and web controls we us in the program. Public Class SendEmail : Inherits Page

  Protected txtFrom As TextBox
  Protected txtTo As TextBox
  Protected txtSubject As TextBox
  Protected txtMessage As TextBox
  Protected ddlPriority As DropDownList
  Protected txtCC As TextBox
  Protected txtBCC As TextBox
  Protected txtAttachment As HtmlInputFile Next is the button_click event where all the work is done. First the top of the code where we dimension the variables and objects we will need. We create both MailMessage and a SmtpMail objects. We also create a MailAttachment object. Next we get the contents of the HtmlInput control (postedFile) and dimension a variable to hold the path to the attachment. Notice that the "strPath =" line is inside a try-catch block. If you are not sending an attachment .Net chokes if you try to get the path to a null attachment object.   Public Sub btnSend_Click(Sender As Object, e As EventArgs)
    Dim objMail As New MailMessage
    Dim objConn As SmtpMail
    Dim objAttach As MailAttachment
    Dim postedFile = txtAttachment.PostedFile
    Dim strPath As String = ""

    Try
      strPath = Path.GetFullPath(postedFile.FileName)
    Catch
    End Try Now for the real work. For the most part properties of the MailMessage object (objMail) are being set to the contents of the textboxes and the message priority DropDownListBox. Notice again that the attachment object (objAttach) and the objMail.Attachements.Add are set within a Try-Catch block. Once again this is to protect against the possibility that no attachment is being sent.     objMail.From = txtFrom.Text
    objMail.To = txtTo.Text
    objMail.Subject = txtSubject.Text
    objMail.Body = txtMessage.Text
    If ddlPriority.SelectedItem.Text = "Low" Then
      objMail.Priority = MailPriority.Low
    ElseIf ddlPriority.SelectedItem.Text = "Normal" Then
      objMail.Priority = MailPriority.Normal
    Else
      objMail.Priority = MailPriority.High
    End If
    objMail.Cc = txtCC.Text
    objMail.Bcc = TxtBCC.Text

    Try
      objAttach = New MailAttachment(strPath)
      objMail.Attachments.Add(objAttach)
    Catch
    End Try

    objConn.Send(objMail)
  End Sub

End Class Conclusion: You have just seen most of the properties and methods of the Mail and Smtp objects at work.


[电脑技术]使用net send在网络发送消息出现网络上找不到此消…  [聊天工具]使用net send命令在局域网内发消息__天极Yesky
[聊天工具]NetTone 电话、短信、聊天、EMAIL样样通__天极Yes…  [聊天工具]用32bit Email Broadcaster群发邮件__天极Yesky
[系统软件]Explanation of UFT-8 and Unicode  [系统软件]Using dllimport and dllexport in C++ Classes
[常用软件]神奇 我家的照片会唱歌 照片会唱歌  [VB.NET程序]VB.NET and C# 语法比较手册
[Delphi程序]IntraWeb And ActiveForms  [Delphi程序]different between BPL and DLL
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台