ASP.net MAIL routine WEB MAIL
ASP.net mail routine (Language: "C #"; with EMail address verification)
--------------------------------------------------------------------------------
<% @ Page Language = "C #" debug = "true" Explicit = "True" %>
<% @ Import Namespace = "System. Web. Util" %>
<% @ Import Namespace = "System. Web. Mail" %>
<Script language = "C #" runat = "server">
Public void SendMail (Object Obj, EventArgs E ){
LabelSendMailResult. Text = "";
If (Page. IsValid ){
MailMessage mailObj = new MailMessage ();
// Set the email address 'from' and 'to'
MailObj. From = inputMailFrom. Value;
MailObj. To = inputMailTo. Value;
MailObj. Subject = inputMailSubject. Value;
MailObj. Body = textBoxMailBody. Text;
// Optional: html-format Email
MailObj. BodyFormat = MailFormat. Html;
// Optional: encrypt the email.
// MailObj. BodyEncoding = MailFormat. Base64;
// Optional: set the priority of the email to high.
MailObj. Priority = MailPriority. High;
// Optional: attachment
If (inputMailAttachment. PostedFile. ContentLength> 0 ){
// Note that a MailAttachment object is created to append a file to the email.
MailObj. Attachments. Add (new MailAttachment (inputMailAttachment. PostedFile. FileName ));
}
// Use the SmtpMail object to send the mail.
SmtpMail. Send (mailObj );
LabelSendMailResult. Text = "successfully sent From:" + inputMailFrom. Value + "; To:" + inputMailTo. Value;
If (inputMailAttachment. PostedFile. ContentLength> 0 ){
LabelSendMailResult. text + = "<br> the email contains an attachment:" + inputMailAttachment. postedFile. fileName + ", attachment size:" + (inputMailAttachment. postedFile. contentLength/1024 ). toString () + "K Byte (s )";
}
}
}
</Script>
<Html>
<Head>
<Title>
Send mail ASP. NET </title>
</Head>
<Body>
<Div align = "center">
<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" style = "border-collapse: collapse "bordercolor =" # eeeeee "id =" AutoNumber1 ">
<Form id = "formMail" method = "post" action = "" enctype = "multipart/form-data" runat = "server">
<Tr>
<Td width = "20%" height = "24"> recipient address: </td>
& Lt; td width = "80%" height = "24" & gt;
<Input type = "text" id = "inputMailTo" name = "inputMailTo" runat = "server" size = "48">
<Asp: RequiredFieldValidator
Id = "RequiredFieldValidatorInputMailTo"
ControlToValidate = "inputMailTo"
Display = "Static"
EnableClientScript = "False"
ErrorMessage = "the recipient cannot be blank"
Runat = "server"/>
<Asp: RegularExpressionValidator id = "RegularExpressionValidatorInputMailTo"
ControlToValidate = "inputMailTo"
ValidationExpression = "^ [\ w \.-] + @ [\ w \.-] + \. [a-zA-Z] + $"
Display = "Static"
EnableClientScript = "false"
ErrorMessage = "recipient email address error"
Runat = "server"/>
</Td>
</Tr>
<Tr>
<Td width = "20%" height = "24"> sender address: </td>
& Lt; td width = "80%" height = "24" & gt;
<Input type = "text" id = "inputMailFrom" name = "inputMailFrom" runat = "server" size = "48">
<Asp: RequiredFieldValidator
Id = "RequiredFieldValidatorInputMailFrom"
ControlToValidate = "inputMailFrom"
Display = "Static"
EnableClientScript = "False"
ErrorMessage = "sender cannot be blank"
Runat = "server"/>
<Asp: RegularExpressionValidator id = "RegularExpressionValidatorInputMailFrom"
ControlToValidate = "inputMailFrom"
ValidationExpression = "^ [\ w \.-] + @ [\ w \.-] + \. [a-zA-Z] + $"
Display = "Static"
EnableClientScript = "false"
ErrorMessage = "the sender's email address is incorrect"
Runat = "server"/>
</Td>
</Tr>
<Tr>
<Td width = "20%" height = "24"> subject: </td>
& Lt; td width = "80%" height = "24" & gt;
<Input type = "text" id = "inputMailSubject" name = "inputMailSubject" runat = "server" size = "48">
<Asp: RequiredFieldValidator
Id = "RequiredFieldValidatorInputMailSubject"
ControlToValidate = "inputMailSubject"
Display = "Static"
EnableClientScript = "False"
ErrorMessage = "the email subject cannot be blank"
Runat = "server"/>
</Td>
</Tr>
<Tr>
<Td width = "20%" height = "24"> email content:
</Td>
& Lt; td width = "80%" height = "24" & gt;
<Asp: TextBox id = "textBoxMailBody" name = "textBoxMailBody" runat = "server" Rows = "6" Cols = "48" TextMode = "MultiLine"/>
<Asp: RequiredFieldValidator
Id = "RequiredFieldValidatorTextBoxMailBody"
ControlToValidate = "textBoxMailBody"
Display = "Static"
EnableClientScript = "False"
ErrorMessage = "the email content cannot be blank"
Runat = "server"/>
</Td>
</Tr>
<Tr>
<Td width = "20%" height = "24"> email attachment:
</Td>
& Lt; td width = "80%" height = "24" & gt;
<Input type = "file" id = "inputMailAttachment" name = "inputMailAttachment" runat = "server" size = "48">
</Td>
</Tr>
<Tr>
<Td colspan = "2" align = "center">
<Input type = "button" value = "Send Mail" OnServerClick = "SendMail" id = "buttonSendMail" name = "buttonSendMail" runat = "server">
</Td>
</Tr>
<Tr>
<Td colspan = "2" align = "center" height = "24">
</Td>
</Tr>
<Tr>
<Td colspan = "2" align = "center">
<Asp: Label id = "labelSendMailResult" runat = "server" text = "" font-bold = "True" forecolor = "# FF0000"/>
</Td>
</Tr>
</Form>
</Table>
</Div>
</Body>
</Html>