[RESOLVED]how to send mail from aspx contact form page in hosting

I am creating one contact form page in aspx and also host this web form in znetlive hosting.after hosting this webpage,i run this page on server.

i fill all fields in contact form and click submit.only postback is done and mail could not be sent. on server.but this web page run on localhost.

I won’t set any mail settings in web.config also

this is my aspx.cs

MailMessage Msg = new MailMessage();

Msg.From = new MailAddress(txtemail.Text);

Msg.To.Add("mailid@gmail.com");
Msg.Subject = txtsubject.Text;
Msg.Body = txtmessage.Text;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("mailid@gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(Msg);

lbldisplaymessage.Text = "Thanks for Contact us";

txtname.Text = "";
txtsubject.Text = "";
txtmessage.Text = "";
txtemail.Text = "";

please help me

thanks in advance,

vasan.p

Check this code to configure

SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25);

smtpClient.Credentials = new System.Net.NetworkCredential("info@MyWebsiteDomainName.com", "myIDPassword");
            smtpClient.UseDefaultCredentials = true;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = true;
            MailMessage mail = new MailMessage();


            //Setting From , To and CC
            mail.From = new MailAddress("info@MyWebsiteDomainName", "MyWeb Site");
            mail.To.Add(new MailAddress("info@MyWebsiteDomainName"));
            mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));


smtpClient.Send(mail);

Hi,

To send e-mail you need to configure SMTP server.

To Configure a SMTP refer this link : Configuring SMTP Email(IIS 7)

 C# use  System.Net.Mail namespace for sending email.

Add the below code inside your button click event

//Set email user name - Change this as per your settings
            const string username = "testuser@googlemail.com";
            //Set the email password - Change this as per your settings
            const string password = "xxxxxxxxx";
            SmtpClient smtpclient = new SmtpClient();
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            //Set the email from address of mail message -  Change this as per your settings
            MailAddress fromaddress = new MailAddress("testuser@googlemail.com");
            //Set the email smtp host -  Change this as per your settings
            smtpclient.Host = "smtp.gmail.com";
            //Set the email client port -  Change this as per your settings
            smtpclient.Port = 587;
            mail.From = fromaddress;
            //Adding email id of receiver link
            mail.To.Add("testuser@googlemail.com");
            //Set the email subject
            mail.Subject = ("Test Subject");
            mail.IsBodyHtml = true;
            //Set the email body - Change this as per your logic
            mail.Body = "This is test Mail";
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Credentials = new System.Net.NetworkCredential(username, password);
            try
            {
                //Sending Email
                smtpclient.Send(mail);
                Response.Write("<B>Email Has been sent successfully.</B>");
            }
            catch (Exception ex)
            {
                //Catch if any exception occurs
                Response.Write(ex.Message);
            }

Above smtp settings are for sample, you may need to change this as per your smtp server settings.

You can also check the below links

@sandy

thank u sandy for your reply but.i also want know about smtp.host for my hosting znetlive server<br>
<br>
<br>
smtp.host=smtp.gmail.com// it is run in localhost but not in server<br>
<br>
so please tell alternate smtp.host=????? for my hosting znetlive server<br>
<br>
<br>
thank in advance,<br>
vasan.p

Please check below thread :

https://manage.znetlive.com/knowledgebase_articels_detail.php?artid=MTE4&back=yes

if you still not able to resolve your query. please ask your Hosting provider <znetlive> to give information about smtp server details for your domain.

They can help you on this.

Hope this will help !!!

Since you use gmail account, I suggest you download phpmailer. pretty straight for gmail smtp configuration.

Leave a Reply