日期:2014-05-19  浏览次数:20797 次

100分求购 asp.net2.0 发送email的源码,在线等
要求是用C#写的,谢谢,只有一个APSX文件的最好.为了学习,

------解决方案--------------------
需要引用System.Web.Mail命名空间。引用该命名空间需要添加System.Web.Dll

MailMessage mail = new MailMessage();
mail.BodyFormat=MailFormat.Html;
mail.From = "some@server ";
mail.To= "some@server ";//多个收件人之间用分号分割
mail.Bcc= "some@server ";//密送
mail.Cc== "some@server ";//抄送
mail.Subject = "this is mail subject ";
mail.Body = "this is mail body ";
mail.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate ", "1 ");
mail.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername ", "username ");
mail.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword ", "password);

System.Web.Mail.MailAttachment attachment=new MailAttachment( "c:\\1.txt ");
mail.Attachments.Add(attachment);
SmtpMail.SmtpServer = "10.2.140.100 ";
SmtpMail.Send(mail);
------解决方案--------------------
protected void btnOk_Click(object sender, EventArgs e)
{
MailMessage mMessage = null;
SmtpClient smtpclient = null;
try
{
string sFrom = "myEmail@163.com ", sPassword = "aaa ", sServer = "smtp.163.com ";
int nPort = 25;


string sResplatURL = Request.ApplicationPath+ "/msg.aspx?Resid= " + Request[ "Resid "];

string body = txtBody.Text + " <br /> 点击下面连接查看资源信息: <br /> <a href= ' " + sResplatURL + " ' target= '_blank '> " + lblSubject.Text + " </a> ";
mMessage = new MailMessage(sFrom, txtTo.Text.Trim(), txtSubject.Text, body);
mMessage.IsBodyHtml = true;
mMessage.BodyEncoding = System.Text.Encoding.Default;
mMessage.SubjectEncoding = System.Text.Encoding.Default;
mMessage.ReplyTo = new MailAddress(sFrom);

smtpclient = new SmtpClient(sServer, nPort);
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
string sUsername = sFrom.Remove(sFrom.IndexOf( '@ '));
smtpclient.Credentials = new NetworkCredential(sUsername, sPassword);

smtpclient.Send(mMessage);
ClientScript.RegisterStartupScript(Type.GetType( "System.String "), " ", " <script> alert( '发送成功! ');window.close(); </script> ");

}
catch (SmtpFailedRecipientsException ex)
{
for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable)
{
//Console.WriteLine( "Delivery failed - retrying in 5 seconds. ");
System.Threading.Thread.Sleep(5000);
smtpclient.Send(mMessage);
}
else
{
LogInfo.Log( "Failed to deliver message to " + ex.FailedRecipient[i]);
}
}
}
catch (Exception ex)
{
LogInfo.Log(ex);