asp.net mail-sent routine web mail

Source: Internet
Author: User
asp.net|web| email asp.net (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 email ' from ' and ' to ' address
Mailobj.from = Inputmailfrom.value;
mailobj.to = Inputmailto.value;

Mailobj.subject = Inputmailsubject.value;
Mailobj.body = Textboxmailbody.text;

Optional: Email with HTML format
Mailobj.bodyformat = mailformat.html;

Optional: Encrypt messages
mailobj.bodyencoding = mailformat.base64;

Optional: Set the message to a high priority level
mailobj.priority = Mailpriority.high;

Optional: Accessories
if (InputMailAttachment.PostedFile.ContentLength > 0) {
Notice here we created a MailAttachment object to append a file to the email.
MAILOBJ.ATTACHMENTS.ADD (New MailAttachment (InputMailAttachment.PostedFile.FileName));
}

Use the SmtpMail object to send messages.
Smtpmail.send (Mailobj);
Labelsendmailresult.text = "Mail sent successfully from:" + Inputmailfrom.value + "; To: "+ inputmailto.value;
if (InputMailAttachment.PostedFile.ContentLength > 0) {
Labelsendmailresult.text + + <br> This message contains attachments: "+ inputMailAttachment.PostedFile.FileName +", Attachment size is: "+" (Inputmailatt Achment. postedfile.contentlength/1024). ToString () + "K Byte (s)";
}
}
}
</script>


<title>
Send mail asp.net</title>
<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>
&LT;TD width= "20%" height= "a" > Recipient address: </td>
&LT;TD width= "80%" height= ">"
<input type= "text" id= "Inputmailto" name= "Inputmailto" runat= "Server" size= ">"
<asp:requiredfieldvalidator
Id= "Requiredfieldvalidatorinputmailto"
Controltovalidate= "Inputmailto"
Display= "Static"
enableclientscript= "False"
Errormessage= "Recipient cannot be empty"
runat= "Server"/>
<asp:regularexpressionvalidator id= "Regularexpressionvalidatorinputmailto"
Controltovalidate= "Inputmailto"
Validationexpression= "^[\w\.-]+@[\w\.-]+\. [a-za-z]+$]
Display= "Static"
Enableclientscript= "false"
Errormessage= "Recipient mail address Error"
runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD width= "20%" height= "a" > Sender address: </td>
&LT;TD width= "80%" height= ">"
<input type= "text" id= "Inputmailfrom" name= "Inputmailfrom" runat= "Server" size= ">"
<asp:requiredfieldvalidator
Id= "Requiredfieldvalidatorinputmailfrom"
Controltovalidate= "Inputmailfrom"
Display= "Static"
enableclientscript= "False"
Errormessage= "Sender cannot be empty"
runat= "Server"/>
<asp:regularexpressionvalidator id= "Regularexpressionvalidatorinputmailfrom"
Controltovalidate= "Inputmailfrom"
Validationexpression= "^[\w\.-]+@[\w\.-]+\. [a-za-z]+$]
Display= "Static"
Enableclientscript= "false"
Errormessage= "Sender mail address error"
runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD width= "20%" height= "E" > Mail subject: </td>
&LT;TD width= "80%" height= ">"
<input type= "text" id= "Inputmailsubject" name= "Inputmailsubject" runat= "Server" size= ">"
<asp:requiredfieldvalidator
Id= "Requiredfieldvalidatorinputmailsubject"
Controltovalidate= "Inputmailsubject"
Display= "Static"
enableclientscript= "False"
Errormessage= "message subject cannot be empty"
runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD width= "20%" height= "E" > Email content:
</td>
&LT;TD width= "80%" height= ">"
<asp:textbox id= "Textboxmailbody" name= "Textboxmailbody" runat= "Server" rows= "6" cols= "a" textmode= "MultiLine"/ >
<asp:requiredfieldvalidator
Id= "Requiredfieldvalidatortextboxmailbody"
Controltovalidate= "Textboxmailbody"
Display= "Static"
enableclientscript= "False"
Errormessage= "message content cannot be empty"
runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD width= "20%" height= "E" > Email attachments:
</td>
&LT;TD width= "80%" height= ">"
<input type= "File" id= "Inputmailattachment" name= "inputmailattachment" runat= "Server" size= ">"
</td>
</tr>
<tr>
&LT;TD colspan= "2" align= "Center" >
<input type= "button" value= "Send Mail" onserverclick= "SendMail" id= "Buttonsendmail" name= "Buttonsendmail" runat= " Server >
</td>
</tr>
<tr>
&LT;TD colspan= "2" align= "center" height= ">"
</td>
</tr>
<tr>
&LT;TD colspan= "2" align= "Center" >
<asp:label id= "Labelsendmailresult" runat= "text=" "True" font-bold= "#FF0000" forecolor=
</td>
</tr>
</form>
</table>
</div>
</body>


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.