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

send1() public method

public send1 ( ) : string
return string
        public string send1()
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("[email protected]");
                mail.IsBodyHtml = true;
                mail.To.Add(this.destAddress);
                mail.Subject = "A new Poll has been assigned to you";
                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
ファイル: PollController.cs プロジェクト: Marknel/dbPOLL
        public ActionResult AssignPollCreator(int pollid, int[] selectedObjects, String pollname)
        {
            if (Session["uid"] == null || Session["uid"].ToString().Equals(""))
            {
                return RedirectToAction("Index", "Home");
            }
            if ((int)Session["user_type"] < User_Type.POLL_CREATOR)
            {
                return RedirectToAction("Invalid", "Home");
            }

            String errorString = "";

            new pollModel().assignPoll(pollid, selectedObjects);

            Assign_PollMasters pollMasters = new Assign_PollMasters();

            pollMasters.assigned = new userModel().displayAssignedUsers(pollid, User_Type.POLL_CREATOR);
            pollMasters.unassigned = new userModel().displayUnassignedUsers(pollid, User_Type.POLL_CREATOR);

                foreach (int id in selectedObjects)
                {
                    userModel u = new userModel();
                    u = u.getUser(id);
                    EmailController mail = new EmailController(pollname, u.username);

                    string mailSuccess = mail.send1();
                    if (!mailSuccess.Equals("Email sent successfully"))
                    {
                        errorString += u.username + "\n";
                        //throw new Exception(mailSuccess);
                    }
                }

            if(errorString.Length != 0)
                ViewData["emailError"] = "Could not send email to following Users: \n" + errorString;

            ViewData["pollid"] = pollid;
            ViewData["pollname"] = pollname;
            return View(pollMasters);
        }