Send Email and Attachments with ASP.Net...

原创 2004年10月06日 20:15:00
By: John Kilgo spacer.gif Date: December 10, 2002spacer.gifdownload.gifspacer.gifDownload the code. spacer.gif <?XML:NAMESPACE PREFIX = ASP />spacer.gifPrinter 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.

<h3>Email with .NET</h3>
<asp:Panel ID="panel1" Runat="Server" Font-Name="Verdana"
          BorderStyle="Ridge" BorderColor="MidnightBlue" BackColor="Ivory"
<form id="Form1" runat="server" enctype="multipart/form-data">
    <td align="right">From:</td>
    <td align="left"><asp:TextBox ID="txtFrom" Runat="Server" /></td>
    <td align="right">To:</td>
    <td align="left"><asp:TextBox ID="txtTo" Runat="Server" /></td>
    <td align="right">Subject:</td>
    <td align="left"><asp:TextBox ID="txtSubject" Runat="Server" /></td>
    <td align="right">Message Body:</td>
    <td align="left">
      <asp:TextBox ID="txtMessage" TextMode="MultiLine" rows="6"
        Runat="Server" />
    <td align="right">Priority:</td>
    <td align="left">
      <asp:DropDownList ID="ddlPriority" Runat="Server">
    <td align="right">CC:</td>
    <td align="left"><asp:TextBox ID="txtCC" Runat="Server" /></td>
    <td align="right">BCC:</td>
    <td align="left"><asp:TextBox ID="txtBCC" Runat="Server" /></td>
    <td align="right">Select File to Attach:</td>
    <td align="left">
      <INPUT type="file" id="txtAttachment" runat="server" NAME="txtAttachment">
    <td colspan="2" align="center">
      <asp:Button ID="btnSend" Text="Send" Type="Submit"
                  OnClick="btnSend_Click" runat="Server" />
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 = ""

      strPath = Path.GetFullPath(postedFile.FileName)
    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
      objMail.Priority = MailPriority.High
    End If
    objMail.Cc = txtCC.Text
    objMail.Bcc = TxtBCC.Text

      objAttach = New MailAttachment(strPath)
    End Try

  End Sub

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

An attempt was made to send an email when no email session has been established,SSIS用sql2008发邮件失败

An attempt was made to send an email when no email session has been established,SSIS用sql2008发邮件失败...
  • lanmao100
  • lanmao100
  • 2010年05月31日 16:50
  • 1650

android send email demo

本文代码基于下面几个类包:compile ‘com.sun.mail:android-mail:1.5.5’ compile ‘com.sun.mail:android-activation:1.5...
  • aikongmeng
  • aikongmeng
  • 2017年02月28日 15:44
  • 381


有些程序需要运行很久才能完成,希望运行完成时,发送微信提醒。方法之一:先发送到公司邮箱,然后微信提醒你公司邮箱有新的邮件了。Matlab函数: function send2email(MailAd...
  • Quant19861127
  • Quant19861127
  • 2015年01月25日 18:16
  • 1038

Send Email

library(mailR) sender
  • Maro_13th
  • Maro_13th
  • 2017年11月28日 16:03
  • 59

send email

MailAddress from = new MailAddress("", "高升"); //邮件的发件人 MailMessage mail = ...
  • dingxingmei
  • dingxingmei
  • 2012年08月23日 12:47
  • 323

Java sendemail

jar  在下面自己下载 package org.mail; import java.util.Properties; import javax.activation.DataHandler...
  • Demon_311
  • Demon_311
  • 2015年10月23日 09:57
  • 507

send email using telnet

Open your command prompt.Now, connect with telnet using the following command: telnet 25...
  • tina_tian1
  • tina_tian1
  • 2017年06月05日 14:50
  • 187

send email

send emailrequest.setCharacterEncoding("gbk");Show show=new Show();Conn con=new Conn();boolean blnEr...
  • dugu666
  • dugu666
  • 2009年03月27日 14:44
  • 411

Example of Sending email with attachments in C#

//file path string file_ddr_service_agreement = HttpContext.Current.Server.MapPath(@"~\service_sol...
  • lucifer517
  • lucifer517
  • 2013年10月16日 13:37
  • 601

Generate PDF from Batch Class and schedule class and send email notification with Pdf attachment

Generate PDF from Batch Class and schedule class and send email notification with Pdf attachment ...
  • Salesforce_Samba
  • Salesforce_Samba
  • 2015年04月28日 22:55
  • 386
您举报文章:Send Email and Attachments with ASP.Net...