小編給大家分享一下asp.net發(fā)送郵件的示例,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)建站是一家做網(wǎng)站、成都網(wǎng)站建設(shè),提供網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,建網(wǎng)站,專(zhuān)業(yè)公司,網(wǎng)站開(kāi)發(fā)公司,于2013年創(chuàng)立是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價(jià)值為核心業(yè)務(wù),全程參與項(xiàng)目的網(wǎng)站策劃設(shè)計(jì)制作,前端開(kāi)發(fā),后臺(tái)程序制作以及后期項(xiàng)目運(yùn)營(yíng)并提出專(zhuān)業(yè)建議和思路。
mailhelper -------mail幫助類(lèi)
代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mail; /// <summary> ///mailhelper 的摘要說(shuō)明 /// </summary> public class mailhelper { public mailhelper() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } /// <summary> /// 郵件發(fā)送操作 /// </summary> /// <param name="Addressee">收件人地址</param> /// <param name="From">發(fā)件人地址</param> /// <param name="sendpassword">發(fā)件人密碼</param> /// <param name="Copy">抄送人地址</param> /// <param name="secret">密送人地址</param> /// <param name="Subject">發(fā)送主題</param> /// <param name="Attachment">附件信息</param> /// <param name="Body">郵件內(nèi)容</param> public string SendeEmal(string Addressee, string From, string sendpassword, string Copy, string secret, string Subject, string Attachment, string Body) { MailMessage objMailMessage; MailAttachment objMailAttachment; // 創(chuàng)建郵件消息 objMailMessage = new MailMessage(); //發(fā)件人EMAIL objMailMessage.From = From;//源郵件地址 //收件人EMAIL objMailMessage.To = Addressee; //目的郵件地址 //郵件抄送 objMailMessage.Cc = Copy; //郵件misong objMailMessage.Bcc = secret; //郵件主題 objMailMessage.Subject = Subject; //發(fā)送郵件的標(biāo)題 //郵件內(nèi)容 objMailMessage.Body = Body;//發(fā)送郵件的內(nèi)容 // 創(chuàng)建一個(gè)附件對(duì)象 if (Attachment != "") { objMailAttachment = new MailAttachment(Attachment);//發(fā)送郵件的附件 c:\\test.txt objMailMessage.Attachments.Add(objMailAttachment);//將附件附加到郵件消息對(duì)象中 } //接著利用SMTP來(lái)發(fā)送郵件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本 //基本權(quán)限 objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //用戶名 string name = From.Substring(0, From.IndexOf('@')); objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", name); //密碼 objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sendpassword); //如果沒(méi)有上述三行代碼,則出現(xiàn)如下錯(cuò)誤提示:服務(wù)器拒絕了一個(gè)或多個(gè)收件人地址。服務(wù)器響應(yīng)為: 554 : Client host rejected: Access denied //SMTP地址 string smtp = "smtp." + From.Substring(From.IndexOf('@') + 1); SmtpMail.SmtpServer = "smtp." + From.Substring(From.IndexOf('@') + 1); //開(kāi)始發(fā)送郵件 try { SmtpMail.Send(objMailMessage); return "郵件發(fā)送成功!"; } catch (System.Net.Mail.SmtpException ex) { return ex.Message; } //核心代碼結(jié)束 } }
然后下來(lái)是自己做的一個(gè)demo--
前臺(tái)
代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mail.aspx.cs" Inherits="information_mail" ValidateRequest="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="../Style/jquery/jquery.js" type="text/javascript"></script> <script src="../Style/jquery/jquery.validate.js" type="text/javascript"></script> <script language="javascript" type="text/javascript"> function gei() { var file_value = document.getElementById("File1").value; document.getElementById("HiddenField1").value = file_value; } </script> </head> <body> <form id="form1" runat="server"> <div> 發(fā)給:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> 抄送:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> 密送:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /> 主題:<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br /> 內(nèi)容:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /> 附件:<input id="File1" type="file" /> <%--<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>--%> <br /> <asp:Button ID="Button1" runat="server" Text="發(fā)送" OnClientClick="gei()" OnClick="Button1_Click" /><br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </div> <asp:HiddenField ID="HiddenField1" runat="server" /> </form> </body> </html>
后臺(tái):
代碼如下:
protected void Button1_Click(object sender, EventArgs e) { //實(shí)例郵件幫助類(lèi) mailhelper mails = new mailhelper(); string filePath = HiddenField1.Value; string a = mails.SendeEmal(TextBox1.Text, "郵件賬號(hào)", "郵件密碼", TextBox2.Text, TextBox4.Text, TextBox5.Text, filePath, TextBox3.Text); Label1.Text = a; }
看完了這篇文章,相信你對(duì)asp.net發(fā)送郵件的示例有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站欄目:asp.net發(fā)送郵件的示例
網(wǎng)站地址:http://aaarwkj.com/article0/jpddoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站維護(hù)、動(dòng)態(tài)網(wǎng)站、云服務(wù)器、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)