cancel
Showing results for 
Search instead for 
Did you mean: 
Go to solution

Not able to send email via C#.

Hi,
Please help, I am not able to send emails via C#.
I am using this code in my web config file:

 

 <system.net>
        <mailSettings>
            <smtp from="noreply@domain.com">
               <network host="relay-hosting.secureserver.net" port="25" />
            </smtp>
       </mailSettings>
</system.net>

 

and below code in C# file:
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("abc@gmail.com"));
msg.From = new MailAddress("noreply@domain.com");
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.EnableSsl = false;
client.Send(msg);

4 REPLIES 4
Helper I

Re: Not able to send email via C#.

Not really competent with C# but I'd say you check the ports, something around that. I guess. Otherwise I'm really not sure. 

Re: Not able to send email via C#.

Hi @Nvulane,

 

 

I tried using 25, 465 and 587 but nothing worked.

Helper I

Re: Not able to send email via C#.

@Rahul_Jain now i really can't tell. But one thing you can look into, doesn't the whole C# need IIS for hosting? I guess it can run better that way. Otherwise, I hope a solution will come your way soon..

Solution

Re: Not able to send email via C#.

I found a solution and its working now.

 

If you are using Windows VPS and want to send email notifications from your website below code needs to be used:

 

MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(toEmailId));
msg.From = new MailAddress("email@domain.com");
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("email@domain.com", "password");
client.Port = 25;
client.Host = "localhost";

 

Also, you need to create the same email on your windows VPS.