/// <summary>
        ///     SendMail function utilized by llEMail
        /// </summary>
        /// <param name="objectID"></param>
        /// <param name="address"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="scene">Can be null</param>
        public void SendEmail (UUID objectID, string address, string subject, string body, IScene scene)
        {
            //Check if address is empty
            if (address == string.Empty)
                return;

            bool isEMailStrictMatch = Utilities.IsValidEmail (address);
            if (!isEMailStrictMatch) {
                MainConsole.Instance.Error ("[Email]: REGEX Problem in EMail Address: " + address);
                return;
            }
            // Check if subject + body = max size (4096) Byte
            if ((subject.Length + body.Length) > m_MaxEmailSize) {
                MainConsole.Instance.Error ("[Email]: subject + body larger than limit of " + m_MaxEmailSize + " bytes");
                return;
            }

            string LastObjectName = string.Empty;
            string LastObjectPosition = string.Empty;
            string LastObjectRegionName = string.Empty;

            if (scene != null)
                resolveNamePositionRegionName (objectID, out LastObjectName, out LastObjectPosition,
                                              out LastObjectRegionName, scene);

            if (!address.EndsWith (m_InterObjectHostname, StringComparison.Ordinal)) {
                bool didError = false;
                if (!m_localOnly) {
                    // regular email, send it out
                    Thread threadSendMail;
                    threadSendMail = new Thread (delegate () {
                        try {
                            // Create EmailMessage
                            string fromEmailAddress;

                            if (scene != null && objectID != UUID.Zero)
                                fromEmailAddress = objectID + "@" + m_HostName;
                            else
                                fromEmailAddress = "no-reply@" + m_HostName;

                            var fromAddress = new MailAddress (fromEmailAddress);
                            var toAddress = new MailAddress (address);

                            if (scene != null) {
                                // If Object Null Don't Include Object Info Headers (Offline IMs)
                                if (objectID != UUID.Zero)
                                    body = body + "\nObject-Name: " + LastObjectName +
                                    "\nRegion: " + LastObjectRegionName + "\nLocal-Position: " +
                                    LastObjectPosition + "\n\n";
                            }

                            //Config SMTP Server
                            var smtpServer = new SmtpClient ();
                            smtpServer.Host = SMTP_SERVER_HOSTNAME;
                            smtpServer.Port = SMTP_SERVER_PORT;
                            smtpServer.EnableSsl = (SMTP_SERVER_PORT == 587);
                            smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
                            smtpServer.UseDefaultCredentials = false;
                            smtpServer.Credentials = new NetworkCredential (SMTP_SERVER_LOGIN, SMTP_SERVER_PASSWORD);
                            smtpServer.Timeout = 15000;

                            // Beware !! This effectively ignores the ssl validation and assumes that all is correct 
                            // For Mono, requires importation of the Google smtpd certificate (see SMTPEmail.ini)
                            // Possibly not needed for Windows
                            //ServicePointManager.ServerCertificateValidationCallback = 
                            //    delegate(object sim, X509Certificate certificate, X509Chain chain SslPolicyErrors sslPolicyErrors)
                            //{ return true; };

                            // if ((!SMTP_SERVER_MONO_CERT) && (Utilities.IsLinuxOs))
                            ServicePointManager.ServerCertificateValidationCallback = delegate {
                                return true;
                            };

                            // create the message
                            var emailMessage = new MailMessage (fromAddress, toAddress);
                            emailMessage.Subject = subject;
                            emailMessage.Body = body;

                            // sample for adding attachments is needed sometime :)
                            //if File(Exist(fullFileName))
                            //{
                            //    var mailAttactment = new Attachment(fullFileName);
                            //    emailMessage.Attachments.Add(mailAttactment);
                            //}

                            // send the message
                            try {
                                smtpServer.Send (emailMessage);
                            } catch (SmtpException ex) {
                                SmtpStatusCode status = ex.StatusCode;
                                if (status == SmtpStatusCode.Ok)
                                    MainConsole.Instance.Info ("[Email]: EMail sent to: " + address + " from object: " +
                                    fromEmailAddress);
                                else
                                    MainConsole.Instance.Info ("[Email]: EMail error sending to: " + address + " from object: " +
                                    fromEmailAddress + " status: " + ex.Message);
                            }
                            smtpServer.Dispose ();
                        } catch (Exception e) {
                            MainConsole.Instance.Error ("[Email]: DefaultEmailModule Exception: " + e.Message);
                            didError = true;
                        }
                    });

                    threadSendMail.IsBackground = true;
                    threadSendMail.Start ();

                }
                if (((didError) || (m_localOnly)) && (scene != null)) {
                    // Notify Owner
                    ISceneChildEntity part = findPrim (objectID, out LastObjectRegionName, scene);
                    if (part != null) {
                        IScenePresence sp = scene.GetScenePresence (part.OwnerID);
                        if ((sp != null) && (!sp.IsChildAgent)) {
                            sp.ControllingClient.SendAlertMessage ("llEmail: email module not configured for outgoing emails");
                        }
                    }
                }
            } else {
                // inter object email, keep it in the family
                string guid = address.Substring (0, address.IndexOf ("@", StringComparison.Ordinal));
                UUID toID = new UUID (guid);

                if (IsLocal (toID, scene)) {
                    // object in this region
                    InsertEmail (toID, new Email {
                        time =((int)((DateTime.UtcNow - new DateTime (1970, 1, 1, 0, 0, 0)).TotalSeconds)).ToString (CultureInfo.InvariantCulture),
                        subject = subject,
                        sender = objectID + "@" + m_InterObjectHostname,
                        message = "Object-Name: " + LastObjectName + "\nRegion: " + LastObjectRegionName + "\nLocal-Position: " +
                            LastObjectPosition + "\n\n" + body,
                        toPrimID = toID
                    });
                } else {
                    // object on another region

                    Email email = new Email {
                        time =((int)((DateTime.UtcNow - new DateTime (1970, 1, 1, 0, 0, 0)).TotalSeconds)).ToString (CultureInfo.InvariantCulture),
                        subject = subject,
                        sender = objectID + "@" + m_InterObjectHostname,
                        message = body,
                        toPrimID = toID
                    };
                    IEmailConnector conn = Framework.Utilities.DataManager.RequestPlugin<IEmailConnector> ();
                    conn.InsertEmail (email);
                }
            }
        }
        public void InsertEmail (UUID to, Email email)
        {
            // It's tempting to create the queue here.  Don't; objects which have
            // not yet called GetNextEmail should have no queue, and emails to them
            // should be silently dropped.

            lock (m_MailQueues) {
                if (m_MailQueues.ContainsKey (to)) {
                    if (m_MailQueues [to].Count >= m_MaxQueueSize) {
                        // fail silently
                        return;
                    }

                    lock (m_MailQueues [to]) {
                        m_MailQueues [to].Add (email);
                    }
                }
            }
        }
        public void InsertEmail (Email email)
        {
            if (m_doRemoteOnly) {
                DoRemote (email);
                return;
            }

            GenericUtils.AddGeneric (email.toPrimID, "Emails", UUID.Random ().ToString (),
                                    email.ToOSD (), GD);
        }
        public void InsertEmail(Email email)
        {
            object remoteValue = DoRemote(email);
            if (remoteValue != null || m_doRemoteOnly)
                return;

            GenericUtils.AddGeneric(email.toPrimID, "Emails", UUID.Random().ToString(),
                                    email.ToOSD(), GD);
        }
Example #5
0
        /// <summary>
        ///     SendMail function utilized by llEMail
        /// </summary>
        /// <param name="objectID"></param>
        /// <param name="address"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="scene">Can be null</param>
        public void SendEmail(UUID objectID, string address, string subject, string body, IScene scene)
        {
            //Check if address is empty
            if (address == string.Empty)
                return;

            //FIXED:Check the email is correct form in REGEX
            const string EMailpatternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
                                              + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
                                              + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
                                              + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
                                              + @"[a-zA-Z]{2,}))$";
            Regex EMailreStrict = new Regex(EMailpatternStrict);
            bool isEMailStrictMatch = EMailreStrict.IsMatch(address);
            if (!isEMailStrictMatch)
            {
                MainConsole.Instance.Error("[EMAIL] REGEX Problem in EMail Address: " + address);
                return;
            }
            //FIXME:Check if subject + body = 4096 Byte
            if ((subject.Length + body.Length) > m_MaxEmailSize)
            {
                MainConsole.Instance.Error("[EMAIL] subject + body larger than limit of " + m_MaxEmailSize + " bytes");
                return;
            }

            string LastObjectName = string.Empty;
            string LastObjectPosition = string.Empty;
            string LastObjectRegionName = string.Empty;

            if (scene != null)
                resolveNamePositionRegionName(objectID, out LastObjectName, out LastObjectPosition,
                                              out LastObjectRegionName, scene);

            if (!address.EndsWith(m_InterObjectHostname))
            {
                bool didError = false;
                if (!m_localOnly)
                {
                    // regular email, send it out
                    try
                    {
                        //Creation EmailMessage

                        string fromEmailAddress;

                        if (scene != null && objectID != UUID.Zero)
                            fromEmailAddress = objectID.ToString() + "@" + m_HostName;
                        else
                            fromEmailAddress = "no-reply@" + m_HostName;

                        EmailMessage emailMessage = new EmailMessage
                                                        {
                                                            FromAddress =
                                                                new EmailAddress(fromEmailAddress),
                                                            Subject = subject
                                                        };

                        //To - Only One
                        emailMessage.AddToAddress(new EmailAddress(address));
                        //Text
                        emailMessage.BodyText = body;
                        if (scene != null)
                        {
                            // If Object Null Dont Include Object Info Headers (Offline IMs)
                            if(objectID != UUID.Zero)
                                emailMessage.BodyText = "Object-Name: " + LastObjectName +
                                                        "\nRegion: " + LastObjectRegionName + "\nLocal-Position: " +
                                                        LastObjectPosition + "\n\n";

                            emailMessage.BodyText += emailMessage.BodyText;
                        }

                        //Config SMTP Server
                        //Set SMTP SERVER config
                        SmtpServer smtpServer = new SmtpServer(SMTP_SERVER_HOSTNAME, SMTP_SERVER_PORT);
                        // Add authentication only when requested
                        if (SMTP_SERVER_LOGIN != String.Empty && SMTP_SERVER_PASSWORD != String.Empty)
                            smtpServer.SmtpAuthToken = new SmtpAuthToken(SMTP_SERVER_LOGIN, SMTP_SERVER_PASSWORD);
                        //Add timeout of 15 seconds
                        smtpServer.ServerTimeout = 15000;
                        //Send Email Message
                        didError = !emailMessage.Send(smtpServer);

                        //Log
                        if (!didError)
                            MainConsole.Instance.Info("[EMAIL] EMail sent to: " + address + " from object: " +
                                                      fromEmailAddress);
                    }
                    catch (Exception e)
                    {
                        MainConsole.Instance.Error("[EMAIL] DefaultEmailModule Exception: " + e.Message);
                        didError = true;
                    }
                }
                if (((didError) || (m_localOnly)) && (scene != null))
                {
                    // Notify Owner
                    ISceneChildEntity part = findPrim(objectID, out LastObjectRegionName, scene);
                    if (part != null)
                    {
                        IScenePresence sp = scene.GetScenePresence(part.OwnerID);
                        if ((sp != null) && (!sp.IsChildAgent))
                        {
                            sp.ControllingClient.SendAlertMessage(
                                "llEmail: email module not configured for outgoing emails");
                        }
                    }
                }
            }
            else
            {
                // inter object email, keep it in the family
                string guid = address.Substring(0, address.IndexOf("@", StringComparison.Ordinal));
                UUID toID = new UUID(guid);

                if (IsLocal(toID, scene))
                {
                    // object in this region
                    InsertEmail(toID, new Email
                                          {
                                              time =
                                                  ((int)
                                                   ((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds))
                                                  .
                                                  ToString(CultureInfo.InvariantCulture),
                                              subject = subject,
                                              sender = objectID.ToString() + "@" + m_InterObjectHostname,
                                              message = "Object-Name: " + LastObjectName +
                                                        "\nRegion: " + LastObjectRegionName + "\nLocal-Position: " +
                                                        LastObjectPosition + "\n\n" + body,
                                              toPrimID = toID
                                          });
                }
                else
                {
                    // object on another region

                    Email email = new Email
                                      {
                                          time =
                                              ((int)
                                               ((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds)).
                                              ToString(CultureInfo.InvariantCulture),
                                          subject = subject,
                                          sender = objectID.ToString() + "@" + m_InterObjectHostname,
                                          message = body,
                                          toPrimID = toID
                                      };
                    IEmailConnector conn = Framework.Utilities.DataManager.RequestPlugin<IEmailConnector>();
                    conn.InsertEmail(email);
                }
            }
        }