日期:2014-05-18  浏览次数:20903 次

C#发送邮件为何出现下列错误????
C# code
       protected void SendMail()
        {
            try
            {
                //create the mail message
                MailMessage myMail = new MailMessage();
                //set the addresses
                myMail.From = new MailAddress("service@gn.com.cn");
                myMail.To.Add( "593234579@qq.com");
                //set the content
                myMail.Subject = "有短信未发送" + DateTime.Now.ToLongTimeString();
                myMail.Body = "有" + MyClass.Dlookup("select count(*) from sms where dirty = 1 and datediff(day,updatetime,getdate())>1") + "条短信未发出";
                //send the message
                SmtpClient smtp = new SmtpClient("mail.gn.com.cn");
                smtp.Credentials = new NetworkCredential("service@gn.com.cn", "51012345");
                smtp.Send(myMail);

            }
            catch (Exception e)
            {
                lbMessage.Text = e.Message;
            }
        }




为何出现了以上错误???


------解决方案--------------------
应当是你的邮件服务器配置问题
直接 给你一个类吧
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;

    public class SendMail
    {
        string emailname = ConfigurationManager.AppSettings["emailname"].ToString();
        string emailpwd = ConfigurationManager.AppSettings["emailpwd"].ToString();
        string host = ConfigurationManager.AppSettings["emailhost"].ToString();

        public void SendMailTo(string from, string body)
        {
            try
            {
                //mail服务器设置
                SmtpClient mailserver = new SmtpClient();
                mailserver.Host = host;
                mailserver.Credentials = new System.Net.NetworkCredential(emailname, emailpwd);
                mailserver.Timeout = 5000;
                mailserver.EnableSsl = true;
                //mail邮件设置
                MailMessage mail = new MailMessage();
                MailAddress mfrom = new MailAddress(emailname);
                mail.From = mfrom;
                mail.To.Add(from);
                mail.Subject = "";
                mail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
                mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                mail.IsBodyHtml = true;
                mail.Body = "";
                mailserver.Send(mail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }