Send Email and Attachments with ASP.Net...

asp.net

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.

时间: 2024-08-02 19:52:32

Send Email and Attachments with ASP.Net...的相关文章

Send email in oracle

  1. CREATE PACKAGE   CREATE OR REPLACE PACKAGE demo_mail IS ----------------------- Customizable Section ----------------------- -- Customize the SMTP host, port and your domain name below.smtp_host VARCHAR2(256) := 'smtp.eygle.com';smtp_port PLS_IN

using System.Web.Mail to send email

there are many demos for send a email with c sharp language .however,you will find that most of them will be failed because of SMTPServer.Especially,authorization is required.   public void SendEMail()        ...{            MailMessage mail = new Ma

python auto send email

/*************************************************************************** * python auto send email * 声明: * 本文主要是记录如何使用python的smtplib发邮件,中间遇到授权密码和邮箱 * 密码不同的问题. * * 2016-2-17 深圳 南山平山村 曾剑锋 *************************************************************

请教APPIAN 6 send e-mail的问题

问题描述 使用了一个sende-mail的节点,流程顺利执行了~但是却收不到邮件,APPIAN的邮件设置在哪里呢?还是需要和outlook结合呢? 解决方案 解决方案二:该回复于2011-03-14 11:53:34被版主删除

ASP.NET中发送Email完整实例(转)

asp.net ASP.NET中发送Email完整实例 本文举例说明在ASP.NET中发送Email的众多可能性,内容覆盖了诸如Email格式.优先权.附件及Email编码等方面. ASP.NET被赋予了一个发送Email的新对象,名为SmtpMail.使用SmtpMail对象从ASP.NET页面中发送Email时,可以遵循以下简单步骤: ▲包含与邮件有关类所需要的名称空间:▲例示一个信息对象,设置属性:▲使用SmtpMail对象实例的send方法发送邮件. 现在我们就来一步一步地研究从一个AS

Sending Email via ASP.NET and CDONTS[vs bate2 等级 中、高]

asp.net <%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False" Strict="True" %><%@ Import Namespace="System.Web.Mail" %> <s

Sending Email via ASP.NET and CDONTS[vs bate2 等级 中

<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False" Strict="True" %><%@ Import Namespace="System.Web.Mail" %> <script la

ASP.NET学习篇(3)——几个简单的ASP.ENT的例子

一个WEBFORM--这里将介绍ASP.NET WEBFORM的基本概念,以及其中表单项的变化. *一个数据库的应用--如何利用CONFIG.WEB中设置的DNS连接数据库,数据库操作对象的一些基本用法. *EMAIL发送--在 引入一个系统类库后(用IMPORT 标识符 ),利用msgMail对象,可以很方便的操作邮件发送过程. *上传--<INPUT TYPE=FILE -- 这样一个INPUT 项在ASP.NET中有POSTEDFILE属性,结合SAVEAS 事件,就可以实现上传了. 一.

一年了,用ASP.NET2.0就没有发送成功一个邮件,我的天!

问题描述 我的代码:if(TextBox1.Text.IndexOf("@")>-1)//判断收信地址集是否有效{MailMessagemm=newMailMessage();mm.To.Add(newMailAddress(TextBox1.Text));mm.From=newMailAddress(TextBox2.Text);mm.Subject=TextBox5.Text;mm.Body=TextBox6.Text;mm.SubjectEncoding=System.Te