c# - 通过C#发送邮件

标签 c# wpf smtp sendmail

我知道如何使用 C# 通过我的 ISP SMTP 发送邮件,但我的 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

最佳答案

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

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

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

关于c# - 通过C#发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4272041/

相关文章:

wpf - 同一程序集中 WPF 和 Silverlight 的资源

IIS SMTP TLS 加密

python - 通过 Python 发送电子邮件的程序以 "AttributeError: ' str 退出'对象没有属性 'get_content_maintype''"

c# - .NET 反射依赖注入(inject)与多个容器

c# - 从包含泛型类的列表中删除重复项

c# - 超出最大请求长度。 IIS 8.0 读取 pcap 文件 ?c#

c# - 绑定(bind)到 WPF 中的祖先

c# - 绑定(bind)到静态属性不响应 PropertyChanged

wpf - 如何在MVVM模式中验证ViewModel中的数据?

java - 使用java发送简单的电子邮件