asp.net 163邮件发送

  <table id="TABLE1" runat="server" border="0" cellpadding="0" cellspacing="0"> 
        <tr> 
            <td style="width: 393px"> 
                收信:<asp:TextBox ID="TextBox1" runat="server">kally32@163.com</asp:TextBox><br /> 
                主题:<asp:TextBox ID="TextBox2" runat="server">测试主题</asp:TextBox><br /> 
                内容:<asp:TextBox ID="TextBox3" runat="server" Height="154px" TextMode="MultiLine" 
                Width="336px">测试内容</asp:TextBox><br /> 
                <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text=" 发  送 " />
            </td> 
        </tr> 
    </table> 

    </div> 
    <table id="Table2" runat="server" border="0" cellpadding="0" cellspacing="0" visible="false"> 
        <tr> 
            <td align="center" style="width: 400px"> 
                <asp:Label ID="Label1" runat="server" ForeColor="Red" Text="恭喜您,发表成功!"></asp:Label><br /> 
                <asp:Button ID="Button2" runat="server" Text="返回" onclick="Button2_Click" />
            </td> 
        </tr> 
    </table> 

以上是前台代码 

以下是后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//倒入命名空间 
using System.Net;
using System.Net.Mail;


namespace WebTestMail
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //返回,继续发送 
            Response.Redirect(Request.Url.ToString());
            TABLE1.Visible = true;
            Table2.Visible = false;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //常用的邮箱服务器(SMTP、POP3)地址、端口
            //http://blog.sina.com.cn/s/blog_6e85b10501012kyv.html
            //smtp.163.com
            bool reVal = SendMail("smtp.163.com", 25, "", "", "发送邮箱地址", TextBox2.Text, TextBox3.Text, "");
            if (reVal) { 

                  Label1.Text = "恭喜你!邮件发送成功。";
            }
            else 
            {
                Label1.Text = "邮件发送失败,检查网络及信箱是否可用。";
            }


            TABLE1.Visible = false;
            Table2.Visible = true;
        }

        //参数说明
        /*
         * strSmtpServer:指定发送邮件服务器 
         * iSmtpPort:发送邮件服务器端口
         * Password:发送邮件地址的密码
         * strFrom:发送邮件地址
         * strto:收件地址
         * strSubject:邮件标题 
         * strBody:邮件内容
         */
        public bool SendMail(string strSmtpServer, int iSmtpPort, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
        {

            //设置发件人信箱,及显示名字 
            MailAddress mailFrom = new MailAddress(strFrom);

            //设置收件人信箱,及显示名字 
            MailAddress mailTo = new MailAddress(strto);

            //创建一个MailMessage对象 
            MailMessage oMail = new MailMessage(mailFrom, mailTo);

            try
            {

                oMail.Subject = TextBox2.Text; //邮件标题 
                oMail.Body = TextBox3.Text; //邮件内容 

                oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式 
                oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码 
                oMail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码 
                oMail.Priority = MailPriority.High;//设置邮件的优先级为高 

                //添加附件
                //System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@"f:/baihe.txt"); 
                if (strFileName != "" && strFileName != null)
                {
                    Attachment data = new Attachment(strFileName);
                    oMail.Attachments.Add(data);
                }

                //发送邮件服务器 
                SmtpClient client = new SmtpClient();

                //发送邮件服务器的smtp
                //每种邮箱都不一致
                client.Host = strSmtpServer; //指定邮件服务器 

                //发送邮件服务器端口
                client.Port = iSmtpPort;

                //设置超时时间 
                client.Timeout = 9999;

                //设置为发送认证消息
                client.UseDefaultCredentials = true;

                //指定服务器邮件,及密码 
                //发邮件人的邮箱地址和密码
                client.Credentials = new NetworkCredential(strFrom, Password);

                client.Send(oMail); //发送邮件 

                //释放资源
                mailFrom = null;

                mailTo = null;

                client.Dispose();//释放资源

                oMail.Dispose(); //释放资源 

                return true;
            }
            catch (Exception ex)
            {
                //释放资源
                mailFrom = null;

                mailTo = null;

                oMail.Dispose(); //释放资源 

                return false;
            }
        }


    }
}

 

 

posted @ 2013-07-03 11:01  beau  阅读(2295)  评论(0编辑  收藏  举报