利用SmtpClient,smtp为服务器发送邮件.
当我把服务器名称,发送邮件的用户名和密码写到配置文件里.
通过配置文件读取的时候会出现超时的问题.当我直接写到方法里面的时候,就不会超时.为什么呢 ?
/// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="server">服务器stmp.163.com</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="toaddr">发送地址</param>
        /// <param name="titel">标题</param>
        /// <param name="body">内容</param>
        public static void sendmail(string server, string username, string password, string toaddr, string titel, string body)//,string path)
        {
            try
            {
                System.Net.Mail.SmtpClient client = new SmtpClient();
                client.Host = server;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(username, password);
                //星号改成自己邮箱的密码
                client.DeliveryMethod = SmtpDeliveryMethod.Network;                System.Net.Mail.MailMessage message = new MailMessage(username, toaddr, titel, body);
                message.BodyEncoding = System.Text.Encoding.Default;
                message.IsBodyHtml = true;                //添加附件
                //if (path != "" || path != null)
                //{
                //   Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
                //    message.Attachments.Add(data);
                //}
                client.Send(message);
            }
            catch
            {            }
        }

解决方案 »

  1.   

    断点判断获取的值
    public void MailSend(string MailFrom,string MailTo,string MailPwd,string Mailtitle,string MailCon)   
    {   
    MailMessage MyMail = new MailMessage();   
    MyMail.From = new MailAddress("", "");   
    MyMail.To.Add(new MailAddress(""));   
    MyMail.Subject = Mailtitle;   
    MyMail.Body = MailCon;   
    MyMail.IsBodyHtml = true;   
    SmtpClient smtpclient = new SmtpClient();   
    smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;   
    smtpclient.Host = "";   
    smtpclient.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);   
    smtpclient.Send(MyMail);   
    }   
    jmail.Message Jmail = new jmail.Message();   
    DateTime t = DateTime.Now;   
    String Subject = "";   
    String body = "";   
    String FromEmail = "";   
    String ToEmail = "";   
    Jmail.Charset = "GB2312";   
    Jmail.ContentType = "text/html";   
    Jmail.AddRecipient(ToEmail, "", "");   
    Jmail.From = FromEmail;   
    Jmail.MailServerUserName = "";   
    Jmail.MailServerPassWord = "";   
    Jmail.Subject = Subject;   
    Jmail.ContentType="text/html";   
    Jmail.Body = body + t.ToString();   
    Jmail.Send("", false);   
    Jmail.Close();   message.Body ="";
    client.Send(message);
      

  2.   

    我断点调试了,所有的数据都获取到了的,当执行到   client.Send(message);的时候,就弹出网页,但是网页很慢,大概等了1-2分钟后,catch捕获异常,说操作超时.
      

  3.   


    可能是client.Host = server;设置的地址不对。。