How to send email from Asp.net Mvc-3?

后端 未结 9 1674
太阳男子
太阳男子 2020-12-23 09:50

How to send a mail through mvc-3 asp.net using c#?

I have to send a forgot password so how can I do this? My code is below.

Model code..

 usi         


        
相关标签:
9条回答
  • 2020-12-23 10:12
        Using Systems.Net.Mail;
    // POST: /Account/Register
    //Here's a simple Mail(MVC4)
    
            public async Task<ActionResult> Register(RegisterViewModel model)
            {
                Mail email= new Mail();
                if (ModelState.IsValid)
                {
                    var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email };
                    var result = await UserManager.CreateAsync(user, model.Password);
                    if (result.Succeeded)
                    {
                        email.to = new MailAddress(model.Email);
                        email.body = "Hello " + model.Firstname + " your account has been created <br/> Username: " + model.UserName + " <br/>Password: " + model.Password.ToString() + " <br/> change it on first loggin";
                        ViewBag.Feed = email.reg();
    
    
                        await SignInAsync(user, isPersistent: false);
    
    
                         return RedirectToAction("Index", "Home");
    
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
    
                // If we got this far, something failed, redisplay form
                return View(model);
            }
    
    
    
    
    //Business Logic(this Is you Email Class)
    
    
    
    
    Using Systems.Net.Mail;
    
    
     public class Mail
    
        {
            public MailAddress to { get; set; }
            public MailAddress from { get; set; }
            public string sub { get; set; }
            public string body { get; set; }
    
    
    
    
            public string reg()
            {
                string feed = "Registration Successful";
                var m = new System.Net.Mail.MailMessage()
                {
                    Subject = "",
                    Body = body,
                    IsBodyHtml = true
                };
                m.From = new MailAddress("Mxolisi@gmail.com  ", "Administrator");
                m.To.Add(to);
                SmtpClient smtp = new SmtpClient
                {
                    Host = "pod51014.outlook.com",
                    //Host = "smtp-mail.outlook.com",
                    Port = 587,
                    Credentials = new System.Net.NetworkCredential("Mxolisi@gmail.com ", " Dut324232"),
                    EnableSsl = true
                };
    
                try
                {
                    smtp.Send(m);
                    // feed = "";
                }
                catch (Exception e)
                {
    
                }
    
                return feed;
    
            }
            public string fogot()
            {
                string feedback = "";
    
                var m = new System.Net.Mail.MailMessage()
                {
                    Subject = "Reset Password PIN",
                    Body = body,
                    IsBodyHtml = true
                };
                m.From = new MailAddress("Mxolisi@gmail.com ", "Administrator");
                m.To.Add(to);
                SmtpClient smtp = new SmtpClient
                {
                    Host = "pod51014.outlook.com",
                    Port = 587,
                    Credentials = new System.Net.NetworkCredential("Mxolisi@gmail.com ", "Dut324232"),
                    EnableSsl = true
                };
    
                try
                {
                    smtp.Send(m);
                    feedback = "Check your email for PIN";
                }
                catch (Exception e)
                {
                    feedback = "Message not sent" + e.Message;
                }
                return feedback;
    
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-23 10:13

    when use smtp for gmail, remember put

    smtpClient.UseDefaultCredentials = false;

    before

    smtpClient.Credentials = loginInfo;
    
    0 讨论(0)
  • 2020-12-23 10:17

    It look like you are trying to send emails through GMail's SMTP service, which this SO question already covers: Sending email in .NET through Gmail

    The only thing that looks missing in your code is that you've set client.UseDefaultCredentials = true, I think you want to set this to false and provide your own credentials. I've never tried using GMail to send through emails, but I'm guessing you'll need to use a GMail account as your credentials in order to authenticate properly.

    0 讨论(0)
  • 2020-12-23 10:20

    Use code from following Link to Send Email in mvc3:

    http://weblogs.asp.net/gunnarpeipman/archive/2010/10/20/asp-net-mvc-3-beta-using-webmail-helper-to-send-e-mail.aspx

    0 讨论(0)
  • 2020-12-23 10:27

    You should turn on the SMTP service in Window 7 :

    • go to Control Panel > Programs
    • click "Turn window features ON or OFF"
    • click Internet Information Service and click OK
    0 讨论(0)
  • 2020-12-23 10:31

    Import the System.Net.Mail namespace.

    The code will look similar to this:

    MailMessage mail = new MailMessage();
    
    SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
    smtpServer.Credentials = new System.Net.NetworkCredential("userName", "password");
    smtpServer.Port = 587; // Gmail works on this port
    
    mail.From = new MailAddress("myemail@gmail.com");
    mail.To.Add("recepient@gmail.com");
    mail.Subject = "Password recovery";
    mail.Body = "Recovering the password";
    
    smtpServer.Send(mail);
    

    P.S. You have a SQL injection vulnerability in the sample code. Use a SqlCommand object with parameters instead of String.Format().

    Using SqlDataReader would be a lot more efficient to check for a record instead of populating a DataSet.

    0 讨论(0)
提交回复
热议问题