用c#.net问题发送联系信息中的邮件?请帮忙

时间:2010-05-20 21:39:55

标签: c# asp.net sendmail

protected void Button2_Click(object sender, EventArgs e)
{
 string ad = TextBox1.Text;
 string firma = TextBox2.Text;
 string mail = TextBox3.Text;
 string tel = TextBox4.Text;
 string tel2 = TextBox5.Text;
 string fax = TextBox6.Text;
 string fax2 = TextBox7.Text;
 string web = TextBox8.Text;
 string mesaj = TextBox9.Text;

 try 
 {
  string fromAddress = "user@gmail.com";
  string fromName = "user";
  string toMail = "user@gmail.com";
  string toNme = "Mr.";
  string msgSubject = "Contact";
  string sifre = "userpassword";

  string msgBody = "you have a message; \n"
  + "\n"
  + "\n"
  + "Mesaj? Gonderenin Ad? :" + ad + "\n"
  + "Mesaj? Gonderen Firma :" + firma + "\n"
  + "Mesaj? Gonderenin Maili :" + mail + "\n"
  + "Mesaj? Gonderenin Tel. Numaras? :" + tel + tel2 + "\n"
  + "Mesaj? Gonderenin Fax Numaras? :" + fax + fax2 + "\n"
  + "Mesaj? Gonderenin Web Adresi :" + web + "\n"
  + "\n"
  + "\n"
  + "" + mesaj + ""
  + "\n"
  + "\n"
  + "=======================================" + "\n";

  SmtpClient client = new SmtpClient();
  client.Credentials = 
  new System.Net.NetworkCredential(fromAddress, sifre);
  client.Host = "smtp.gmail.com";
  client.Port = 1772;
  client.EnableSsl = false;
  MailAddress from = new MailAddress(fromAddress, fromName);
  MailAddress to = new MailAddress(toMail, toNme);
  MailMessage message = new MailMessage(from, to);

  message.Subject = msgSubject;
  message.Body = msgBody;

  client.Send(message);
  Response.Redirect("iletisim.aspx");
 }
 catch (Exception ex)
 {
 }
}

和WEB.CONFIG

<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="user@gmail.com">
        <network host="smtp.gmail.com" port="1772" defaultCredentials="false"
            userName="user" password="userpassword"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

我在我的网站上的联系人,我填写了一些文本框,然后点击发送按钮。后来我打开user@gmail.com帐户,但我没有收到联系信息邮件..我在哪里弄错了?

1 个答案:

答案 0 :(得分:1)

尝试使用此代码段发送简单的电子邮件:

    var smtpClient = new SmtpClient("smtp.gmail.com", 587)
    {
        Credentials = new NetworkCredential(
            "yourusername@gmail.com", 
            "yourpassword"
        ),
        EnableSsl = true
    };
    smtpClient.Send("from@gmail.com", "to@gmail.com", "subject", "body");

假设,yourusername@gmail.comyourpassword是您在谷歌上的用户名和密码。

与您的代码的不同之处在于端口587(而不是1772)并使用SSLEnableSsl = true)。