Get gmail Name instead of EmailID using C# in ASP.Net

Last Reply on Sep 16, 2016 03:13 AM By dharmendr

Posted on Sep 14, 2016 11:06 PM

Pls refer the below site

http://www.aspsnippets.com/Articles/Send-email-using-Gmail-SMTP-Mail-Server-in-ASPNet.aspx

I could send email using this website.

But the reciever sees the sender's email id instead of his/her name as mentioned in the sender's  gmail account.

PLs help me on how to  send email with sender's name mentioned in the  receiver's inbox

Posted on Sep 14, 2016 11:40 PM Modified on on Sep 14, 2016 11:44 PM

Please check Below Code for your reference. We sets display name for sender from where we sends an email.

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.SendEmail("fromemail@email.com", "Email Subject", "Your Email Body", "Your Display Name");
    }
}

public void SendEmail(string fromEmailAddress, string subject, string body, string displayName)
{
    using (MailMessage mm = new MailMessage())
    {
        // To set Email Id Which user has entered also if Display name is there the pass it as second parameter when assigning mm.From in new MailAddress. 
        // You can set sender's display name 
        mm.From = new MailAddress(fromEmailAddress, displayName);

        // It is receiver email Id which we assign in mm.To.
        mm.To.Add(new MailAddress("Youremail@gmail.com"));

        // Subject which you sets from Email
        mm.Subject = subject;

        // Body which Sets for Email
        mm.Body = body;

        mm.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();

        // Your Account details which you are going to Send Email from Application            
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        System.Net.NetworkCredential networkCred = new System.Net.NetworkCredential();
        networkCred.UserName = fromEmailAddress;
        networkCred.Password = "your gmail password";
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = networkCred;
        smtp.Port = 587;
        smtp.Send(mm);
    }
}

 


Posted on Sep 16, 2016 12:17 AM

Thanks a lot Kalpesh sir for your prompt  reply.


Posted on Sep 16, 2016 03:13 AM
Instead of saying thanks mark the reply or replies(if multiple) that helped as Answer.