簡體   English   中英

通過C#發送郵件

[英]sending mail through C#

我知道如何使用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

如果您沒有收到郵件服務器的響應,則需要開始調查原因。

確保您已在app.configweb.config指定了如何傳遞郵件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM