I wish to incorporate e-mail in my page such that when a user had pressed a button, an email will be sent to the recipient. This has been my code.

MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.BodyFormat = MailFormat.Html;
mail.Body = "this is my test email body.<br><b>this part is in bold</b>";
SmtpMail.SmtpServer = "localhost";  //your real server goes here
SmtpMail.Send( mail );

When I had uploaded the page in my site I received this error:The transport failed to connect to the server.
and the error is pointed to the statement SmtpMail.SmtpServer = "localhost";

What is the problem in my code?

you need an SMTP mail server, andyour local host isn't it :)

find out which is yours, if you have one, and put the address there.

Most hosted domains have mail.domainname.com as there smtp server.

Thanks SheSaidImaPregy! Can you answer my problem on Calendar please. I want that when a user clicks a date in a calendar, a message in a textbox will appear and let's assume that it is a scheduled meeting. Thanks. I badly need the answer please.

Gmail is a very good free smtp and pop3 free service. You can try your email job with gmail.

You can do it through AJAX, Javascript, or to stick with ASP.NET, just changed the onselected change event to call a sub like below:

Sub caldate(s as object, e as eventargs)
    If Page.IsPostBack Then
        tbDate.Text = Calendar1.SelectedDate.ToShortDateString()
    End If
End Sub

Thanks SheSaidImaPregy! The code is great. I just want to know from empkeydotcom if how can I set it as my mail server in my code. Assume that I have an email account as ebabes@gmail.com. I badly need the code for this. Thanks and more power!

you can specify the username and password within the code if it is needed.

however, I am not sure how to do 3rd party email servers with your code.

You will need to add the following code if you have to use username and passwords:

msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25) ;
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort) ;
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic); 
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName); 
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);

Add this code right after you declare your mailmessage.

You need to specify your SMTP server there. Meaning, you need to put the IP Address or the domain name.

What it is telling you is that "smtpServer" is not set or declared on the page, so when reading it, it doesn't do anything.

You can, if you wish, declare the variable as follows:

Dim smtpServer As String = "mail.mydomain.com"

Or whatever your smtpserver address is.

use the system.net / system.net.mail namespace. Gmail, which has free mail and free @yourdomain.com mail service, uses SSL for its smtp, so you need to use port 587.

Dim ObjectMail As New SmtpClient("smtp.gmail.com", 587)
ObjectMail.EnableSsl = True
ObjectMail.Credentials = New NetworkCredential("you@yourdomain.com", "password")
Dim TheMessage As New MailMessage
TheMessage.From = New MailAddress("youraddress@domain.com", "Your Name")
TheMessage.To.Add(user@user.com)

Some places, like godaddy, require you to use their SMTP server is you are hosted on their site.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.