Sending email problem by Gmail account
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Sending email problem by Gmail account

  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 = "";
    client.EnableSsl = true;
    client.Credentials = new System.Net.NetworkCredential(username, password);
    MailAddress mailAdd = new MailAddress("");

    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("");
            message.Sender = new MailAddress("");
            message.To.Add(new MailAddress(""));
            message.Subject = "Hi!";
            client.Credentials = new NetworkCredential("", "mypassword");
            client.EnableSsl = true;
            client.Port = 587;
            client.Host = "";
    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

This is a survey!

HTML5 Development Center