Exemplo n.º 1
0
        /* Main email function/controller.
         * Most of the following code was superficially tested (interface flow only) because OAuth wasn't implemented (it requires additional website integration, which is outside the scope of this project).
         * The provided email credentials will act as sender and send a copy of the email statement to the receiver, which is the email specified in the account statement. This prevents the need to bake in plaintext account details, which is a major security issue.
         * Mailkit was not used because it requires OAuth for Gmail. Sendgrid could be a viable alternative (commercial email API) if a project website with separate domain was set up. */
        private void viewEmailStatement(User user, Account account)
        {
            string header = "Email bank statement";

            string[] auth      = new string[2];
            string   _origin   = "";
            string   _password = "";

            try
            {
                auth      = loopEmail(account, header); // Looping form to obtain email credentials from user. Used for sending.
                _origin   = auth[0];
                _password = auth[1];
            }
            catch (Exception)
            { // Catch null return
                viewMainMenu(user);
            }
            string recipient = account.FirstName + " " + account.LastName;
            string subject   = "SimpleBankingApp - Bank Statement for " + account.AccountNumber;
            string body      = account.emailOutput(fe);

            string[] outgoingServerList =
            {
                "smtp.gmail.com", // Gmail smtp server
                "smtp.live.com",  // Hotmail smtp server
                "smtp-mail.outlook.com",
            };
            string smtp     = "";
            int    smtpType = validator.validateEmailType(_origin);

            switch (smtpType)
            {
            case 0: smtp = outgoingServerList[0]; break;

            case 1: smtp = outgoingServerList[1]; break;

            case 2: smtp = outgoingServerList[2]; break;
            }
            ;
            try
            {
                MailMessage email = new MailMessage(_origin, account.Email)
                {
                    From       = new MailAddress(account.Email),
                    Subject    = subject,
                    Body       = body,
                    IsBodyHtml = false,
                };
                var client = new SmtpClient(smtp)
                {
                    Port        = 587, // General port for TLS email connections
                    Credentials = new NetworkCredential(_origin, _password),
                    EnableSsl   = true,
                };
                client.Send(email);
                display.interfaceHeader(header);
                display.interfaceMessage("success", "Please check the inbox of " + account.Email + "for a copy of the statement.");
            }
            catch (System.Net.Mail.SmtpException snmse)
            {
                fe.log(snmse.ToString(), snmse.StackTrace);
                display.interfaceHeader(header);
                display.interfaceMessage("error", "Failed to authenticate using your credentials. You may have typed in a wrong password or need an app-specific password to continue. For Gmail users, please check this answer: https://stackoverflow.com/a/26709761");
            }
            catch (Exception e)
            {
                fe.log(e.ToString(), e.StackTrace);
                try
                {
                    if (smtpType == 2)
                    { // Alternative method for Outlook
                        MailMessage email = new MailMessage(_origin, account.Email)
                        {
                            From       = new MailAddress(account.Email),
                            Subject    = subject,
                            Body       = body,
                            IsBodyHtml = false,
                        };
                        smtp = outgoingServerList[3]; // Switch to smtp.office365.com
                        var client = new SmtpClient(smtp)
                        {
                            Port        = 587,
                            Credentials = new NetworkCredential(_origin, _password),
                            EnableSsl   = true,
                        };
                        client.Send(email);
                        display.interfaceHeader(header);
                        display.interfaceMessage("success", "Please check the inbox of " + account.Email + "for a copy of the statement.");
                    }
                    else
                    {
                        display.interfaceHeader(header);
                        display.interfaceMessage("error", "An error occurred while emailing. Refer to the error log for more details.");
                    }
                }
                catch (System.Net.Mail.SmtpException snmse)
                {
                    fe.log(snmse.ToString(), snmse.StackTrace);
                    display.interfaceHeader(header);
                    display.interfaceMessage("error", "Failed to authenticate using your provided credentials. You may have typed in a wrong password or need an app-specific password to continue. For Gmail users, please check this answer: https://stackoverflow.com/a/26709761");
                }
                catch (Exception f)
                {
                    fe.log(f.ToString(), f.StackTrace);
                    display.interfaceHeader(header);
                    display.interfaceMessage("error", "An error occurred while emailing. Refer to the error log for more details.");
                }
            }
        }