【问题标题】:Sending email problem by Gmail account通过 Gmail 帐户发送电子邮件问题
【发布时间】:2011-02-14 20:02:18
【问题描述】:

我正在尝试使用 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);

在上面的代码中,“用户名”和“密码”字段包含另一个电子邮件地址和密码。收到的电子邮件具有“发件人”标头,值为

【问题讨论】:

标签: c# .net email


【解决方案1】:

如果您的邮件提供的不是 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);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 2016-05-28
  • 2011-12-15
  • 2013-03-24
  • 2012-03-30
相关资源
最近更新 更多