Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to send a email with my asp.net website , here is my code but i have error , can anybody find out where is my mistake ?????

C#
protected void btnsend_Click(object sender, EventArgs e)
{
try
{
string bodyHTML = string.Empty;
string bodyPlain = string.Empty;
MailMessage mailMsg = new MailMessage("info@website.com", txtemail.Text);
mailMsg.Subject = txtsubject.Text;
mailMsg.IsBodyHtml = true;
bodyHTML = txtbody.text;

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(bodyHTML, null, "text/html");
AlternateView plainView = AlternateView.CreateAlternateViewFromString(bodyPlain, null, "text/plain");

mailMsg.AlternateViews.Add(htmlView);
mailMsg.AlternateViews.Add(plainView);
SmtpClient smtp = new SmtpClient();
smtp.Port = 25;
smtp.Host = "smtp.website.com";

smtp.Send(mailMsg);
lblsend.Text = "email sent";
}

catch(Exception ex)
{
lblsend.Text = ex.ToString();
}

}
Posted
Updated 21-Aug-12 11:46am
v2

Does smtp.website.com exist, or did you copy this off the web ? You need to change this code to point to an actual mail server. You also should tell us the actual error message, if you want us to decode it for you.
 
Share this answer
 
Comments
Prasad_Kulkarni 22-Aug-12 0:25am    
5'ed :D
Saeed Jafarian 22-Aug-12 5:15am    
this is the error :

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 209.190.121.56:25 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) 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(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) 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 sendnewsletter.btnsend_Click(Object sender, EventArgs e)
Christian Graus 22-Aug-12 5:44am    
This error means that you have a real mail server, but it is refusing your connection, either because your settings are wrong, such as requiring a username and password, or requiring SSL, or your server is rejecting your connection for other reasons, such as, some ISPs will only let you use their mail server, if you are logged in to the internet through their servers. I would suggest getting a gmail account, and reading up on how to send mail through gmail, or hotmail, or similar. If you have a server you know works for other people, you eliminate that as an issue.
What is the actual error you are getting?
Check whether your smtp server supports SSL and Authentication,

if (_ssl == "SSL")
{
    smtpClient.EnableSsl = true;
}
else
{
    smtpClient.EnableSsl = false;
}
if (_isauthenticate == "Yes")
{
    smtpClient.UseDefaultCredentials = false;
    credentials =
            new System.Net.NetworkCredential(str_user, str_password);

    smtpClient.Credentials = credentials;
}
else
{
    smtpClient.UseDefaultCredentials = true;
    credentials =
            new System.Net.NetworkCredential();
    smtpClient.Credentials = credentials;

}


Hope this helps.
cheers
 
Share this answer
 
Comments
Saeed Jafarian 22-Aug-12 5:15am    
this is the error:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 209.190.121.56:25 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) 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(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) 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 sendnewsletter.btnsend_Click(Object sender, EventArgs e)
Sandip.Nascar 22-Aug-12 9:53am    
check the port. Best idea is to open your gmail account and check the smtp settings.
i try using gmail from this (Send E-Mail from your .NET Application using your GMail Account[^]) and it worked fine.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900