C# 从app.config发送邮件和提取数据
我有个情况。HTML代码是三个文本框和一个按钮的标准代码:C# 从app.config发送邮件和提取数据,c#,asp.net,C#,Asp.net,我有个情况。HTML代码是三个文本框和一个按钮的标准代码: <add key="cma_contact_form_email" value="somec@gmail.com"/> <add key="cma_contact_to_address" value="some@gmail.com"/> <add key="smtpServer" value="smtp.gmail.com" /> <add key="EnableSsl" value = "tr
<add key="cma_contact_form_email" value="somec@gmail.com"/>
<add key="cma_contact_to_address" value="some@gmail.com"/>
<add key="smtpServer" value="smtp.gmail.com" />
<add key="EnableSsl" value = "true"/>
<add key="smtpPort" value="993" />
<add key="smtpUser" value="some@gmail.com" />
<add key="smtpPass" value="pass" />
正常情况下,这应该可以工作,但我得到一个超时错误。有人知道捕获物可能在哪里吗?谢谢 在实际发送电子邮件之前,您可以使用以下方法检查连接是否允许,并且您还应该处理SmtpException。它有一个StatusCode属性,可以告诉您Send()失败的原因 您可以这样调用下面的方法
if(TestConnection("smtpServerAddress",port))
SendEmail();
public static bool TestConnection(string smtpServerAddress, int port)
{
IPHostEntry hostEntry = Dns.GetHostEntry(smtpServerAddress);
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], port);
using (Socket tcpSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
//try to connect and test the rsponse for code 220 = success
tcpSocket.Connect(endPoint);
if (!CheckResponse(tcpSocket, 220))
{
return false;
}
// send HELO and test the response for code 250 = proper response
SendData(tcpSocket, string.Format("HELO {0}\r\n", Dns.GetHostName()));
if (!CheckResponse(tcpSocket, 250))
{
return false;
}
// if we got here it's that we can connect to the smtp server
return true;
}
}
private static bool CheckResponse(Socket socket, int expectedCode)
{
while (socket.Available == 0)
{
System.Threading.Thread.Sleep(100);
}
byte[] responseArray = new byte[1024];
socket.Receive(responseArray, 0, socket.Available, SocketFlags.None);
string responseData = Encoding.ASCII.GetString(responseArray);
int responseCode = Convert.ToInt32(responseData.Substring(0, 3));
if (responseCode == expectedCode)
{
return true;
}
return false;
}
GMAIL设置
- Gmail SMTP服务器地址:SMTP.Gmail.com
- Gmail SMTP用户名:example@gmail.com
- Gmail SMTP密码:密码
- Gmail SMTP端口:465
- Gmail SMTP TLS/SSL必需:是
解析:GMAIL SMTP的正确端口是,而不是它的正确端口。您从哪里得到超时错误?你能发布一条异常消息,一个stacktrace,或者更好地解释发生这种情况的地方吗?除此之外,超时通常听起来像是网络问题。超时错误闻起来像是您的服务器没有对smtp服务器的网络访问权限……所需的端口是否打开?@ArveSystad他很可能在
smtp.send()上获得超时
callUse使用MailSettings
元素,而不是将设置放在AppSettings
部分。您的代码将比这短得多。你确定你的SMTP端口是993吗?我认为这是IMAP端口。不需要complify,他的代码中的try/catch是正确的,问题只是设置了一个错误的端口。尽管try/Catch是不正确的。如果您使用多台服务器发送电子邮件,最好是兼容。我已将smtp端口更改为465,但出现相同的错误“发送邮件时出错。操作已超时。”@mikrimouse您是否添加了代码来检查您是否实际访问了服务器?我在第二个方法中使用senddata和encodeWhy-1对此答案有一些错误?您的答案是正确的,但我建议您澄清一下。让我编辑itI将端口更改为465,并出现相同错误,“发送邮件时出错。操作已超时。”
if(TestConnection("smtpServerAddress",port))
SendEmail();
public static bool TestConnection(string smtpServerAddress, int port)
{
IPHostEntry hostEntry = Dns.GetHostEntry(smtpServerAddress);
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], port);
using (Socket tcpSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
//try to connect and test the rsponse for code 220 = success
tcpSocket.Connect(endPoint);
if (!CheckResponse(tcpSocket, 220))
{
return false;
}
// send HELO and test the response for code 250 = proper response
SendData(tcpSocket, string.Format("HELO {0}\r\n", Dns.GetHostName()));
if (!CheckResponse(tcpSocket, 250))
{
return false;
}
// if we got here it's that we can connect to the smtp server
return true;
}
}
private static bool CheckResponse(Socket socket, int expectedCode)
{
while (socket.Available == 0)
{
System.Threading.Thread.Sleep(100);
}
byte[] responseArray = new byte[1024];
socket.Receive(responseArray, 0, socket.Available, SocketFlags.None);
string responseData = Encoding.ASCII.GetString(responseArray);
int responseCode = Convert.ToInt32(responseData.Substring(0, 3));
if (responseCode == expectedCode)
{
return true;
}
return false;
}