错误:找不到SMTP主机 [英] error : The SMTP host was not found

查看:208
本文介绍了错误:找不到SMTP主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码通过gmail从c#发送邮件,但出现了错误:
不是SMTP主机".
请帮帮我..

static bool mailSent = false;


 public void SendMail()
        {
            //Builed The MSG
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("gan@gmail.com");
            msg.To.Add("ganesan@gmail.com");
            msg.From = new MailAddress("ganesan@gmail.com");
            msg.Subject = "Test mail using .net2.0";
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = "This is my msg Body";
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = false;
            msg.Priority = MailPriority.High;            
           
            //Add the Creddentials
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential
                ("ganesan@gmail.com", "*******");
            client.Port = 587;//or use 587            
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);                
            object userState=msg;
            try
            {
                //you can also call client.Send(msg)
                client.SendAsync(msg, userState);   // i am gettint error in this line........             
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "Send Mail Error");
            }
        }

      void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
       {
           MailMessage mail = (MailMessage)e.UserState;
           string subject = mail.Subject;

           if (e.Cancelled)
           {
               string cancelled = string.Format("[{0}] Send canceled.", subject);
               MessageBox.Show(cancelled);
           }
           if (e.Error != null)
           {
               string error = String.Format("[{0}] {1}", subject, e.Error.ToString());
               MessageBox.Show(error);
           }
           else
           {
               MessageBox.Show("Message sent.");
           }
           mailSent = true;
       }

解决方案

提示在错误消息中.连接到SMTP服务器时出现问题,请检查您的连接设置


添加此行代码

client.UseDefaultCredentials = false;



在您的
之前

<pre lang="midl">client.Credentials = new System.Net.NetworkCredential
                ("ganesan@gmail.com", "*******");



看看这个技巧: ^ ]


I am using the below code for sending mail from c# using gmail but I got the error:
"The SMTP host was not".
Please help me..

static bool mailSent = false;


 public void SendMail()
        {
            //Builed The MSG
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("gan@gmail.com");
            msg.To.Add("ganesan@gmail.com");
            msg.From = new MailAddress("ganesan@gmail.com");
            msg.Subject = "Test mail using .net2.0";
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = "This is my msg Body";
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = false;
            msg.Priority = MailPriority.High;            
           
            //Add the Creddentials
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential
                ("ganesan@gmail.com", "*******");
            client.Port = 587;//or use 587            
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);                
            object userState=msg;
            try
            {
                //you can also call client.Send(msg)
                client.SendAsync(msg, userState);   // i am gettint error in this line........             
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "Send Mail Error");
            }
        }

      void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
       {
           MailMessage mail = (MailMessage)e.UserState;
           string subject = mail.Subject;

           if (e.Cancelled)
           {
               string cancelled = string.Format("[{0}] Send canceled.", subject);
               MessageBox.Show(cancelled);
           }
           if (e.Error != null)
           {
               string error = String.Format("[{0}] {1}", subject, e.Error.ToString());
               MessageBox.Show(error);
           }
           else
           {
               MessageBox.Show("Message sent.");
           }
           mailSent = true;
       }

解决方案

The clue is in the error message.. there is a problem connecting to the SMTP Server, check your connection settings


Add this line of code

client.UseDefaultCredentials = false;



before your

<pre lang="midl">client.Credentials = new System.Net.NetworkCredential
                ("ganesan@gmail.com", "*******");



Have a look at this tip: Sending an Email in C# with or without attachments: generic routine.[^]


这篇关于错误:找不到SMTP主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆