使用C#2.0发送电子邮件

时间:2015-04-18 20:39:45

标签: c# asp.net email

我正在尝试发送电子邮件,但我使用的是asp.net c#2.0 我的服务器仅支持2.0版本。是否可以在2.0中发送电子邮件?

protected void btnSubmit_Click(object sender, EventArgs e)
 {
  try
  {

   MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
   SmtpClient emailClient = new SmtpClient(smtp.gmail.com);
   emailClient.Send(message);
   litStatus.Text = "Message Sent";

  }
  catch (Exception ex)
  {
   litStatus.Text=ex.ToString();
  }
 }

4 个答案:

答案 0 :(得分:1)

我刚试过这个并且效果很好:

     public static void Send()
    {
        SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);

        MailMessage MyMailMessage = new MailMessage("sender@gmail.com", "receiver@ymail.com",
            "subject", "message");
        MyMailMessage.IsBodyHtml = false;
        NetworkCredential mailAuthentication = new NetworkCredential("your_email@gmail.com", "password");

        mailClient.EnableSsl = true;
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = mailAuthentication;
        mailClient.Send(MyMailMessage);
    }

我也来过这里并开启了访问安全性较低的应用程序:

https://www.google.com/settings/security/lesssecureapps

答案 1 :(得分:0)

根据MSDN documentation在.NET 2.0中引入了SmtpClient,因此您应该可以使用代码在2.0中发送电子邮件

答案 2 :(得分:0)

您需要提供凭据:

NetworkCredential _Credentials = 
new NetworkCredential("username", "password"); 
...

SmtpClient emailClient = new SmtpClient("smtp.gmail.com", 587); // I don't know gmails port it might be 465
smtpClient.UseDefaultCredentials = false;
emailClient.Credentials = _Credentials;

EDIT。

根据您粘贴在评论中的例外,请尝试添加以下行:

emailClient.EnableSsl = True;

答案 3 :(得分:-1)

尝试System.Net.Mail

并选择2.0版本。