0

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("myanotherid@gmail.com", "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("myanotherid@gmail.com", "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 "myanotherid@gmail.com" address.

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

4

3 回答 3

1

我想这是gmail防止发件人欺骗的保护措施。

您不能以 yogibear@gmail.com 的身份登录 GMail 并以barack.obama@whitehouse.gov的身份发送电子邮件。GMail 的 SMTP 将重写邮件的标头以正确指示谁真正发送了电子邮件。

于 2013-06-29T20:19:56.940 回答
0

您的代码看起来正确。Gmail 不允许您指定不同的“发件人地址”,除非您已证明该地址属于您。

转到设置>帐户>“发送电子邮件”并在那里添加一个地址。您只能选择从您在此处配置的任何帐户发送。

于 2013-06-29T20:56:58.313 回答
0

你应该使用 new mailaddress();

MailAddress from = new MailAddress("someone@something.com", "John Doe");
MailAddress to = new MailAddress("someoneelse@something.com", "Jane Doe");
MailMessage mail = new MailMessage(from, to);

在这里进一步阅读:http: //msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx

于 2013-06-29T19:45:41.187 回答