Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;


public partial class contact : System.Web.UI.Page
{

    protected void SendEmail()
    {
        try
        {
            MailMessage message = new MailMessage();
            MailAddress Sender = new MailAddress(ConfigurationManager.AppSettings["smtpUser"], "Inquiry");
            MailAddress receiver = new MailAddress("example@gmail.com", "Inquiry");
            SmtpClient smtp = new SmtpClient()
            {
                Host = ConfigurationManager.AppSettings["smtpServer"],
                Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]),
                EnableSsl = true,
                UseDefaultCredentials = false,
                Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"])

            };
            message.From = Sender;
            message.To.Add(receiver);

            message.Subject = "New Inquiry @ " + txtname.Text;

            string body = "From: " + txtname.Text + " <br />";
            body += "Mobile Phone: " + txtmobile.Text + " <br />";
            body += "Message: " + txtmsg.Text + " <br />";

            message.Body = body;
            message.IsBodyHtml = true;
            smtp.Send(message);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        SendEmail();
        ScriptManager.RegisterStartupScript(this, GetType(), "alertmsg", "alert('Thank you for Inquiry!Our team contact you soon');", true);
       
    }
}


What I have tried:

what is the problem in it
mail does not send
please help to solve this problem
Posted
Updated 10-May-16 20:52pm
v2
Comments
Michael_Davies 11-May-16 2:11am    
What is the error message?
Member 11811231 11-May-16 2:15am    
no error on it but email cant recive
Member 11811231 11-May-16 2:22am    
ya bro i'm getting error something like that

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Michael_Davies 11-May-16 2:35am    
What are the values of smtpServer and smtpPort.
Jawad Ahmed Tanoli 11-May-16 2:21am    
Check in your spam

1 solution

try below snippet, here I am sending mail using Gmail account
put your code always I TRY..CATCH block to get exact error
C#
MailMessage mail = new MailMessage();

//put mail from Email
 mail.From = new MailAddress("MailFrom@gmail.com");

//put mail to Email
 mail.To.Add(new MailAddress("MailTo@gmail.com"));


//you can add blank carbon copy mails here
//mail.Bcc.Add(new MailAddress("YOUR BCC EMAIL"));

//Get an attachment in attachment variable class
Attachment att = new Attachment("D:\\test.txt");

//add attachment to email
 mail.Attachments.Add(att);

//Put email subject
 mail.Subject = "Send Email with Gmail using ASP.NET";


//Put email body
 string Body = "Put your Email Body TEXT here";

// Here you can put HTML string if you want email to be sent in HTML format.   
 mail.Body = Body;
 mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "PASSWORD");

//keep ssl true if you have to send email with secured socket layer
smtp.EnableSsl = false;

smtp.Send(mail);


Exception points
There are so many factor that may affects your email sending code, here is list,
1. Firewall block port 587 (This is port for smtp gmail server)
2. Internet connection is not available on machine or internet connection has face some obstacles while pinging
3. Due to some typo mistake UserName and password may be get wrong
4. Enable SSL is block email sending
5. POP settings for smtp server in your Gmail account is disabled
 
Share this answer
 
Comments
Maciej Los 11-May-16 3:03am    
5ed!
koolprasad2003 11-May-16 3:38am    
Thankx

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