ASP.net傳送郵件的例項

iDotNetSpace發表於2008-03-21

 

 

ASP.net傳送郵件的例項:點選下載

ASP.net傳送郵件的例項







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 += "
該郵件包含附件: " + inputMailAttachment.PostedFile.FileName + ", 附件大小為: " + (inputMailAttachment.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
}
}
}





<br>
傳送郵件 ASP.NET



































收件人地址:

id="RequiredFieldValidatorInputMailTo"
ControlToValidate="inputMailTo"
Display="Static"
EnableClientScript="False"
ErrorMessage="收件人不能為空"
runat="server"/>
ControlToValidate="inputMailTo"
ValidationExpression="^[\w\.-]+@[\w\.-]+\.[a-zA-Z]+$"
Display="Static"
EnableClientScript="false"
ErrorMessage="
收件人郵件地址錯誤"
runat="server"/>
發件人地址:

id="RequiredFieldValidatorInputMailFrom"
ControlToValidate="inputMailFrom"
Display="Static"
EnableClientScript="False"
ErrorMessage="發件人不能為空"
runat="server"/>
ControlToValidate="inputMailFrom"
ValidationExpression="^[\w\.-]+@[\w\.-]+\.[a-zA-Z]+$"
Display="Static"
EnableClientScript="false"
ErrorMessage="
發件人郵件地址錯誤"
runat="server"/>
郵件主題:

id="RequiredFieldValidatorInputMailSubject"
ControlToValidate="inputMailSubject"
Display="Static"
EnableClientScript="False"
ErrorMessage="郵件主題不能為空"
runat="server"/>
郵件內容:


id="RequiredFieldValidatorTextBoxMailBody"
ControlToValidate="textBoxMailBody"
Display="Static"
EnableClientScript="False"
ErrorMessage="郵件內容不能為空"
runat="server"/>
郵件附件:










 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-214922/,如需轉載,請註明出處,否則將追究法律責任。

相關文章