DBPOLLDemo.Controllers.EmailController.send C# (CSharp) Method

send() public method

public send ( ) : string
return string
        public string send()
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("[email protected]");
                mail.IsBodyHtml = true;
                mail.To.Add(this.destAddress);
                mail.Subject = "Your new account details";
                mail.Body = this.body;

                //smtp.Host = "mailhub.itee.uq.edu.au";
                //smtp.Port = 25;

                SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "csse3004");
                smtp.EnableSsl = true;

                smtp.Send(mail);
            }
            catch (Exception e)
            {
                return "An error occured while sending the email: " + e.Message;
            }
            return "Email sent successfully";
        }

Usage Example

コード例 #1
0
ファイル: UserController.cs プロジェクト: Marknel/dbPOLL
        public ActionResult RegisterUser(string email, string name, int user_type)
        {
            // Basic check to see if the user is Authenticated.
            if (Session["uid"] == null || Session["uid"].ToString().Equals(""))
            {
                return RedirectToAction("Index", "Home");
            }
            if ((int)Session["user_type"] < User_Type.POLL_MASTER)
            {
                return RedirectToAction("Invalid", "Home");
            }
            bool errorspresent = false;
            // VALIDATE FORM DATA!
            if (name == null || name == "")
            {
                ViewData["nameError"] = "Above field must contain a name!";
                errorspresent = true;
            }
            else if (name.Length > 64)
            {
                ViewData["nameError"] = "Name is too long, maximum length allowed is 64 characters";
                errorspresent = true;
            }
            //if (email == null || System.Text.RegularExpressions.Regex.IsMatch(email, @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=
            //  [0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"))
            if (email == null || !Regex.IsMatch(email, @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", RegexOptions.IgnoreCase))
            {
                ViewData["emailError"] = "Above field must contain a valid email address!";
                errorspresent = true;
            }
            else if (email.Length > 64)
            {
                ViewData["emailError"] = "Email address is too long, maximum length allowed is 64 characters";
                errorspresent = true;
            }
            if (errorspresent)
            {
                buildSelectList();
                return View();
            }

            userModel user = new userModel();

            // Get the ID for a new user
            int UserID = user.getNewID();

            string password = user.Password_Generator();
            DateTime expiry_Date = DateTime.Now.AddYears(10);

            // Create the user
            if (!user.createUser(UserID, user_type, password, name, email, (int)Session["uid"]))
            {
                ViewData["Message"] = "A user account with this email address already exists";
                buildSelectList();
                return View();
            }

            // Send Email to new user
            EmailController mail = new EmailController(email, password, email);

            string mailSuccess = mail.send();
            if (!mailSuccess.Equals("Email sent successfully"))
            {
                throw new Exception(mailSuccess);
            }

            return RedirectToAction("RegisterUserSuccess", "User");
        }
All Usage Examples Of DBPOLLDemo.Controllers.EmailController::send