0

我正在使用 c# 在 mvc4 中做一个网站。目前我在 localhost 中对此进行了测试。我想将密码发送到注册人的电子邮件。我使用以下代码。

//模型

 public void SendPasswordToMail(string newpwd,string email)
    {
        string mailcontent ="Your password is "+ newpwd;
        string toemail = email.Trim();
        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("paru.mr@gmail.com");
            mail.To.Add(toemail);
            mail.Subject = "Your New Password";
            mail.Body = mailcontent;
            //Attachment attachment = new Attachment(filename);
            //mail.Attachments.Add(attachment);
            SmtpServer.Port = 25;
            SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password");  //is that NetworkCredential
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);

        }
        catch (Exception e)
        {
            throw e;
        }
    }

//控制器

  string newpwd;
    [HttpPost]
    public ActionResult SendPassword(int Id,string email)
    {
        bool IsMember=CheckMember(Id);
        if (IsMember == true)
        {
             newpwd = new Member().RandomPaswordGen();
        }
       .......//here i want to call that model
        return View();
    }

模型是否正确。这个模型的返回类型应该是什么(现在它是无效的,我不知道这个在控制器中是如何调用的)。以及如何获取默认的 NetworkCredential。请帮我

4

1 回答 1

0

Network Credential 是电子邮件发件人的用户名和密码(如公司的默认邮件地址,例如 noreply@abccompany.com)。因此,在这种情况下使用您的 gmail 用户名和密码。您可以使用以下代码发送电子邮件。

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

                         };
        var from = new MailAddress(Email);
        var to = new MailAddress(Email);
        var message = new MailMessage(from, to) { Body = Message, IsBodyHtml = IsBodyHtml };

        message.Body += Environment.NewLine + Footer;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = Subject;
        message.SubjectEncoding = System.Text.Encoding.UTF8;

        var nc = new NetworkCredential("me@gmail.com", "password");
        client.Credentials = nc;

        //send email
        client.Send(message);
于 2013-08-28T05:54:36.937 回答