asp.net 2.0发送和接收邮件总结

总结:.用asp.net 2.0发送邮件非常的方便,只需要用using System.Net.Mail命名空间下的类就可以完成发送邮件的功能,发送邮件的服务器可以有以下几种情况:
1.本地的smtp服务器
2.网络上的smtp服务器(smtp.163.com等)
以下是我测试发送邮件的范例,基本上都考虑到的所有的情况,如果还有什么没有考虑到的请给我留言,我接续完善其功能.
1.新建一网站名字叫SentEmailText.
2.添加一个新的页面default.aspx
代码如下:
< form id ="Form1" runat ="server" method ="post" >
< div id ="content" >
< div id ="msg" >
< div id ="msg_title" > SendEmailTest </ div >
< div id ="msg_prompt" ></ div >
< asp:ValidationSummary id ="msg_alarm" runat ="server" ></ asp:ValidationSummary >
</ div >
< div id ="form" >
< div >
< label for ="SmtpServerText" > Severname: </ label >
< input type ="text" id ="SmtpServerText" name ="SmtpServerText" runat ="server" />
</ div >
< div >
< label for ="SmtpServerPort" > SeverPort: </ label >
< input type ="text" id ="SmtpServerPort" name ="SmtpServerPort" runat ="server" />
</ div >
< div >
< label for ="UserName" > UserName: </ label >
< input type ="text" name ="UserName" id ="UserName" runat ="server" />
</ div >
< div >
< label for ="Pwd" > Pwd: </ label >
< input type ="text" name ="Pwd" runat ="server" id ="Pwd" />
</ div >
< div >
< label for ="FromAddress" > FromAddress: </ label >
< input type ="text" name ="FromAddress" runat ="server" id ="FromAddress" />
</ div >
< div >
< label for ="SendAddress" > SendAddress: </ label >
< input type ="text" name ="SendAddress" runat ="server" id ="SendAddress" />
</ div >
< div >
< label for ="attachment" > Attachment: </ label >
< input type ="file" runat ="server" name ="attachment" id ="attachment" />
</ div >
< div >
< label for ="txtSubject" > EmailSubject: </ label >
< input type ="text" name ="txtSubject" runat ="server" id ="txtSubject" />
</ div >
< div >
< label for ="txtBody" > EmailBody: </ label >
< textarea name ="txtBody" runat ="server" rows ="10" cols ="50" id ="txtBody" ></ textarea >
</ div >
< div >
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
< input type ="submit" value ="SendEmail" class ="prim" id ="btnSend" runat ="server" onserverclick ="btnSend_ServerClick" />
</ div >
</ div >
</ div >
</ form >
3.新建一个style的文件夹,添加样式页面,主要功能是表单的对齐样式,比较通用的设置(见附件)
4.添加一个mail的类文件,注意用于发送邮件考虑的几种情况(见附件)
using System;
using System.Text;
using System.Net.Mail;
using System.Net.Sockets;
using System.IO;
public class Mail
{
"Customvar"#region"Customvar"
privatestring_User;
privatestring_Pwd;
privatestring_Host;
privateint_Port;
#endregion


"Property"#region"Property"
publicintPort
{
get
{
return_Port;
}

set
{
_Port
=value;
}

}

publicstringUser
{
get
{
return_User;
}

set
{
_User
=value;
}

}

publicstringPwd
{
get
{
return_Pwd;
}

set
{
_Pwd
=value;
}

}

publicstringHost
{
get
{
return_Host;
}

set
{
_Host
=value;
}

}

#endregion


"OverLoad"#region"OverLoad"
publicMail(stringuser,stringpwd,stringhost,intport)
{//othersmtpserverincludingusernameandpwd
this._User=user;
this._Pwd=pwd;
this._Host=host;
this._Port=port;
}

publicMail(stringhost,intport)
{//othersmtpserverbutusernameandpwd
this._Port=port;
this._Host=host;
}

publicMail()
{//localsmtpserverandport=25
this._Port=25;
this._Host="127.0.0.1";
}

#endregion


"SendEmialincludinggroupsandattachment"#region"SendEmialincludinggroupsandattachment"
/**////<summary>
///Sentemailsincludingattachment
///</summary>
///<paramname="toUser">FromAddress</param>
///<paramname="toMail">ToAddress</param>
///<paramname="subject">ThesubjectofEmail</param>
///<paramname="body">ThebodyofEmail</param>
///<paramname="ishtml">HtmlOrText</param>
///<paramname="priority">ThepriorityofEmail</param>
///<paramname="filenames">theattachmentofemail(optional)</param>

publicvoidSendMailWithAttachment(stringtoUser,stringtoMail,stringsubject,stringbody,boolishtml,MailPrioritypriority,paramsstring[]filenames)
{
System.Net.Mail.MailMessagemsg
=newSystem.Net.Mail.MailMessage(toUser,toMail,subject,body);
msg.BodyEncoding
=Encoding.UTF8;
msg.Priority
=priority;
msg.IsBodyHtml
=ishtml;
if(filenames!=null)
{
foreach(stringsinfilenames)
{
msg.Attachments.Add(
newAttachment(s));
}

}

System.Net.Mail.SmtpClientsc
=newSmtpClient(Host,Port);
sc.EnableSsl
=false;
sc.Timeout
=3600000;
sc.UseDefaultCredentials
=false;
sc.Credentials
=newSystem.Net.NetworkCredential(_User,_Pwd);
sc.DeliveryMethod
=SmtpDeliveryMethod.Network;
try
{
sc.Send(msg);
}

catch(Exceptione)
{
throwe;
}

}

/**////<summary>
///SentEmailincludinggroupsandattachment
///</summary>
///<paramname="toUser">FromAddress</param>
///<paramname="toMail">ToAddress</param>
///<paramname="subject">ThesubjectofEmail</param>
///<paramname="body">ThebodyofEmail</param>
///<paramname="ishtml">HtmlOrText</param>
///<paramname="priority">ThepriorityofEmail</param>
///<paramname="filenames">TheattachmentofEmail(optional)</param>

publicvoidSendMailWithGroupWithAttachment(stringtoUser,stringtoMail,stringsubject,stringbody,boolishtml,MailPrioritypriority,paramsstring[]filenames)
{
string[]toMails=toMail.Split(';');
System.Net.Mail.MailMessagemsg
=newSystem.Net.Mail.MailMessage();
msg.From
=newMailAddress(toUser);
foreach(stringsintoMails)
{
msg.To.Add(s);
}

msg.Subject
=subject;
msg.Body
=body;
msg.BodyEncoding
=Encoding.UTF8;
msg.Priority
=priority;
msg.IsBodyHtml
=ishtml;
if(filenames!=null)
{
foreach(stringsinfilenames)
msg.Attachments.Add(
newAttachment(s));
}

System.Net.Mail.SmtpClientsc
=newSmtpClient(Host,Port);
sc.EnableSsl
=false;
sc.Timeout
=3600000;
sc.UseDefaultCredentials
=false;
sc.Credentials
=newSystem.Net.NetworkCredential(_User,_Pwd);
sc.DeliveryMethod
=SmtpDeliveryMethod.Network;
try
{
sc.Send(msg);
}

catch(Exceptione)
{
throwe;
}

}

#endregion


/**////<summary>
///ReceivedEmail
///</summary>

publicvoidReseiveMail()
{
stringServerHost="pop3."+this._Host;
TcpClienttcp
=newTcpClient(ServerHost,110);
NetworkStreamns
=tcp.GetStream();
StreamReadersr
=newStreamReader(ns);
StreamWritersw
=newStreamWriter(ns);
}

}

5.页面调用方式参见附件
6.附件下载 附件(源代码下载)
付出最大努力,追求最高成就。
关于asp的邮件收发系统。 %@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="read_mail.aspx.cs" Inherits="read_mail" Title="读邮件" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <div> <table width="800" height="30" border="0" bgcolor="bfdbff"> <tr> <td width="800" align="left" valign="middle"> <asp:ImageButton ID="goback" runat="server" ImageUrl="image/1tuichu.jpg" /> <asp:Button ID="Button1" runat="server" Text="回复" /></td> </tr> </table> <table width="800" border="0" bgcolor="e0edff"> <tr> <td width="120" height="30" align="right">发信人:</td> <td width="666"> <asp:TextBox ID="TextBox1" runat="server" Height="24px" Width="180px"></asp:TextBox></td> </tr> <tr> <td width="120" height="30" align="right">邮件主题:</td> <td> <asp:TextBox ID="TextBox2" runat="server" Height="24px" Width="180px"></asp:TextBox></td> </tr> <tr> <td width="120" height="30" align="right">邮件内容:</td> <td> </td> </tr> </table> <table width="800" height="300" border="0" bgcolor="e0edff"> <tr> <td align="left" valign="top">  <asp:TextBox ID="TextBox3" runat="server" Height="280px" Width="600px"></asp:TextBox></td> </tr> </table> <table width="800" border="0" bgcolor="e0edff"> <tr> <td height="30">        下载附件:</td> </tr> <tr> <td height="150" align="left" valign="top"> <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="A_id" <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" /> <EditRowStyle BackColor="#2461BF" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField HeaderText="下载"> <ItemTemplate> <a target="_blank">'>Downloads</a> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="A_name" HeaderText="附件名称" /> </Columns> </asp:GridView> </td> </tr> </table> <table width="800" border="0" bgcolor="#e0edff"> <tr> <td height="30" colspan="2" bgcolor="#bfdbff"> </td> </tr> </table> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </div> </asp:Content>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值