您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

分享一个asp.net发送邮件实例

2025/7/1 15:24:56发布22次查看
这篇文章主要介绍了asp.net发送邮件示例分享,需要的朋友可以参考下
mailhelper  -------mail帮助类
代码如下:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mail; /// <summary> ///mailhelper 的摘要说明 /// </summary> public class mailhelper { public mailhelper() { // //todo: 在此处添加构造函数逻辑 // } /// <summary> /// 邮件发送操作 /// </summary> /// <param name="addressee">收件人地址</param> /// <param name="from">发件人地址</param> /// <param name="sendpassword">发件人密码</param> /// <param name="copy">抄送人地址</param> /// <param name="secret">密送人地址</param> /// <param name="subject">发送主题</param> /// <param name="attachment">附件信息</param> /// <param name="body">邮件内容</param> public string sendeemal(string addressee, string from, string sendpassword, string copy, string secret, string subject, string attachment, string body) { mailmessage objmailmessage; mailattachment objmailattachment; // 创建邮件消息 objmailmessage = new mailmessage(); //发件人email objmailmessage.from = from;//源邮件地址 //收件人email objmailmessage.to = addressee; //目的邮件地址 //邮件抄送 objmailmessage.cc = copy; //邮件misong objmailmessage.bcc = secret; //邮件主题 objmailmessage.subject = subject; //发送邮件的标题 //邮件内容 objmailmessage.body = body;//发送邮件的内容 // 创建一个附件对象 if (attachment != "") { objmailattachment = new mailattachment(attachment);//发送邮件的附件 c:\\test.txt objmailmessage.attachments.add(objmailattachment);//将附件附加到邮件消息对象中 } //接着利用smtp来发送邮件,需要使用microsoft .net framework sdk v1.1和它以上的版本 //基本权限 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); //如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : client host rejected: access denied //smtp地址 string smtp = "smtp." + from.substring(from.indexof('@') + 1); smtpmail.smtpserver = "smtp." + from.substring(from.indexof('@') + 1); //开始发送邮件 try { smtpmail.send(objmailmessage); return "邮件发送成功!"; } catch (system.net.mail.smtpexception ex) { return ex.message; } //核心代码结束 } }
然后下来是自己做的一个demo--
前台
代码如下:
<%@ 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> 发给:<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 /> 内容:<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="发送" 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>
后台:
代码如下:
protected void button1_click(object sender, eventargs e) { //实例邮件帮助类 mailhelper mails = new mailhelper(); string filepath = hiddenfield1.value; string a = mails.sendeemal(textbox1.text, "邮件账号", "邮件密码", textbox2.text, textbox4.text, textbox5.text, filepath, textbox3.text); label1.text = a; }
以上就是分享一个asp.net发送邮件实例的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product