Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost \
// smtpClient.Host = "smtp.google.com";

currently i am using : smtpClient.Host = "localhost";

i get mail to gmail id but it also gets error as fake email id report r learn more so if i don't want to get that what i have to do instead of "local host"


from where i get hostname..??
which ip address i have to give gmail or hosting providers..??
and why i get that error "This message may not have been sent by: test@gmail.com Learn more Report phishing"..??

Check code below:

C#
using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page 
{
    #region  "Send email"
    protected void btnSendmail_Click(object sender, EventArgs e)
    {
        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
            MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

            // You can specify the host name or ipaddress of your server
            // Default in IIS will be localhost \
            smtpClient.Host = "smtp.gmail.com";
           // smtpClient.Host = "localhost";

            //Default port will be 25
            smtpClient.Port = 25;

            //From address will be given as a MailAddress Object
            message.From = fromAddress;

            // To address collection of MailAddress
            message.To.Add("karthikh87@gmail.com");
            message.Subject = "Feedback";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")
            message.CC.Add("karthikh87@gmail.com");
            message.CC.Add("karthikh87@gmail.com");

            // You can specify Address directly as string
            message.Bcc.Add(new MailAddress("karthikh87@gmail.com"));
            message.Bcc.Add(new MailAddress("karthikh87@gmail.com"));

            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = false;

            // Message body content
            message.Body = txtMessage.Text;
         
            // Send SMTP mail
            smtpClient.Send(message);

            lblStatus.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
        }
        Response.Redirect("index.html");
    }
    #endregion

    #region "Reset"
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtMessage.Text = "";
        txtEmail.Text = "";
    }
    #endregion
}
Posted
Updated 16-Apr-17 22:50pm
v4

You have to add the following codes to send a mail as simple as possible...

[Example is given for gmail account.
That means you have to have one gmail account to send a mail to any address.]

C#
string pweda = "FromMailPassword"; //(ConfigurationManager.AppSettings["password"]);
        string from = "FromYourmail@gmail.com"; //Replace this with your own correct Gmail Address
        string to = "abc@gef.com"; //Replace this with the Email Address to whom you want to send the mail
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress(from);
        mail.Subject = "This is a test mail";
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = "Test Mail.";
 
        mail.Priority = MailPriority.High;
        SmtpClient client = new SmtpClient();
 
        //Add the Creddentials- use your own email id and password
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(from, pweda);
        client.Port = 587; // Gmail works on this port
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true; //Gmail works on Server Secured Layer

        try
        {
            client.Send(mail);
            Response.Write("Message Sent...");
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty;
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
            HttpContext.Current.Response.Write(errorMessage);
        } // end try


You can send from any mail address if you have any other...
Just replace with the credentials and port and host names which you can get easily get from google.

You can send anything by assigning value to 'mail.Body'.

Add this code where ever you want send the mail...

Happy coding........
 
Share this answer
 
Comments
VICK 26-Oct-13 7:20am    
Hi Tadit.. I have tried your code in the above given link and found the following exception.
Can you explain me a bit please.


System.Net.Mail.SmtpException: 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 at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at EmailTry.btnEmail_Click(Object sender, EventArgs e)
VICK 26-Oct-13 7:22am    
Sorted out.. mistake was mine.. Credentials issue. :) Thanks.
Good to know that. Most welcome. :)
Karthik_Mahalingam 10-Jan-14 9:25am    
Good,
bookmarked :)
Thanks. :)
See my Answer Here
error in asp.net code to send email[^]

Thanks
--RA
 
Share this answer
 
Comments
karthikh87 30-Jan-12 4:53am    
is it mandatory to give our gmail account username and password..??
can we do without giving " smtps.Credentials = new NetworkCredential ( "mail@gmail.com", "pwd" ); "
Rajesh Anuhya 30-Jan-12 4:56am    
No not possible, you have to provide
--RA
karthikh87 30-Jan-12 5:00am    
do u mean all contact forms and feedback forms on the website have there email username and password in coding..??
Rajesh Anuhya 30-Jan-12 5:04am    
if you are using 3-different mail accounts for these forms , defiantly you should have, like Support@gmail.com, feedback@gmail.com, contact@gmail.com
--RA
Try this
Sending E-mail using ASP.net through Gmail account (Gmail SMTP Server account)[^]

Missing ----System.Net.NetworkCredential(from, "Password")
 
Share this answer
 

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