Exemplo n.º 1
0
        /// <summary>
        /// Sends email
        /// </summary>
        /// <param name="Subject">Mail Subject</param>
        /// <param name="Body">Message Body</param>
        public void SendEmail(string Subject, string Body)
        {
            try
            {
                using (SmtpClient smtp = new SmtpClient(_host,_port))
                {
                    // Create message
                    MailMessage message = new MailMessage(_smtpUserName,
                        _destinationAddress, Subject, Body);

                    // Authenticate to server
                    smtp.Authenticate = true;
                    smtp.Username = _smtpUserName;
                    smtp.Password = _smtpPassword;

                    // Send message
                    smtp.Send(message);
                }
            }
            catch (SmtpException e)
            {
                // Exception handling here
                Console.DEBUG_ACTIVITY(e.Message);
                Console.DEBUG_ACTIVITY("Error Code: " + e.ErrorCode.ToString());

                if (AlarmByZones.AlarmByZones.SdCardEventLogger.IsSDCardAvailable() && AlarmByZones.Alarm.ConfigDefault.Data.STORE_EXCEPTION)
                {
                    AlarmByZones.AlarmByZones.SdCardEventLogger.saveFile(DateTime.Now.ToString("d_MMM_yyyy--HH_mm_ss") + ".log",
                        "Exception trying to send an email.\nErrorCode: " + e.ErrorCode.ToString()+
                        "\nException Message: " + e.Message, "Exception");
                }
            }
        }
Exemplo n.º 2
0
        public static void Main()
        {
            MailMessage message = new MailMessage();
            // Set sender name and address
            message.From = new MailAddress("*****@*****.**", "Foo Bar");

            // Set recipient address
            message.To.Add(new MailAddress("*****@*****.**", "John Doe"));
            message.Cc.Add(new MailAddress("*****@*****.**", "The Boss"));

            message.Subject = "Hello World";
            message.Body = "Good news,<br />from now on you can send e-mails from <b>.NET Micro Framework</b>.";
            // Format body as HTML
            message.IsBodyHtml = true;

            // Create new attachment and define it's name
            Attachment attachment = new Attachment("Snowflake.gif");
            attachment.ContentType = "image/gif";
            attachment.TransferEncoding = TransferEncoding.Base64;
            // Attachment content
            attachment.Content = Base64.Encode(Resources.GetBytes(
                                               Resources.BinaryResources.Snowflake_gif),
                                               true);

            // Add attachment to message
            message.Attachments.Add(attachment);

            // Create new SMTP instance
            SmtpClient smtp = new SmtpClient("smtp.contoso.com", 25);
            try
            {
                // Authenicate to server
                smtp.Authenticate = true;
                smtp.Username = "******";
                smtp.Password = "******";

                // Send message
                smtp.Send(message);
            }
            catch (SmtpException e)
            {
                // Exception handling here
                Debug.Print(e.Message);
                Debug.Print("Error Code: " + e.ErrorCode.ToString());
            }
            finally
            {
                smtp.Dispose();
            }
        }