Click here to Skip to main content
15,888,052 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want a functionality to send automatic email to any gmail/yahoo/rediffmail/......... account from my website which is on my local machine. Please help me with the coding as well as server settings if any.
Have added

C#
using System.Web.Mail;


I have added a button on whose click, the following will happen....

C#
try
       {
           SmtpMail.SmtpServer = "localhost";
           SmtpMail.Send("abcdef@rediffmail.com", "uvwxyz@gmail.com", "Hi", "Mail sent");
           Label9.Text = "Message sent!";
       }
       catch (Exception ex)
       {
           Label9.Text = ex.Message;
       }


On button click, Label9.Text is: "The transport failed to connect to the server."
I am a beginner and dont know much about such things??? Plzzzzzzzzz reply....... Have to complete project......... :confused::confused::confused:
Posted

It can be because of various reasons. You need to look at them one by one.
Is the port open? Firewall permissions in place?
Further make sure you have configured SMTP configuration in Web.Config:
<system.net>
   <mailSettings>
     <smtp from="abc@somedomain.com">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />
     </smtp>
   </mailSettings>
</system.net>


If needed, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]
Further, if needed, there are lots of article on this very site on how to send emails.
 
Share this answer
 
Comments
Tech Code Freak 23-Feb-11 2:41am    
First of all, Thanks for the reply!!!

I dont know all these configuration settings... Please tell me what values to assign to host, port, userName, password??
Sandeep Mewara 23-Feb-11 3:45am    
You must be kidding! You are asking me these things? :)
Tech Code Freak 23-Feb-11 10:42am    
May be shameful but really I dont know those!
I'm a beginner ..............
Sandeep Mewara 23-Feb-11 10:48am    
It's your smtp server, the one that you will use in order to send emails.

Port is going to be configured by you/your IT that would be used for emailing.

Username/Password is again going to be yours that authenticates and lets you send an email.

There is nothing here that me or anyone can tell/help you out with.
Tech Code Freak 24-Feb-11 0:17am    
Okay, I understood.
Can you please give me examples of some smtp servers that I can use for this task??
Plzzz..........
in the web.config
XML
<appSettings>
<add key="SMTP" value="hostname.com"/>
<add key="FROMEMAIL" value="myemailadderss@yahoo.com"/>
<add key="FROMEPWD" value="ur email pwd">
<add key="PORT" value="25">
</appSettings>

and use following code to send the mail
C#
protected bool Send(string To, string From, string Subj, string Msg)
    {
        try
        {
            string mFrom = From.ToString().Trim();
            string mTo = To.ToString().Trim();
            string mSubject = Subj.ToString().Trim();
            string mMsg = Msg.ToString().Trim();

            string mMailServer = ConfigurationManager.AppSettings.Get("SMTP");
            int mPort = Convert.ToInt32(ConfigurationManager.AppSettings.Get("PORT"));

            MailMessage message = new MailMessage(mFrom, mTo, mSubject, mMsg);
            SmtpClient mySmtpClient = new SmtpClient(mMailServer, mPort);
            // mySmtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
            //mySmtpClient.EnableSsl = true;
            message.Priority = MailPriority.High;
            mySmtpClient.UseDefaultCredentials = true;
            mySmtpClient.Send(message);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
 
Share this answer
 
v2

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