This page I use to send e-mails and offers you the possibility to attach a file to your message and also you can specify how to send it (as
1 <%@ Page Language="VB" ValidateRequest="false" %>
2
3 <%@ Import Namespace="System.Net.Mail" %>
4 <%@ Import Namespace="System.Net.Mail.SmtpClient" %>
5 <%@ Import Namespace="System.IO" %>
6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7
8 <script runat="server">
9 Private Sub ClearForm()
10 Me.SenderTextBox.Text = ""
11 Me.SubjectTextBox.Text = ""
12 Me.MessageTextBox.Text = ""
13 End Sub
14
15 Protected Overrides Sub OnLoadComplete(ByVal e As System.EventArgs)
16 MyBase.OnLoadComplete(e)
17 '// Clear the form
18 ClearForm()
19 End Sub
20
21 Protected Sub sendMessageButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendMessageButton.Click
22
23 '// Grab the data for your message
24 Dim _theSender As String = Me.SenderTextBox.Text
25
26 Dim _theReceiver As String = "" '// "your e-mail address here" '// e.g. someone@example.com
27 Dim _theSubject As String = Me.SubjectTextBox.Text
28 Dim _theMessage As String = Me.MessageTextBox.Text
29
30 '// Compose the message
31 Dim theMessage As New MailMessage(_theSender, _theReceiver, _theSubject, _theMessage)
32 theMessage.Priority = MailPriority.Normal
33
34 '// This should be your internet provider mail server address
35 Dim theHost As New SmtpClient("")
36
37 '// Create the attachment
38 Dim attachedFile As Attachment = New Attachment(Me.AttachmentFileUpload.PostedFile.InputStream, Me.AttachmentFileUpload.FileName)
39
40 Try
41 If (Me.SendAsHtmlRadioButton.Checked = True) Then
42 Try
43 '// Verify if file attached
44 If Not (String.IsNullOrEmpty(Me.AttachmentFileUpload.FileName) OrElse (Me.AttachmentFileUpload.PostedFile Is Nothing)) Then
45 '// Attach the file
46 theMessage.Attachments.Add(attachedFile)
47 Else
48 '// There is no need to treat this error here.
49 '// the RequiredFieldValidator will do the job
50 End If
51
52 '// Get your message content and declare it as HTML content
53 theMessage.Body = Me.MessageTextBox.Text
54 theMessage.IsBodyHtml = True
55
56 '// Send the message
57 theHost.Send(theMessage)
58
59 ClearForm()
60 '// Show success message
61 Me.ErrorMessageLabel.Text = "Success: The message was sent"
62
63 Catch ex As Exception
64 '// Show error message
65 Me.ErrorMessageLabel.Text = "Error: The message was not sent"
66
67 End Try
68
69 ElseIf (Me.SendAsTextRadioButton.Checked = True) Then
70 Try
71 '// Verify if file attached
72 If Not (String.IsNullOrEmpty(Me.AttachmentFileUpload.FileName) OrElse (Me.AttachmentFileUpload.PostedFile Is Nothing)) Then
73 '// Attach the file
74 theMessage.Attachments.Add(attachedFile)
75 Else
76 '// There is no need to treat this error here.
77 '// the RequiredFieldValidator will do the job
78 End If
79
80 '// Get your message content and declare it as plain Text content
81 theMessage.Body = Me.MessageTextBox.Text
82 theMessage.IsBodyHtml = False
83
84 '// Send the message
85 theHost.Send(theMessage)
86
87 ClearForm()
88 '// Show success message
89 Me.ErrorMessageLabel.Text = "Success: The message was sent"
90
91 Catch ex As Exception
92 '// Show error message
93 Me.ErrorMessageLabel.Text = "Error: The message was not sent"
94 End Try
95 End If
96
97 Catch ex As Exception
98 '// Show error message
99 Me.ErrorMessageLabel.Text = "Error: The message was not sent"
100
101 End Try
102
103 End Sub
104
105 </script>
106
107 <html xmlns="http://www.w3.org/1999/xhtml">
108 <head id="Head1" runat="server">
109 <title>How to send an e-mail message</title>
110 <style type="text/css" media="screen">
111
112 body
113 {
114 font-family: Verdana, Sans-Serif;
115 font-size: 9pt;
116 color: #000000;
117 text-align: center;
118 vertical-align: top;
119 }
120 .ctSpacer
121 {
122 height: 10px;
123 }
124 FORM#AppMailForm
125 {
126 width: 100%;
127 height: auto;
128 }
129 div.ctWrapper
130 {
131 width: 700px;
132 height: auto;
133 text-align: center;
134 vertical-align: top;
135 }
136 TD#ContactFormContainerCell
137 {
138 width: 100%;
139 height: auto;
140 text-align: center;
141 vertical-align: top;
142 }
143 table.ctTableContact
144 {
145 width: 100%;
146 height: auto;
147 text-align: left;
148 vertical-align: top;
149 }
150 TD.ctPageTitle
151 {
152 width: 100%;
153 height: 35px;
154 text-align: left;
155 vertical-align: middle;
156 padding-left: 15px;
157 background-color: #eeeeee;
158 border-bottom: solid 2px #cccccc;
159 }
160 .ctPageTitleLabel
161 {
162 font-family: Georgia, Serif;
163 font-size: 10pt;
164 color: #900000;
165 font-weight: bold;
166 }
167 .TextCell
168 {
169 width: 100%;
170 height: 20px;
171 text-align: left;
172 vertical-align: middle;
173 }
174 .LabelControls
175 {
176 font: 9pt Verdana, Sans-Serif;
177 color: #434041;
178 }
179 .InputCell
180 {
181 width: 100%;
182 height: 25px;
183 text-align: left;
184 vertical-align: middle;
185 }
186 .InputCell input
187 {
188 text-align: left;
189 vertical-align: middle;
190 color: #434041;
191 }
192 .validatorHand
193 {
194 cursor: hand;
195 }
196 td.Note span
197 {
198 font-family: Verdana, Sans-Serif;
199 font-size: 11pt;
200 font-weight: bold;
201 color: #000000;
202 }
203 .infoMessage
204 {
205 font: 8pt Verdana, Sans-Serif;
206 color: #434041;
207 }
208 input.SendMessageButton
209 {
210 text-align: center;
211 vertical-align: middle;
212 }
213 #AllControlsValidationSummary
214 {
215 width: 99%;
216 height: auto;
217 text-align: left;
218 vertical-align: top;
219 padding-left: 5px;
220 padding-top: 5px;
221 }
222 </style>
223 </head>
224 <body>
225 <form id="AppMailForm" runat="server" method="post" enctype="multipart/form-data">
226 <div class="ctWrapper">
227 <table class="ctTableContact" cellpadding="0" cellspacing="0">
228 <thead>
229 <tr id="MAIL_HEADER">
230 <td class="ctPageTitle">
231 <asp:Label ID="PageTitleLabel" runat="server" CssClass="ctPageTitleLabel">Send Mail Message</asp:Label>
232 </td>
233 </tr>
234 <tr>
235 <td class="ctSpacer">
236 </td>
237 </tr>
238 </thead>
239 <tbody id="MAIL_CONTENT">
240 <tr>
241 <td id="ContactFormContainerCell">
242 <table class="ctTableContact" cellpadding="2" cellspacing="2">
243 <tbody>
244 <tr>
245 <td id="Td1" class="TextCell">
246 <asp:Label ID="FromLabel" runat="server" Text="Sender:" CssClass="LabelControls"></asp:Label>
247 <asp:RequiredFieldValidator ID="RequiredFieldValidatorSenderTextBox" runat="server"
248 ControlToValidate="SenderTextBox" CssClass="validatorHand" ErrorMessage='You must provide an e-mail address! This field is required.'
249 Font-Names="Verdana" Font-Size="12pt" ForeColor="#FF0000" ToolTip='You must provide an e-mail address!'>*</asp:RequiredFieldValidator></td>
250 </tr>
251 <tr>
252 <td id="Td2" class="InputCell">
253 <asp:TextBox ID="SenderTextBox" runat="server" Width="200px"></asp:TextBox>
254 <asp:RegularExpressionValidator ID="RegularExpressionValidatorSenderTextBox" runat="server"
255 ControlToValidate="SenderTextBox" ErrorMessage='Your e-mail address is not in the right format!'
256 Font-Names="Verdana" Font-Size="12pt" ForeColor="#ff0000" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator></td>
257 </tr>
258 <tr>
259 <td id="Td5" class="TextCell">
260 <asp:Label ID="SubjectLabel" runat="server" CssClass="LabelControls" Text="Subject:"></asp:Label>
261 <asp:RequiredFieldValidator ID="RequiredFieldValidatorSubjectTextBox" runat="server"
262 ControlToValidate="SubjectTextBox" CssClass="validatorHand" ErrorMessage='You must provide a subject to your message! This field is required.'
263 Font-Names="Verdana" Font-Size="12pt" ForeColor="#ff0000" ToolTip='You must provide a subject to your message!'>*</asp:RequiredFieldValidator></td>
264 </tr>
265 <tr>
266 <td id="Td6" class="InputCell">
267 <asp:TextBox ID="SubjectTextBox" runat="server" Width="300px"></asp:TextBox>
268 </td>
269 </tr>
270 <tr>
271 <td id="Td7" class="TextCell">
272 <asp:Label ID="MesssageLabel" runat="server" CssClass="LabelControls" Text="Message:"></asp:Label>
273 <asp:RequiredFieldValidator ID="RequiredFieldValidatorMessageTextBox" runat="server"
274 ControlToValidate="MessageTextBox" CssClass="validatorHand" ErrorMessage='You cannot send a blank message! This field is required.'
275 Font-Names="Verdana" Font-Size="12pt" ForeColor="#ff0000" ToolTip='You cannot send a blank message!'>*</asp:RequiredFieldValidator></td>
276 </tr>
277 <tr>
278 <td id="Td8" class="InputCell">
279 <asp:TextBox ID="MessageTextBox" runat="server" Height="123px" TextMode="MultiLine"
280 Width="458px" MaxLength="500"></asp:TextBox>
281 <br />
282 <asp:Label ID="InfoLabel" runat="server" CssClass="infoMessage">500 chars max</asp:Label>
283 </td>
284 </tr>
285 <tr>
286 <td class="InputCell">
287 <asp:RadioButton ID="SendAsHtmlRadioButton" runat="server" Checked="true" Text="Send as HTML"
288 GroupName="MailingOptions" />
289 <asp:RadioButton ID="SendAsTextRadioButton" runat="server" Checked="false" Text="Send as Text"
290 GroupName="MailingOptions" /></td>
291 </tr>
292 <tr>
293 <td id="AttachmentFileUploadCell" runat="server" class="InputCell">
294 <asp:FileUpload ID="AttachmentFileUpload" runat="server" Width="458px" />
295 <asp:RequiredFieldValidator ID="UploaderRequiredFieldValidator" runat="server" ControlToValidate="AttachmentFileUpload"
296 CssClass="validatorHand" ErrorMessage="No file was attached to your message. This field is required."
297 ToolTip="This field is required.">*</asp:RequiredFieldValidator></td>
298 </tr>
299 <tr>
300 <td class="InputCell">
301 <asp:Label ID="ErrorMessageLabel" runat="server" ForeColor="red"></asp:Label>
302 </td>
303 </tr>
304 <tr>
305 <td id="Td9" class="InputCell">
306 <asp:Button ID="SendMessageButton" CssClass="SendMessageButton" runat="server" Text="Send" />
307 </td>
308 </tr>
309 </tbody>
310 <tfoot>
311 <tr>
312 <td class="CaptionContentCell">
313 <asp:ValidationSummary ID="AllControlsValidationSummary" runat="server" BackColor="#eeeeee"
314 BorderColor="#cccCCC" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana, Sans-Serif"
315 Font-Size="8pt" ForeColor="#ff0000" HeaderText="Fix the following errors:" />
316 </td>
317 </tr>
318 </tfoot>
319 </table>
320 </td>
321 </tr>
322 </tbody>
323 </table>
324 </div>
325 </form>
326 </body>
327 </html>