代码:public static bool SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
    {
        //发送
        try
        {
        //生成一个 使用SMTP发送邮件的客户端对象
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);        //表示以当前登录用户的默认凭据进行身份验证
        client.UseDefaultCredentials = true;        //包含用户名和密码
        client.Credentials = new System.Net.NetworkCredential(UserName, Password);        //指定如何发送电子邮件。
        //Network                      电子邮件通过网络发送到 SMTP 服务器。  
        //PickupDirectoryFromIis       将电子邮件复制到挑选目录,然后通过本地 Internet 信息服务 (IIS) 传送。  
        //SpecifiedPickupDirectory     将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。          client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis;        //建立邮件对象 
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);        //定义邮件正文,主题的编码方式
        message.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        message.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8");        //获取或设置一个值,该值指示电子邮件正文是否为 HTML。 
        message.IsBodyHtml = true;        //指定邮件优先级        message.Priority = System.Net.Mail.MailPriority.Normal;        //添加附件
        //System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@"f:/baihe.txt"); 
        if (strFileName != "" && strFileName != null)
        {
            Attachment data = new Attachment(strFileName);
            message.Attachments.Add(data);
        }
        //发件人身份验证,否则163 发不了
        client.Credentials = new System.Net.NetworkCredential(strFrom, Password);        
            client.Send(message);
        }
        catch (SmtpException ex)
        {
            throw ex;
        }
        return true;
    }
本地测试正常,能收到邮件,
但是放到服务器上就收不到了。而且不报错谁知道为什么?指点我一下。谢谢了

解决方案 »

  1.   

    是否开启SMTP。邮箱地址是否支持,
    用jmail看看
      

  2.   

    windows 的 mail服务 安装了么
      

  3.   

    windows 的 mail服务开启了么
      

  4.   

    没有开启SMTP,未安装mail服务,程序不会报错吗?
      

  5.   

    看msdn的例子using System.Net.Mail; 
    using System.Net.Mime; 
    using System.Net; public static void CreateMessageWithAttachment(string server) 
        { 
            // Specify the file to be attached and sent. 
            // This example assumes that a file named Data.xls exists in the 
            // current working directory. 
            string file = @"D:\asdf.txt"; 
            // Create a message and set up the recipients. 
            MailMessage message = new MailMessage( 
              "[email protected]", 
              "[email protected]", 
              "test", 
              "no du");         // Create  the file attachment for this e-mail message. 
            Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); 
            // Add time stamp information for the file. 
            ContentDisposition disposition = data.ContentDisposition; 
            disposition.CreationDate = System.IO.File.GetCreationTime(file); 
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); 
            disposition.ReadDate = System.IO.File.GetLastAccessTime(file); 
            // Add the file attachment to this e-mail message. 
            message.Attachments.Add(data); 
            //Send the message. 
            SmtpClient client = new SmtpClient(server); 
            // Add credentials if the SMTP server requires them. 
            //client.Credentials = CredentialCache.DefaultNetworkCredentials; 
            client.UseDefaultCredentials = true; 
            client.Credentials = new System.Net.NetworkCredential("username", "password"); 
            client.Send(message); 
            // Display the values in the ContentDisposition for the attachment. 
            ContentDisposition cd = data.ContentDisposition; 
            Console.WriteLine("Content disposition"); 
            Console.WriteLine(cd.ToString()); 
            Console.WriteLine("File {0}", cd.FileName); 
            Console.WriteLine("Size {0}", cd.Size); 
            Console.WriteLine("Creation {0}", cd.CreationDate); 
            Console.WriteLine("Modification {0}", cd.ModificationDate); 
            Console.WriteLine("Read {0}", cd.ReadDate); 
            Console.WriteLine("Inline {0}", cd.Inline); 
            Console.WriteLine("Parameters: {0}", cd.Parameters.Count); 
            foreach (DictionaryEntry d in cd.Parameters) 
            { 
                Console.WriteLine("{0} = {1}", d.Key, d.Value); 
            } 
            data.Dispose(); 
        }     protected void Timer1_Tick(object sender, EventArgs e) 
        { 
            if (DateTime.Now.Second % 20 == 0) 
            { 
                CreateMessageWithAttachment("smtp.163.com"); 
            } 
        }