开发者

sending mail through C#

I know how to send mail using C# through my ISP SMTP, yet my SMTP started blacklisting my emails and flagging them as spam emails. So I have a paid server, which I can set email accounts on. I want to send mails through my server SMTP.

I`m using WPF if this can help.

this is my code (emails/pwds have been removed)

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);
    }

Always produces this error:

"SMTP Exception has occured: Failure sending mail."

What am I doing wrong here?

EDIT

This is the error that I get when I try 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


According to the inner exception, this is a network connection issue:

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

Forget your code for now. You need to make sure you can actually make an SMTP connection with the mail server. You'll want to ensure the hostname is correct, check firewalls, etc.

If you have the Telnet client installed, you can do this for a quick test:

telnet <host> 25

If you don't receive a response from the mail server, you'll need to start investigating why.


Make sure you've specified how to deliver the mail in your app.config or web.config:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜