By: John kilgo date: December 10,200 2 download the code. Printer friendly version
In this example you will see how to send email and one way of sending attachments from within. 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 shoshould 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 encoded ded 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 <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 wrongly ded. also notice that system. web. UI. htmlcontrols is also encoded ded. 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 tx1_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 = txw.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. TextTry 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.