我知道如何通过我的ISP SMTP使用C#发送邮件,但我的SMTP开始将我的电子邮件列入黑名单并将其标记为垃圾邮件。所以我有一个付费服务器,我可以设置电子邮件帐户。我想通过我的服务器SMTP发送邮件。
如果这可以帮助我使用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
答案 0 :(得分: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
如果您没有收到邮件服务器的回复,则需要开始调查原因。
答案 1 :(得分:0)
确保您已在app.config
或web.config
或<{1}}中指定了如何投放邮件:
<system.net>
<mailSettings><!-- set your pickup folder or SMTP server here --></mailSettings>
</system.net>