-1

我正在使用 MVC3,需要向用户发送电子邮件。我不想使用 gmail 服务器。但是,我确实想使用服务器 10.1.70.100。我不明白我做错了什么。这是我的代码:

                var fromAddress = new MailAddress("sum1@abc.com", "From Name");                    var toAddress = new MailAddress(EmailID, "To Name");
                const string fromPassword = "";//To be Filled
                const string subject = "Verification Mail";

                string body = "You have successfully registered yourself. Please Enter your Verification code " + code.ActivatedCode;
                var smtp = new SmtpClient
                {
                    Host = "10.1.70.100",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new NetworkCredential(),

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

有人可以建议一种我不必提供凭据的方法吗?

4

2 回答 2

2

我更喜欢通过以下方式执行此操作:

网络配置:

   <system.net>
       <mailSettings>
           <smtp from="Description">
               <network host="your.smtpserver" password="" userName="" />
           </smtp>
      </mailSettings>
  </system.net>

你的代码:

      var smtpClient = new SmtpClient();
      smtpClient.Send(mail);
于 2012-04-23T12:33:19.853 回答
1

对于我们的 OSS 项目,我们使用这个小助手。希望能帮助到你。

public void SendEmail(string address, string subject, string message)
    {
        string email = ConfigurationManager.AppSettings.Get("email");
        string password = ConfigurationManager.AppSettings.Get("password");
        string client = ConfigurationManager.AppSettings.Get("client");
        string port = ConfigurationManager.AppSettings.Get("port");

        NetworkCredential loginInfo = new NetworkCredential(email, password);
        MailMessage msg = new MailMessage();
        SmtpClient smtpClient = new SmtpClient(client, int.Parse(port));

        msg.From = new MailAddress(email);
        msg.To.Add(new MailAddress(address));
        msg.Subject = subject;
        msg.Body = message;
        msg.IsBodyHtml = true;

        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = loginInfo;
        smtpClient.Send(msg);
    }
于 2012-04-23T12:30:43.403 回答