CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2011

    Sending email problem by Gmail account


    I'm trying to send a email in C# using Gmail. I want the 'from' header to have another my own specified email address whenever user receive email. Could anyone please tell me how can I do this?

    MailMessage mailMsg = new MailMessage();
    SmtpClient client = new SmtpClient();

    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    client.Credentials = new System.Net.NetworkCredential(username, password);
    MailAddress mailAdd = new MailAddress("pawan22@gmail.com");

    mailMsg.Sender = new MailAddress(username);
    mailMsg.From = mailAdd;

    mailMsg.Subject = txtSubject.Text;
    mailMsg.Body = txtBody.Text;
    mailMsg.IsBodyHtml = chkHtmlBody.Checked;
    if (System.IO.File.Exists(txtAttechments.Text))
    System.Net.Mail.Attachment attechment = new Attachment(txtAttechments.Text);


    In above code 'username' and 'password' fields contain another email address and password. The received email having 'from' header with value of 'username' filed.

  2. #2
    Join Date
    Jul 2007

    Re: Sending email problem by Gmail account

    I do this a similar way. I tested the following code and it works fine:

            SmtpClient client = new SmtpClient();
            MailMessage message = new MailMessage();
            message.Body = "Hello, there!";
            message.From = new MailAddress("noreply@mycompany.com");
            message.Sender = new MailAddress("noreply@mycompany.com");
            message.To.Add(new MailAddress("errorsatmycompany@gmail.com"));
            message.Subject = "Hi!";
            client.Credentials = new NetworkCredential("errorsatmycompany@gmail.com", "mypassword");
            client.EnableSsl = true;
            client.Port = 587;
            client.Host = "smtp.googlemail.com";
    It just uses the email accounts user name and password, but the From field in gmail always shows up as "me". Odd...
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)