Send Email and Attachments with asp.net ...

Source: Internet
Author: User
Tags add closing tag file system mail mailmessage
ASP.net in this example you'll have to send emails and one way of sending attachments from within a. 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 Can learn a little about that. NET control.
The example uses an ASPX page as a code-behind page. We'll look in the ASPX page. Notice the opening tag of the Panel control near. Its properties should is pretty much self explanatory. The closing tag for the panel is near the bottom of the page. Notice also the enctype line. The enctype is necessary because we are 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 the using only it browsing component to locate attachments/may want to include.

The rest of the page are simple textboxes to hold the elements of a email such as from (which needs to being 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.

<body>
<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>
&LT;TD align= "right" >From:</td>
&LT;TD align= "left" ><asp:textbox id= "Txtfrom" runat= "Server"/></td>
</tr>
<tr>
&LT;TD align= "right" >To:</td>
&LT;TD align= "left" ><asp:textbox id= "Txtto" runat= "Server"/></td>
</tr>
<tr>
&LT;TD align= "right" >Subject:</td>
&LT;TD align= "left" ><asp:textbox id= "Txtsubject" runat= "Server"/></td>
</tr>
<tr>
&LT;TD align= "right" >message body:</td>
&LT;TD align= "Left" >
<asp:textbox id= "Txtmessage" textmode= "MultiLine" rows= "6"
runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD align= "right" >Priority:</td>
&LT;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>
&LT;TD align= "right" >CC:</td>
&LT;TD align= "left" ><asp:textbox id= "TXTCC" runat= "Server"/></td>
</tr>
<tr>
&LT;TD align= "right" >BCC:</td>
&LT;TD align= "left" ><asp:textbox id= "TXTBCC" runat= "Server"/></td>
</tr>
<tr>
&LT;TD align= ' right ' >select File to Attach:</td>
&LT;TD align= "Left" >
<input type= "File" id= "txtattachment" runat= "Server" name= "Txtattachment" >
</td>
</tr>
<tr>
&LT;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>
Now for the Code-behind file. 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, we use for browsing for a 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. 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
The Next is the Button_Click event where all the work are done. The top of the code where we dimension the variables and objects we 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's "strpath =" line is inside a try-catch block. If you are are not sending a attachment. Net chokes If you are 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 Me Ssage priority Dropdownlistbox. Notice again that's Attachment object (Objattach) and the OBJMAIL.ATTACHEMENTS.ADD are set within a try-catch block. Once again this are to protect against the possibility, no attachment is being. 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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.