Send email and attachments with asp.net ...

xiaoxiao2021-03-06  67

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

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

Email with .NET

< TD align = "right"> bcc:
from:
to:
Subject:
message body:
priority: LOW Normal High
cc:
select file to attach:
now for the code-behind file. First The namespace imports. NOTICE THASTEM.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 systemimports system.web.mailimports system.webimports system.web.ui.webcontrolsimports system.web.ui.htmlcontrolsimports 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 HtmlInputFileNext 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. NOTH THATHE "strpath =" Line is inside a try-catch block if you used 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 (Objmail) End Subend ClassConclusion: You Have Just Seen Most of The Properties and Methods of the Mail and SMTP Objects at work.

转载请注明原文地址:https://www.9cbs.com/read-110041.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.047, SQL: 9