Click here to Skip to main content
12,349,554 members (29,370 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Below is my code for sending mail.the only problem is that the line marked bold at the end of the code('clear()')gives me the error "Method must have return type".Can anybody help me in this regard....

*************************************************************

public partial class contact_us : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
	{
	}
    public void clear()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
    }
    
    protected void image_Click(object sender, ImageClickEventArgs e)
    {
        
        //(1) Create the MailMessage instance
        
 
            MailMessage mail = new MailMessage();
            mail.To.Add("xyz@gmail.com");
            mail.From = new MailAddress(TextBox2.Text);
            mail.Subject = "Inquiry regarding Umang Party Plot";
            mail.Body =  TextBox4.Text;
            mail.IsBodyHtml = true;
 
            //(3) Create the SmtpClient object
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
 
            //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "12345");
 
            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);
        
       
        Response.Write("<script language="'javascript'">alert('Your Inquiry has been Successfully send.');location.href('contact_us.aspx');</script>");
     
    }
  clear();
}
Posted 23-Dec-12 19:59pm
Rohan Leuva119.4K
Edited 23-Dec-12 20:01pm
v3
Comments
Krunal Rohit 24-Dec-12 2:03am
   
I assume that you are aware with that Void returns nothing..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

clear();
Response.Write("<script language='javascript'>alert('Your Inquiry has been Successfully send.');location.href('contact_us.aspx');</script>");


cut clear() method then paste it before Response.Write();
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

clear() is a function here. You cannot call the function inside the class directly. You can only declare a function in the class. A function can be called inside a function. Do a little modificaiton in your coding. Here it is:
public partial class contact_us : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
	{
	}
    public void clear()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
    }
    
    protected void image_Click(object sender, ImageClickEventArgs e)
    {
        
        //(1) Create the MailMessage instance
        
 
            MailMessage mail = new MailMessage();
            mail.To.Add("xyz@gmail.com");
            mail.From = new MailAddress(TextBox2.Text);
            mail.Subject = "Inquiry regarding Umang Party Plot";
            mail.Body =  TextBox4.Text;
            mail.IsBodyHtml = true;
 
            //(3) Create the SmtpClient object
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
 
            //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "12345");
 
            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);
        
       
        Response.Write("<script language="'javascript'">alert('Your Inquiry has been Successfully send.');location.href('contact_us.aspx');</script>");
        //Put it inside the function.
        clear();
    }
}


--Amit
  Permalink  
Comments
Rohan Leuva 24-Dec-12 2:13am
   
ok.the problem got solved...thanks for that...now the problem is every time i reload the page,the mail is being sent....how to overcome this issue?as i am new to c#?
_Amy 24-Dec-12 2:19am
   
Yes. I know, this issue is there from the beginning in Asp.Net. After pressing F5 it'll ask you to resend the data. And if you'll click resend, then the mail will be sent. No need to worry about that issue. Actually it's not an issue.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100