Click here to Skip to main content
15,887,410 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I have placed a page for sending email from users to my gmail account.
My domain name is sedayeir.ir bellow comes my code but nothing happens and the script cant send email to my gmail account. What is wrong with it?

protected void btnSend_Click(object sender, EventArgs e)<br />
    {<br />
        if (Page.IsValid)<br />
        {<br />
<br />
            try<br />
            {<br />
                MailMessage mailMessage = new MailMessage();<br />
                mailMessage.To.Add("lingo1357@gmail.com");<br />
                mailMessage.From = new MailAddress(txtEmail.Text, txtName.Text);<br />
                mailMessage.Subject = txtSubject.Text;<br />
                mailMessage.Body = txtMessage.Text;<br />
<br />
                mailMessage.IsBodyHtml = false;<br />
                SmtpClient smtpClient = new SmtpClient("mail.sedayetasvir.ir");<br />
                smtpClient.Send(mailMessage);<br />
                lblEmailResult.Text = "ایمیل با موفقیت ارسال شد";<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                lblEmailResult.Text = "خطا در ارسال ایمیل ، لطفا مجددا تلاش نمایید " + ex.Message;<br />
            }<br />
        }<br />
<br />
    }<br />


can you please help me?

thanks,
Posted

"Send a mail to gmail" is absurd. There is a global mail system where anyone can send a mail to any address (except some cases when someone does something especially stupid :-)). Sooner or later, you will succeed, too. Start with checking the operation of your SMTO server "mail.sedayetasvir.ir": make sure it works correctly with some available mail client, pay attention for all options and reproduce them in your code. Use the debugger and make sure your code (which is not "script", by the way) is even called and finished.

You have one bigger problem. If you ignore it, your site's host can be turned into a zombie sending spam (or something like that) in no time. Please see my past answer: unable to send mail , it showing the error in below code .[^].

—SA
 
Share this answer
 
Thanks for the reply.

I changed my code to this:

C#
protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {

            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add("lingo1357@gmail.com");
                mailMessage.From = new MailAddress(txtEmail.Text, txtName.Text);
                mailMessage.Subject = txtSubject.Text;
                mailMessage.Body = txtMessage.Text;

                mailMessage.IsBodyHtml = false;
                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 465); 
                smtpClient.Credentials = new System.Net.NetworkCredential("ling1357@gmail.com", "lingo13410");
                smtpClient.EnableSsl = true;
                smtpClient.Send(mailMessage);
                lblEmailResult.Text = "ایمیل با موفقیت ارسال شد";
            }
            catch (Exception ex)
            {
                lblEmailResult.Text = "خطا در ارسال ایمیل ، لطفا مجددا تلاش نمایید " + ex.Message;
            }
        }

    }


also I used these articles:

http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm

https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server

but still keep this error: the operation has timed out.

you can try it your self, the page is: http://sedayetasvir.ir/Email.aspx

The first textbox is for name.

The second textbox is for subject.

The third textbox is for email and the forth textbox is for message.

Also the left button is for sending and the right button is for cleaning.

Let me know your opinion and what is wrong with my code.

Thanks
 
Share this answer
 
v3

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