C# 通过Gmail帐户发送电子邮件问题

C# 通过Gmail帐户发送电子邮件问题,c#,.net,email,C#,.net,Email,我正试图用Gmail发送一封C#格式的电子邮件。每当用户收到电子邮件时,我希望“发件人”标题有另一个我自己指定的电子邮件地址。谁能告诉我怎么做 MailMessage mailMsg = new MailMessage(); SmtpClient client = new SmtpClient(); client.Port = 587; client.Host = "smtp.gmail.com"; client.EnableSsl = true; client.Credentials = n

我正试图用Gmail发送一封C#格式的电子邮件。每当用户收到电子邮件时,我希望“发件人”标题有另一个我自己指定的电子邮件地址。谁能告诉我怎么做

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("jack2@gmail.com");

mailMsg.Sender = new MailAddress(username);
mailMsg.From = mailAdd;
//mailMsg.Headers.Add("Sender",username);
mailMsg.Bcc.Add(builder.ToString());

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);
mailMsg.Attachments.Add(attechment);
}

client.Send(mailMsg);

在上述代码中,“用户名”和“密码”字段包含另一个电子邮件地址和密码。如果您提供的邮件不是gmail,也不使用IMAP服务,请尝试使用“发件人”邮件头的值

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

client.Port = 587;
client.Host = "mail.youdomain.com"; //////EDITED
client.EnableSsl = false; //////EDITED
client.Credentials = new System.Net.NetworkCredential(username, password);
MailAddress mailAdd = new MailAddress("jack2@gmail.com");

mailMsg.Sender = new MailAddress(username);
mailMsg.From = mailAdd;
//mailMsg.Headers.Add("Sender",username);
mailMsg.Bcc.Add(builder.ToString());

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);
mailMsg.Attachments.Add(attechment);
}

client.Send(mailMsg);

和的副本。基本上,使用Gmail是无法做到的(按设计),他明确表示他正在使用Gmail,所以你的解决方案将无法工作。他应该读这篇文章