站點首頁   聯系我們 
站點首頁 數據中心 域名註冊 網頁寄存 企業郵局 智能建站  
返回站點首頁
支持中心首頁
常見問題搜索
常見問題
域名相關問題
空間相關問題
郵箱相關問題
智能建站相關問題
繁簡通相關問題
動態域名相關問題
中文域名相關問題
代理申請相關問題
網絡查詢工具
 

   

ASP.net
發郵件的例程(Language:"C#"; 帶EMail地址驗證)

<%@ 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();

// 設置email的'from'和'to'的地址
mailObj.From = inputMailFrom.Value;
mailObj.To = inputMailTo.Value;

mailObj.Subject = inputMailSubject.Value;
mailObj.Body = textBoxMailBody.Text;

// 可選: 使用html格式的Email
mailObj.BodyFormat = MailFormat.Html;

// 可選: 對郵件進行加密
// mailObj.BodyEncoding = MailFormat.Base64;

// 可選: 設置郵件的優先級別為高
mailObj.Priority = MailPriority.High;

// 可選: 附件
if (inputMailAttachment.PostedFile.ContentLength > 0) {
// 註意這裏我們創建了一個MailAttachment對象來附加一個文件到email。
mailObj.Attachments.Add(new MailAttachment(inputMailAttachment.PostedFile.FileName));
}

// 使用SmtpMail對象來發送郵件。
SmtpMail.Send(mailObj);
labelSendMailResult.Text = "郵件發送成功 From: " + inputMailFrom.Value + "; To: " + inputMailTo.Value;
if (inputMailAttachment.PostedFile.ContentLength > 0) {
labelSendMailResult.Text += "<br>該郵件包含附件: " + inputMailAttachment.PostedFile.FileName + ", 附件大小為: " + (inputMailAttachment.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
}
}
}
</script>


<html>
<head>
<title>
發送郵件 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">收件人地址: </td>
<td width="80%" height="24">
<input type="text" id="inputMailTo" name="inputMailTo" runat="server" size="48">
<asp:RequiredFieldValidator
id="RequiredFieldValidatorInputMailTo"
ControlToValidate="inputMailTo"
Display="Static"
EnableClientScript="False"
ErrorMessage="收件人不能為空"
runat="server"/>
<asp:RegularExpressionValidator id="RegularExpressionValidatorInputMailTo"
ControlToValidate="inputMailTo"
ValidationExpression="^[\w\.-]+@[\w\.-]+\.[a-zA-Z]+$"
Display="Static"
EnableClientScript="false"
ErrorMessage="收件人郵件地址錯誤"
runat="server"/>
</td>
</tr>
<tr>
<td width="20%" height="24">發件人地址: </td>
<td width="80%" height="24">
<input type="text" id="inputMailFrom" name="inputMailFrom" runat="server" size="48">
<asp:RequiredFieldValidator
id="RequiredFieldValidatorInputMailFrom"
ControlToValidate="inputMailFrom"
Display="Static"
EnableClientScript="False"
ErrorMessage="發件人不能為空"
runat="server"/>
<asp:RegularExpressionValidator id="RegularExpressionValidatorInputMailFrom"
ControlToValidate="inputMailFrom"
ValidationExpression="^[\w\.-]+@[\w\.-]+\.[a-zA-Z]+$"
Display="Static"
EnableClientScript="false"
ErrorMessage="發件人郵件地址錯誤"
runat="server"/>
</td>
</tr>
<tr>
<td width="20%" height="24">郵件主題: </td>
<td width="80%" height="24">
<input type="text" id="inputMailSubject" name="inputMailSubject" runat="server" size="48">
<asp:RequiredFieldValidator
id="RequiredFieldValidatorInputMailSubject"
ControlToValidate="inputMailSubject"
Display="Static"
EnableClientScript="False"
ErrorMessage="郵件主題不能為空"
runat="server"/>
</td>
</tr>
<tr>
<td width="20%" height="24">郵件內容:
</td>
<td width="80%" height="24">
<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="郵件內容不能為空"
runat="server"/>
</td>
</tr>
<tr>
<td width="20%" height="24">郵件附件:
</td>
<td width="80%" height="24">
<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>

返回

  打 印   發 送 時代互聯Eranet International Limited 版權所有 ©2005-2024
《中華人民共和國增值電信業務經營許可證》 ISP證粵B2-20042046