Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
3.60/5 (2 votes)
See more:
this is the code i am trying to run but it gives error

C#
public  void sendmail()
    {
        string to = TextBox2.Text;
        string from = "xyz@gmail.com";
        MailMessage message = new MailMessage(from,to);
        
        message.Subject = "This is my subject";

        message.Body = "This is the content";

        SmtpClient client = new SmtpClient();
               
        client.Send(message);
    }


it wont accept the value from textbox2.text
Posted
Updated 22-Mar-14 5:45am
v2
Comments
CoderPanda 22-Mar-14 5:49am    
Does TextBox2 has runat=server? Can you please post the HTML/ASPX part also?
Member 10454386 22-Mar-14 10:37am    
<asp:TextBox ID="tb2" runat="server">
King Fisher 22-Mar-14 6:04am    
whats your error?

try this.. :)

C#
try
            {
                string to = TextBox2.Text;
                string from = "xyz@gmail.com";


                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
 
                mail.From = new MailAddress("me@mydomain.com");
                mail.To.Add(to);
                mail.Subject = "This is my subject";
                mail.Body = "This is the content";
                Attachment attachment = new Attachment(filename);
                mail.Attachments.Add(attachment);
 
                SmtpServer.Port = 25;
                SmtpServer.Credentials = new System.Net.NetworkCredential("Username", "password");
                SmtpServer.EnableSsl = true;
 
                SmtpServer.Send(mail);
            }
 
Share this answer
 
v2
Comments
CHill60 22-Mar-14 13:51pm    
OP will have problems with this as they don't have a txtEmailSend or a txtBody!
Nirav Prabtani 22-Mar-14 14:41pm    
???
CHill60 22-Mar-14 14:45pm    
In your solution you have a line mail.To.Add(txtEmailSend.Text); - the Original Poster does not have a textbox called txtEmailSend or a textbox called txtBody. If they are already struggling to realise that they've used TextBox2 instead of tb2 then I doubt they could use your solution without further help
Nirav Prabtani 22-Mar-14 14:51pm    
:) :) :) ya you are right, I am updating my code.....
Of course, it won't but It would work like this
C#
public  void sendmail(string toAddr)
{
    string to = toAddr;
    string from = "xyz@gmail.com";
    MailMessage message = new MailMessage(from,to);
    
    message.Subject = "This is my subject";

    message.Body = "This is the content";

    SmtpClient client = new SmtpClient();
           
    client.Send(message);
}

protected void sendBtn_Click(object sender, EventArgs e)
{
    sendMail(TextBox2.Text);
}


Parameters must be passed as an arguments to the function - basic rule of function/method.

-KR
 
Share this answer
 
v2
string to = TextBox2.Text;


Should be

string to = tb2.Text;


[Please accept/up-vote answers or solutions that work for you to encourage participation]
 
Share this answer
 
v2
Comments
Krunal Rohit 22-Mar-14 11:45am    
What ???????????

-KR
CoderPanda 22-Mar-14 11:57am    
sorry, something wrong?
Krunal Rohit 22-Mar-14 12:03pm    
Nah, Sorry.. Didnt read the comments.!

-KR
CHill60 22-Mar-14 13:51pm    
Good spot!
CoderPanda 22-Mar-14 14:28pm    
Thanks :-)

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