在C#中发送电子邮件问题

时间:2011-02-14 07:21:52

标签: c# .net email gmail

我创建了一个C#应用程序,它使用gmail中的不同电子邮件地址发送邮件,但是尽管从不同的电子邮件地址发送邮件,我想在“from”标题中显示一个特定的指定电子邮件地址每当任何用户收到电子邮件,我想要用户对该特定指定电子邮件地址的回复。

我可以知道怎么办?

StringBuilder builder = new StringBuilder();
for (int j = i; j < (i + quantity) && j < EmailArr.Length; j++)
{
   //Add new sender email address
   builder.Append(EmailArr[j] + ",");
}

builder.Remove(builder.Length - 1, 1);

btnSend.Text = "Wait...";
this.Enabled = false;

MailMessage mailMsg = new MailMessage();
MailAddress mailAdd = new MailAddress(username);

mailMsg.From = mailAdd;
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);
}

SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;

client.Credentials = new System.Net.NetworkCredential(username, password);

client.Send(mailMsg);

3 个答案:

答案 0 :(得分:0)

只需输入您想要的地址即可。请注意,某些邮件服务器可能不允许您设置不反映该服务器上有效帐户的发件人地址。

答案 1 :(得分:0)

我不明白你想要做什么,但你可能也想看看Sender属性。

但是,根据确切的问题,由于GMail的工作方式,这也可能是一个问题。我有一些记忆,必须做一些设置来摆脱某些“发送代表”文本显示在from字段中,但如果这是问题,那么这个问题可能更适合{{ 3}}

答案 2 :(得分:0)

您可以使用此代码

using System.Net.Mail;
using System.Net;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@yahoo.com", "To Name");
const string fromPassword = "password";
const string subject = "test";
 const string body = "Hey now!!";

var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000

};

  using (var message = new MailMessage(fromAddress, toAddress)
  {
   Subject = subject,
  Body = body
 })
 {
  smtp.Send(message);
 }