这段代码如何在服务器上运行 [英] How can this code work on server

查看:84
本文介绍了这段代码如何在服务器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 MailMessage msg =  new  MailMessage(); 
msg.From = new MailAddress(txtEmail.Text);
msg.To.Add( mymailid);
msg.IsBodyHtml = true ;
msg.Body = 名称: + txtname。文字+ < br />< br /> 电子邮件: + txtEmail.Text + < br />< br /> 电话: + txtmobile.Text + < br />< br /> ; 消息: + Txtmessage.Text + < br> ;
msg.Subject = txtSubject.Text;
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = smtp.gmail.com;
smtpclient.Port = 587 ;
smtpclient.Credentials = new System.Net.NetworkCredential( mymailid password);
smtpclient.EnableSsl = true ;
smtpclient.Send(msg);
Label1.Text = 感谢您与我们联系;
txtname.Text = ;
txtEmail.Text = ;
txtSubject.Text = ;
Txtmessage.Text = ;
txtmobile.Text = ;
}
catch (例外情况)
{
Console.WriteLine( {0}执行警告。,ex);
}







这个我的代码在本地主机上工作但在我放的时候没有工作它在服务器上可以建议我做什么。



i想知道是否有其他主机(雅虎,我自己的网站等)然后我需要改变什么在我的代码中做



我尝试过:



已更改标签为C#

解决方案

检查此链接!



它适用于其他用户,如果它不起作用,请检查服务器上的防火墙设置!

尝试运行这个代码在一个示例代码段中,网络中必定存在可能阻止您的代码发送电子邮件的内容。

正如Marco建议的那样,尝试从服务器pingsmtp.google.com检查服务器是否允许访问smtp。



回答第2个问题:

如果要将代码用于其他SMTP客户端,则可以将SmtpClient设置更改为SmtpServer名称和端口。 :)

MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtEmail.Text);
msg.To.Add("mymailid");
msg.IsBodyHtml = true;
msg.Body = "Name : " + txtname.Text + "<br /><br /> Email : " + txtEmail.Text + "<br /><br /> Phone : " + txtmobile.Text + "<br /><br /> Message : " + Txtmessage.Text + "<br>";
msg.Subject = txtSubject.Text;
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = "smtp.gmail.com";
smtpclient.Port = 587;
smtpclient.Credentials = new System.Net.NetworkCredential("mymailid", "password");
smtpclient.EnableSsl = true;
smtpclient.Send(msg);
Label1.Text = "Thanks for contacting us";
txtname.Text = "";
txtEmail.Text = "";
txtSubject.Text = "";
Txtmessage.Text = "";
txtmobile.Text = "";
}
catch(Exception ex)
{
Console.WriteLine("{0} Exeception caugh.",ex);
}




this code of mine working on local host but not working when i put it on server can an one suggest me what to do.

i als like to ask if there is another host(yahoo,my own website etc) then what changes i need to do in my code

What I have tried:

Changed the tag to C#

解决方案

Check this link!

It worked for other users, if it doesn't work, Check the firewall settings on server!
Try to run this code in a sample snippet, there must be something from the network which might be preventing your code to send an email.
As Marco suggested, try to ping "smtp.google.com" from server to check if server allows access to smtp.

Answer of 2nd question:
If you want to use the code for other SMTP Clients, then you can change the SmtpClient settings as SmtpServer name and port. :)


这篇关于这段代码如何在服务器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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