2010-11-24 148 views
4

我知道如何使用C#通过我的ISP SMTP发送邮件,但是我的SMTP开始将我的电子邮件列入黑名单并将它们标记为垃圾邮件。所以我有一个付费服务器,我可以设置电子邮件帐户。我想通过我的服务器SMTP发送邮件。通过C#发送邮件

我使用WPF如果这可以提供帮助。

这是我的代码(电子邮件/残疾人士已被删除)

try 
{ 
    SmtpClient client = new SmtpClient("got it from the CPanel"); 
    client.UseDefaultCredentials = false; 
    NetworkCredential basicAuthInfo = new NetworkCredential("email username", "email password"); 
    client.Credentials = basicAuthInfo; 

    MailAddress from = new MailAddress("from email address"); 
    MailAddress to = new MailAddress("to email address"); 

    MailMessage mail = new MailMessage(from,to); 

    mail.Subject = txt_subj.Text.ToString(); 
    mail.SubjectEncoding = Encoding.UTF8; 

    mail.Body = "<b>Test Mail</b><br>using <b>HTML</b>."; 
    mail.BodyEncoding = Encoding.UTF8; 

    mail.IsBodyHtml = true; 

    client.Send(mail); 

    mail.IsBodyHtml = true; 
    client.Send(mail); 
    } 
    catch (SmtpException ex) 
    { 
     MessageBox.Show("SMTP Exception has occured: " + ex.Message); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show("Error Occured: " + ex.Message); 
    } 

总会产生这样的错误:

“SMTP异常已发生:发送邮件失败”

我在这里做错了什么?

编辑

这是我收到的时候我尝试ex.ToString()错误:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 92.242.144.5:25 
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) 
--- End of inner exception stack trace --- 
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) 
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) 
at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) 
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) 
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) 
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) 
at System.Net.Mail.SmtpClient.GetConnection() 
at System.Net.Mail.SmtpClient.Send(MailMessage message) 
--- End of inner exception stack trace --- 
at System.Net.Mail.SmtpClient.Send(MailMessage message) 
at RemoteMySQL.mail.button1_Click(Object sender, RoutedEventArgs e) in D:\SIKAS_TEST\RemoteMySQL\RemoteMySQL\mail.xaml.cs:line 53 
+1

是否存在内部异常?试试`ex.ToString()`,它会给出更多的信息 – PostMan 2010-11-24 22:19:14

+0

或在该行上放置一个断点并潜入。究竟是什么原因? – 2010-11-24 22:20:55

+0

我已更新内部异常的帖子 – sikas 2010-11-24 22:36:24

回答

4

根据内部异常,这是一个网络连接问题:

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 92.242.144.5:25 

忘记你的代码吧。您需要确保实际上可以与邮件服务器建立SMTP连接。你要确保主机名是否正确,检查防火墙等

如果您已经安装Telnet客户端,您可以快速测试做到这一点:

telnet <host> 25 

如果您没有收到来自邮件服务器的回应,您需要开始调查原因。

0

确保您指定如何在app.configweb.config投递邮件:

<system.net> 
    <mailSettings><!-- set your pickup folder or SMTP server here --></mailSettings> 
</system.net>