发邮件

Asp.net 自动 发送邮件
注:System.Net.Mail命名空间下的测试已通过。若有时发送不成功,不是程序本身的错误,是邮件服务器的事。本人测试时邮件服务器是smtp.163.com的可以通过,smtp.126.com的不行。

 

/********************************** 未测试 **********************************/
    /// <summary>
    /// 用 using System.Web.Mail命名空间
    /// </summary>
    protected void WebMailSebd()
    {
        MailMessage mail = new MailMessage();

        //发件人地址
        mail.From =TBfrom1.Text;

        //收件人地址
        mail.To = TBto.Text;
       
        //邮件主题
        mail.Subject = TBsubject.Text;

        //邮件内容
        mail.Body = TBbody.Text;

        //优先级
        int priority = 0;
        switch (priority)
        {
            case 0:
                mail.Priority = MailPriority.High;
                break;
            case 1:
                mail.Priority = MailPriority.Low;
                break;
            default:
                mail.Priority = MailPriority.Normal;
                break;
        }

        //设置邮件格式
        //if (DDDlfromat.SelectedIndex == 0)
        //    mail.BodyFormat = MailFormat.Text;
        //else
            mail.BodyFormat = MailFormat.Html;

        //设置服务器


        //以下处理附件
            string strFileName = this.FileUpload1.PostedFile.FileName;
            if (strFileName != "")
            {
                MailAttachment attach = new MailAttachment(strFileName);
                mail.Attachments.Add(attach);
            }
        //发送邮件
        SmtpMail.SmtpServer = "smtp.163.com";
        SmtpMail.Send(mail);
        Response.Write("发送成功!");
        Response.End();

    }
/********************************** 未测试 **********************************/


/********************************** 已测试-正确 **********************************/
/// <summary>
    /// System.Net.Mail命名空间
   /// </summary>
   /// <returns></returns>
    public void NetMailSend()
    {
        
        //邮件发送时请确认服务的杀毒软件因素

        System.Net.Mail.SmtpClient client;
        client = new System.Net.Mail.SmtpClient("smtp.163.com");
        client.Timeout = 60000;
       
        //client.UseDefaultCredentials = false;
       
        client.UseDefaultCredentials = true;
        client.Credentials = new System.Net.NetworkCredential("发件人邮箱地址", "邮箱密码");
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        message.SubjectEncoding = System.Text.Encoding.UTF8;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.From = new System.Net.Mail.MailAddress("发件人邮箱地址", "发件人名字", System.Text.Encoding.UTF8);
        message.To.Add(new System.Net.Mail.MailAddress("收件人邮箱地址", "收件人名字", System.Text.Encoding.UTF8));
        message.IsBodyHtml = false;
        message.Subject = "邮件主题";
        message.Body =  "邮件内容";
        string strFileName = FileUpload1.PostedFile.FileName;
        if (strFileName != "")
        {
            Attachment data = new Attachment(strFileName);//附件
            message.Attachments.Add(data);
        }

        try
        {
            client.Send(message);
           
        }
        catch
         {
             Response.Write("发送失败");
        }
    }

/********************************** 已测试-正确 **********************************/

VS2005下的例子:

        /// <summary>
        /// 发送邮件可带附件,System.Net.Mail命名空间
        /// </summary>
        /// <param name="smtpName">邮件服务器地址</param>
        /// <param name="mailFrom">发件人邮箱地址</param>
        /// <param name="mailFromPwd">发件人邮箱密码</param>
        /// <param name="fromUserName">发件人名字</param>        
        /// <param name="mailTo">收件人邮箱地址</param>
        /// <param name="toUserName">收件人名字</param>
        /// <param name="mailSubject">邮件主题</param>
        /// <param name="mailBody">邮件内容</param>
        /// <param name="strFileName">附件名</param>
        /// <returns>成功返回true,否则返回false</returns>
        public bool NetMailSend
            (string smtpName, string mailFrom, string mailFromPwd, string fromUserName, string mailTo, string toUserName, string mailSubject, string mailBody, string strFileName)
        {
            bool isSucceed = false;//发送邮件是否成功

            //邮件发送时请确认服务的杀毒软件因素

            System.Net.Mail.SmtpClient client;
            client = new System.Net.Mail.SmtpClient(smtpName);
            client.Timeout = 60000;
            //client.UseDefaultCredentials = false;

            client.UseDefaultCredentials = true;
            client.Credentials = new System.Net.NetworkCredential(mailFrom, mailFromPwd);
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.From = new System.Net.Mail.MailAddress(mailFrom, fromUserName, System.Text.Encoding.UTF8);
            message.To.Add(new System.Net.Mail.MailAddress(mailTo, toUserName, System.Text.Encoding.UTF8));
            message.IsBodyHtml = true;
            message.Subject = mailSubject;//邮件主题
            message.Body = mailBody;//邮件内容
            message.Priority = MailPriority.High;

            //发送附件
            if (!string.IsNullOrEmpty(strFileName))
            {
                Attachment data = new Attachment(strFileName);//附件
                message.Attachments.Add(data);
            }

            try
            {
                client.Send(message);
                isSucceed = true;
            }
            catch
            {
                isSucceed = false;
            }

            return isSucceed;
        }

        /// <summary>
        /// 发送邮件可带附件,System.Net.Mail命名空间
        /// </summary>
        /// <param name="mailTo">收件人邮箱地址</param>
        /// <param name="toUserName">收件人名字</param>
        /// <param name="mailSubject">邮件主题</param>
        /// <param name="mailBody">邮件内容</param>
        /// <param name="strFileName">附件名</param>
        /// <returns>成功返回true,否则返回false</returns>
        public bool NetMailSend
            (string mailTo, string toUserName, string mailSubject, string mailBody, string strFileName)
        {
            string smtpName = "smtp.163.com";//邮件服务器地址
            string mailFrom = "mailto:ss@163.com%22;//发件人邮箱地址
            string mailFromPwd = "ss";//发件人邮箱密码
            string fromUserName = "\"赛默飞世尔科技\"";//发件人名字
            return NetMailSend(smtpName, mailFrom, mailFromPwd, fromUserName, mailTo, toUserName, mailSubject, mailBody, strFileName);
        }

 

 

posted @ 2010-04-13 22:45  洗碗心得  阅读(404)  评论(1编辑  收藏  举报