1

I particularly made this tutorial as a basis: http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp

I successfully debug the errors but then it doesn't seem to work... HELP please...

PREVIEW:

my contact us form

CODE BEHIND:

   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {

} protected void SendMail() { // Gmail Address from where you send the mail var fromAddress = "[email protected]"; // any address where the email will be sending var toAddress = YourEmail.Text.ToString(); //Password of your gmail address const string fromPassword = "Password"; // Passing the values and make a email formate to display string subject = YourSubject.Text.ToString(); string body = "From: " + YourName.Text + "\n"; body += "Email: " + YourEmail.Text + "\n"; body += "Subject: " + YourSubject.Text + "\n"; body += "Question: \n" + Comments.Text + "\n"; // smtp settings var smtp = new System.Net.Mail.SmtpClient(); { smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); smtp.Timeout = 20000; } // Passing values to smtp object smtp.Send(fromAddress, toAddress, subject, body); } protected void Button1_Click(object sender, EventArgs e) { try { //here on button click what will done SendMail(); DisplayMessage.Text = "Message sent!"; DisplayMessage.Visible = true; YourSubject.Text = ""; YourEmail.Text = ""; YourName.Text = ""; Comments.Text = ""; } catch (Exception) { } }}

4
  • You need to provide more details about WHAT doesn't work and WHAT errors you encounter Commented Jun 24, 2013 at 8:10
  • which error occured and where.. Commented Jun 24, 2013 at 8:12
  • no mail is being sent... Commented Jun 24, 2013 at 8:15
  • Please look How to use gmail SMTP in ASP.NET form Commented Jun 24, 2013 at 8:19

3 Answers 3

0

I think Gmail SMTP port should be 465, not 587.

https://support.google.com/mail/troubleshooter/1668960?hl=en&from=75726&rd=1#ts=1665018,1665142

Sign up to request clarification or add additional context in comments.

2 Comments

I use 587 successfully, but it is Google... var client = new SmtpClient("smtp.gmail.com", 587) { Credentials = new NetworkCredential("[email protected]", "pw"), EnableSsl = true };
Log in to your account and check Sent folder. It the smtp.Send method doesn't throw an exception it means that the connection is correct.
0

Have you tried commenting on that article / contact the author?

I guess there are 2 things here:

A. You haven't done anything in catch block. You should consider following in catch block

catch(Exception ex){
DisplayMessage.Text = ex.Message;
DisplayMessage.Visible = true;
}

B. check if the gmail account you are using has smtp with pop3/imap enabled.

Comments

0

try this

    protected void SendMail()
    {
        // any address where the email will be sending
        var toAddress = YourEmail.Text.ToString();
        //Password of your gmail address
        const string fromPassword = "Password";
        // Passing the values and make a email formate to display
        string subject = YourSubject.Text.ToString();
        string body = "From: " + YourName.Text + "\n";
        body += "Email: " + YourEmail.Text + "\n";
        body += "Subject: " + YourSubject.Text + "\n";
        body += "Question: \n" + Comments.Text + "\n";

        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress("[email protected]");
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.Port = 587;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.EnableSsl = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", fromPassword);
        message.From = fromAddress;
        message.To.Add(toAddress);
        message.Subject = subject;
        message.Priority = MailPriority.High;
        message.Body = body;
        message.IsBodyHtml = true;
        smtpClient.Send(message);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.