user2535165
user2535165

Reputation:

Email id address issue in c# code

I wrote a simple program in C# Winforms for sending an email and my code is mentioned below:-

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }
        public MailMessage rtnMail()
        {
            string to = txt_To.Text;
            string from = txt_From.Text;
            string subject = txt_Subject.Text;
            string body = txt_Body.Text;
            MailMessage message = new MailMessage(from, to, subject, body);
            return message;
        }

         //Button click event

        private void btn_Send_Click(object sender, EventArgs e)
        {
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
            smtp.EnableSsl = true;
            smtp.Timeout = 500000;
            smtp.Send(this.rtnMail());
        } 

    }

when i run this code and put all the values in textboxes like (to, from, body, subject) and click the "Send" button i do end up getting an email at an address

mentioned in the Textbox named txt_To ( which is my recipient gmail account id).But whenever i look at which address(email id) i got this email from in Microsoft

Outlook (which i have configued for my gmail recipeint account), it always says that i got this email from the email address mentioned as first argument in the line of

code below,

smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password");

My question is, am i doing anything wrong because i expect that email address from which im receiving an email( in my outlook gmail) should be the one that i put in

TextBox named txt_From rather than from "[email protected]" address.

Is there a work around or does there exist an alternate to it.

Upvotes: 0

Views: 203

Answers (3)

Jan Dobkowski
Jan Dobkowski

Reputation: 661

I guess it's gmail's protection to prevent sender spoofing.

You can't login to GMail as [email protected] and send an e-mail as [email protected]. GMail's SMTP will rewrite the message's header to properly indicate who has really sent the e-mail.

Upvotes: 1

Ben Harrison
Ben Harrison

Reputation: 2219

Your code looks correct. Gmail does not allow you to specify a different 'from address' unless it is one you have proven belongs to you.

Go to Settings > Accounts > 'Send email as' and add an address there. You can only choose to send from any of the accounts you have configured here.

Upvotes: 0

reggaemahn
reggaemahn

Reputation: 6668

You should use new mailaddress();

MailAddress from = new MailAddress("[email protected]", "John Doe");
MailAddress to = new MailAddress("[email protected]", "Jane Doe");
MailMessage mail = new MailMessage(from, to);

further reading here: http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx

Upvotes: 0

Related Questions