使用asp.net发送电子邮件

时间:2012-11-08 04:28:30

标签: asp.net email smtp

我想使用asp.net发送电子邮件,我已经配置了我的SMTP服务并添加了127.0.0.1作为中继。我曾经在家里申请成功,但我在办公室安装了它无法发送电子邮件的原因?

这是C#代码

MailMessage objemail = new MailMessage();
objemail.To.Add(new MailAddress("apthodisiac@gmail.com"));
objemail.From = new MailAddress(Request.Form["inputEmail"].ToString());
objemail.Subject = Request.Form["inputSubject"].ToString();
objemail.Body = "Dari: " + Request.Form["inputName"].ToString() + "\n\n" +
                "Phone: " + Request.Form["inputPhone"].ToString() + "\n\n" +
                Request.Form["inputMsg"].ToString();
objemail.IsBodyHtml = true;
objemail.Priority = MailPriority.Normal;

SmtpClient objSmtpClient = new SmtpClient();
objSmtpClient.Send(objemail);

这是web.config配置

<system.net>
<mailSettings>
  <smtp deliveryMethod="Network">
    <network host="127.0.0.1" port="25" userName="yyyy" password="xxxxx" />
  </smtp>
</mailSettings>
</system.net>

我的办公室使用代理,是我无法发送电子邮件的问题?请建议

2 个答案:

答案 0 :(得分:0)

可能端口号错误尝试使用端口号"587",如下所示:

private void SendEmail(string from, string to, string subject, string body)
    {
      MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));

      mail.Subject = subject;
      mail.Body = body; 

      SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
      smtpMail.Port = 587;
      smtpMail.EnableSsl = true;
      smtpMail.Credentials = new NetworkCredential("xxx@gmail.com", "xxx");
      // and then send the mail
      smtpMail.Send(mail);
    }

答案 1 :(得分:-1)

你可以试试这个:

objSmtpClient.EnableSsl = true