How to send mail from Domain server in c#?

How to send email from my c# website from my domain email id with attachment.


// Email
using System.Net;
using System.Net.Mail;


public void Gomail()
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
//Configure the address we are sending the mail from
MailAddress address = new MailAddress("");
msg.From = address;
msg.Subject = "N K Technology";
string msgbody = "Dear " + "User" + " ,<table align=center width=800><tr><td><B> Your Details are as below </B> </td> </tr><tr><td>Name </td><td> :</td><td> <b>" + txtname.Value + "</b> </tr><tr><td>Designation </td><td> :</td><td> <b>" + txtdesign.Value + "</b> </tr><tr><td>Contact Number </td><td>: </td><td><b>" + txtcontct.Value + "</b></td></tr><tr><td>Address </td><td>: </td><td><b>" + txtadd.Value + "</b></td></tr><tr><td>Email </td><td>: </td><td><b>" + txtemail.Value + " </b></td> </tr><tr><td>Message </td><td> :</td><td> <b>" + "Demo" + "</b></td></tr><tr><td colspan=7 align=right><font size=1><span style='font-size:8.5pt;font-family:TimesNewRoman'><br>Sincerely, <br><br>N.K. Technology<br></span></font><br><font style='font-size:9px'>This mail is autogenerated to confirm your Enquiry</font> </tr></tr></table>";
msg.Body = msgbody.ToString();

SmtpClient client = new SmtpClient();
client.Host = "";
client.Port = 25;

//Send the msg

//Display some feedback to the user to let them know it was sent
//lblResult.Text = "Your message was sent!";
catch (Exception ex)
//If the message failed at some point, let the user know
//lblResult.Text = ex.ToString(); //alt text "Your message failed to send, please try again."


check this code, is this correct, if not please rewrite this code and reply urgently. Deadline till 5 pm today.

Thanks in advance

Asked by:- VivekVishwas
: 5199 At:- 11/4/2017 9:16:02 AM C# SQl send-email-from-domain-server

check port number, add credentials for smtp address also 0
By : vikas_jk - at :- 11/4/2017 10:20:08 AM

2 Answers
profileImage Answered by:- vikas_jk

please check this article for sending email using C#, it has the complete working code, with attachments also

Check section 3 for sending Email in ASP.NET MVC using C# (It will work for Web-forms ASP.NET also, as C# code required is always same in MVC and Web-forms)

At:- 11/4/2017 10:18:48 AM Updated at:- 11/4/2017 10:23:21 AM
This code only works on Gmail , I want to send email from domain server email address like 0
By : VivekVishwas - at :- 11/4/2017 10:26:10 AM
yes, it works for the domain also, we are sending email from using similar code. If you are getting any error, please attach images of error by editing question, as i have commented, check your port, gmail uses 587, godaddy uses port 465 for SSL and 80, 3535, 25 for non SSL, i have sent the email using above code from godaddy hosted Workspace also, mainly issue is related to port. Check port and SMTP client credentials also in you question code, i don't see smpty client password/email 0
By : vikas_jk - at :- 11/4/2017 10:50:07 AM

profileImage Answered by:- jaya

I tried executing your code locally on my pc, and got an error Unable to connect to the remote server while trying to send mail  and

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

I have found a similar query here

The issue is related to the server there is no issue in the code if your server doesn't require credentials and port is 25, so try to send mail asynchronously and check firewall also.

At:- 11/5/2017 12:09:34 PM Updated at:- 11/5/2017 12:09:59 PM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use