예제 #1
0
        public void GetEmail(string sServer, string sUserName, string sPassword, bool bSSLConnection, out int lMailCount, out string sEmail)
        {
            Pop3MailClient  oClient = new Pop3MailClient(sServer, 995, true, sUserName, sPassword);
            string          sUIDL   = "";
            List <EmailUid> oList;

            oClient.Connect();
            oClient.GetUniqueEmailIdList(out oList);
            lMailCount = oList.Count;
            sEmail     = "";
            if (oList.Count > 0)
            {
                oClient.GetRawEmail(lMailCount - mcount, out sEmail);
            }

            oClient.Disconnect();
        }
예제 #2
0
파일: Mail.cs 프로젝트: edrockfsd/sigmeh
        public static void Send()
        {
            Sent    += new MailHandler(OnSent);
            onError += new ErrorHandler(OnError);


            try
            {
                Body = Body.Replace(@"\n", "\n").Replace(@"\r", "\r").Replace(@"\t", "\t");

                //Preparando mensagem de email.
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(
                    ConfigurationManager.AppSettings["EmailAdmin"],
                    ConfigurationManager.AppSettings["EmailNameAdmin"],
                    System.Text.Encoding.UTF8);
                msg.ReplyTo = new MailAddress(
                    ConfigurationManager.AppSettings["EmailAdmin"],
                    ConfigurationManager.AppSettings["EmailNameAdmin"],
                    System.Text.Encoding.UTF8);
                msg.To.Add(EmailTo);

                msg.Subject         = Subject;
                msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.Body            = Body;
                msg.BodyEncoding    = System.Text.Encoding.UTF8;
                msg.IsBodyHtml      = true;

                if (attachLog && !logPath.Equals(string.Empty))
                {
                    msg.Attachments.Add(new Attachment(logPath));
                }

                //Criando credenciais para envio da mensagem.
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential(
                    ConfigurationManager.AppSettings["EmailAdmin"],
                    ConfigurationManager.AppSettings["EmailPasswordAdmin"]);

                //Google funciona em 587 ou 465.
                client.Port = int.Parse(ConfigurationManager.AppSettings["SmtpPortAdmin"]);

                //client.Host = "smtp.googlemail.com"; //client.Host = "smtp.gmail.com"; //Preferencial: "smtp.googlemail.com"
                client.Host = ConfigurationManager.AppSettings["SmtpAdmin"];

                //         if (client.Port == 587)
                client.EnableSsl = false;

                client.Timeout = 30000; //Tempo para tentativa de envio de email

                Pop3MailClient pop3client = null;

                if (!client.EnableSsl)
                {
                    //Porta pop3 tcp = 110 / com SSL (gmail) = 995
                    pop3client = (ConfigurationManager.AppSettings["Pop3PortAdmin"] == "995") ?
                                 new Pop3MailClient(
                        ConfigurationManager.AppSettings["Pop3Admin"],
                        int.Parse(ConfigurationManager.AppSettings["Pop3PortAdmin"]),
                        true,
                        ConfigurationManager.AppSettings["EmailAdmin"],
                        ConfigurationManager.AppSettings["EmailPasswordAdmin"])
                    :
                                 new Pop3MailClient(
                        ConfigurationManager.AppSettings["Pop3Admin"],
                        int.Parse(ConfigurationManager.AppSettings["Pop3PortAdmin"]),
                        false,
                        ConfigurationManager.AppSettings["EmailAdmin"],
                        ConfigurationManager.AppSettings["EmailPasswordAdmin"]);
                    pop3client.IsAutoReconnect = true;

                    pop3client.ReadTimeout = 60000; //60 segundos para o servidor pop responder
                }
                //Tenta enviar três vezes, questões de autenticação ( em alguns servidores não se consegue enviar o email na primeira tentativa )
                bool enviado = false;
                for (int i = 0; i < 4; i++)
                {
                    try
                    {
                        if (!client.EnableSsl)
                        {
                            pop3client.Connect();
                            pop3client.Disconnect();
                        }
                        client.Send(msg);

                        enviado = true;
                        break; // Caso tenha sido enviado corretamente apenas sai do loop.
                    }
                    catch
                    {
                        //Caso não funcione com a conta completa pega-se apenas o nome do usuário para as credenciais.
                        // tenta duas vezes com a conta completa, ex: [email protected], e duas vezes somente com o nome de usuário, ex: digiquest.
                        if (i == 2)
                        {
                            pop3client = (ConfigurationManager.AppSettings["Pop3PortAdmin"] == "995") ?
                                         new Pop3MailClient(
                                ConfigurationManager.AppSettings["Pop3Admin"],
                                int.Parse(ConfigurationManager.AppSettings["Pop3PortAdmin"]),
                                true,
                                ConfigurationManager.AppSettings["EmailAdmin"].Split('@')[0],
                                ConfigurationManager.AppSettings["EmailPasswordAdmin"])
                            :
                                         new Pop3MailClient(
                                ConfigurationManager.AppSettings["Pop3Admin"],
                                int.Parse(ConfigurationManager.AppSettings["Pop3PortAdmin"]),
                                false,
                                ConfigurationManager.AppSettings["EmailAdmin"].Split('@')[0],
                                ConfigurationManager.AppSettings["EmailPasswordAdmin"]);

                            client.Credentials = new System.Net.NetworkCredential(
                                ConfigurationManager.AppSettings["EmailAdmin"].Split('@')[0],
                                ConfigurationManager.AppSettings["EmailPasswordAdmin"]);
                        }
                    }
                }
                if (enviado)
                {
                    Sent(Status.Success);
                }
                else
                {
                    Sent(Status.Error);
                }
            }
            catch (Exception ex)
            {
                //Log.Trace(ex);
            }
        }