Sending mail through C #

I know how to send mail using C # through my SMTP provider, but my SMTP has started a blacklist of my emails and is marked as spam messages. Therefore, I have a paid server on which I can set up email accounts. I want to send mail through an SMTP server.

I am using WPF if this can help.

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

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: Error sending mail."

What am I doing wrong here?

EDIT

This is the error I get when trying 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 
+4
source share
2 answers

According to internal 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. You need to make sure that you can really establish an SMTP connection with the mail server. You want the hostname to be valid, check firewalls, etc.

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

 telnet <host> 25 

If you do not receive a response from the mail server, you will need to start investigating the reasons.

+4
source

Make sure you specify how to deliver mail in app.config or web.config :

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

All Articles