Beispiel #1
0
            //==== GMail =======

            #region === Email / Gmail ===

            // Send Gmail (From Gmail Account) - Can include list of file paths to add as attachments

            public bool Gmail_Coder(string Subject = "Namtrak Says Sup AGAIN?", string Body = "Mail with attachment? Maybe so...", List <string> Attachments = null, object To = null)
            {
                _AHK ahk = new _AHK();

                _Database.SQLite sqlite = new _Database.SQLite();
                _StatusBar       sb     = new _StatusBar();

                MailMessage mail       = new MailMessage();
                SmtpClient  SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("*****@*****.**");

                // default email address if nothing provided
                if (To == null)
                {
                    mail.To.Add("*****@*****.**");
                }
                else
                {
                    string VarType = To.GetType().ToString();  //determine what kind of variable was passed into function

                    // user assed in 1 email address as string
                    if (VarType == "System.String")
                    {
                        mail.To.Add(To.ToString());
                    }

                    // user passed in list of email addresses to send to
                    else if (VarType == "System.Collections.Generic.List`1[System.String]")
                    {
                        //ahk.MsgBox("String List");
                        List <string> ToList = (List <string>)To;
                        foreach (string add in ToList)
                        {
                            mail.To.Add(add);
                        }
                    }
                    else
                    {
                        ahk.MsgBox("Gmail Coder Function | Unable To Use VarType " + VarType);
                        return(false);
                    }
                }


                mail.Subject = Subject;
                mail.Body    = Body;

                if (Attachments != null && Attachments.Count > 0)
                {
                    System.Net.Mail.Attachment attachment;

                    foreach (string file in Attachments)
                    {
                        attachment = new System.Net.Mail.Attachment(file);
                        mail.Attachments.Add(attachment);
                    }
                }

                string gmailL = ConfigurationManager.AppSettings["GmailLogin"]; // read setting from app.config
                string gmailP = ConfigurationManager.AppSettings["GmailPass"];  // read setting from app.config


                //SmtpServer.EnableSsl = (SmtpServer.Port == 465);
                SmtpServer.Port        = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential(gmailL, gmailP);
                SmtpServer.EnableSsl   = true;

                try
                {
                    SmtpServer.Send(mail);
                    sb.StatusBar("Sent Gmail");
                    return(true);
                }
                catch (Exception ex)
                {
                    ahk.MsgBox(ex.ToString());
                    sb.StatusBar("ERROR SENDING GMAIL");
                    return(false);
                }
            }