示例#1
0
        /////////////////////////////////////////////////////////////////////
        // Example One: Unsecure transmission and plain text password.
        // Message content is plain text.
        // The message is a single part message with no boundaries.
        /////////////////////////////////////////////////////////////////////
        private void ExampleUnsecurePlainText
        (
            string Host,
            string UserName,
            string UserPassword,
            string FromName,
            string FromAddress,
            string ToName,
            string ToAddress,
            string MessageSubject,
            string MessageText
        )
        {
            try
            {
                // create smtp client
                if (ConnectionUnsecurePlain == null)
                {
                    ConnectionUnsecurePlain = new SecureSmtpClient(ConnectMethod.Secure, Host, UserName, UserPassword);
                }

                // create mail message object
                SecureSmtpMessage Message = new SecureSmtpMessage();

                // Set subject
                Message.Subject = MessageSubject;

                // Set mail from address and display name.
                Message.From = new MailAddress(FromAddress, FromName);

                // Add minimum one or more recipients.
                // in addition you can add CC or BCC recipients.
                Message.To.Add(new MailAddress(ToAddress, ToName));

                // Add mail body contents.
                // The plain text is the only content part.
                // Load it to the root part of the message.
                SecureSmtpContent PlainTextContent = new SecureSmtpContent(ContentType.Plain, MessageText);
                Message.RootPart = PlainTextContent;

                // send mail
                ConnectionUnsecurePlain.SendMail(Message);
                MessageBox.Show("Email was successfully sent");
            }

            // catch exceptions
            catch (Exception Exp)
            {
                MessageBox.Show(Exp.Message);
            }
            return;
        }
示例#2
0
        private static void SendEmail(int type, string host, int port, string accountEmail, string password, string subject, string body, string fromAddress, string fromUserName, List <string> toAddress, string refreshToken = null, byte[] file = null, string fileName = null)
        {
            try
            {
                // create one of three possible connection classes
                // the SecureSmtpClient is re-useable. If you send more than one email,
                // you can reuse the class. It is of real benefit for gmail.
                SecureSmtpClient connection = null;
                switch (type)
                {
                case 0:
                    connection = new SecureSmtpClient(host, accountEmail, new SecureSmtpOAuth2(refreshToken));
                    break;

                case 1:
                    connection = new SecureSmtpClient(ConnectMethod.Secure, host, accountEmail, password);
                    break;

                case 2:
                    connection = new SecureSmtpClient(ConnectMethod.Unsecure, host, accountEmail, password);
                    break;
                }

                if (connection == null)
                {
                    throw new Exception("SecureSmtpClient can not NULL");
                }

                connection.PortNo  = port;
                connection.Timeout = 20;

                // create related boundary
                var related = new SecureSmtpMultipart(MultipartType.Related);
                // add html mail body content
                related.AddPart(new SecureSmtpContent(ContentType.Html, body));

                // add inline image attachment.
                // NOTE image id is set to IMAGE001 this id must match the html image id in HtmlView text.
                //related.AddPart(new SecureSmtpAttachment(AttachmentType.Inline, image, DateTime.Now.ToString("yyyyMMddHHmmss"))
                //{
                //    MediaType = "image/jpg"
                //});


                // create alternative boundary
                var alternative = new SecureSmtpMultipart(MultipartType.Alternative);
                //Add plain text mail body contents.
                //alternative.AddPart(new SecureSmtpContent(ContentType.Plain, body));
                alternative.AddPart(related);

                // create mixed multipart boundary
                var mixed = new SecureSmtpMultipart(MultipartType.Mixed);
                mixed.AddPart(alternative);

                if (file != null && file.Length > 0 && !string.IsNullOrEmpty(fileName))
                {
                    // add file attachment to the email.
                    // The recipient of the email will be able to save it as a file.
                    mixed.AddPart(new SecureSmtpAttachment(AttachmentType.Attachment, file, fileName));
                }

                // create mail message object
                var message = new SecureSmtpMessage
                {
                    Subject  = subject,                                                                // Set subject
                    From     = new MailAddress(fromAddress, fromUserName),                             // Set mail from address and display name.
                    RootPart = mixed,                                                                  // create mixed multipart boundary
                    To       = toAddress.Select(address => new MailAddress(address, address)).ToList() // Add minimum one or more recipients.
                };

                // send mail
                connection.SendMail(message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#3
0
        /////////////////////////////////////////////////////////////////////
        // Example Three: Secure transmission and password authentication.
        // This example is specific to GMail.
        // The message is a multi-part message. At the root level it is a
        // mixed part message with two mixed parts: content and file
        // attachment.
        // The content part is divided into two alternate parts: HTML content
        // and plain text content.
        // The HTML part is divided into two related parts: the HTML content
        // and an embedded image. The image is sent as an inline attachment.
        /////////////////////////////////////////////////////////////////////
        private void ExampleGMail
        (
            string Host,
            string UserName,
            string RefreshToken,
            string FromName,
            string FromAddress,
            string ToName,
            string ToAddress,
            string MessageSubject,
            string MessageHtml
        )
        {
            try
            {
                // create smtp client
                if (ConnectionGMail == null)
                {
                    ConnectionGMail = new SecureSmtpClient(Host, UserName, new SecureSmtpOAuth2(RefreshToken));
                }

                // create mail message object
                SecureSmtpMessage Message = new SecureSmtpMessage();

                // Set subject
                Message.Subject = MessageSubject;

                // Set mail from address and display name.
                Message.From = new MailAddress(FromAddress, FromName);

                // Add minimum one or more recipients.
                // in addition you can add CC or BCC recipients.
                Message.To.Add(new MailAddress(ToAddress, ToName));

                // This message is made of two alternative methods for content plus a attachment file.
                // create mixed multipart boundary
                SecureSmtpMultipart Mixed = new SecureSmtpMultipart(MultipartType.Mixed);
                Message.RootPart = Mixed;

                // create alternative boundary
                // there are two alternatives to the content: plain text and HTML.
                // NOTE: The recipient of the message will display the last part it knows how to display.
                // For email program that can handle both HTML and text make sure the HTML part is last.
                SecureSmtpMultipart Alternative = new SecureSmtpMultipart(MultipartType.Alternative);
                Mixed.AddPart(Alternative);

                // Add plain text mail body contents.
                SecureSmtpContent PlainTextContent = new SecureSmtpContent(ContentType.Plain, PlainTextView);
                Alternative.AddPart(PlainTextContent);

                // The HTML alternative has two related parts. The HTML content and an inline image attachement
                // create related boundary
                SecureSmtpMultipart Related = new SecureSmtpMultipart(MultipartType.Related);
                Alternative.AddPart(Related);

                // Add html mail body content.
                SecureSmtpContent HtmlContent = new SecureSmtpContent(ContentType.Html, HtmlView);
                Related.AddPart(HtmlContent);

                // Add inline image attachment.
                // NOTE image id is set to IMAGE001 this id must match the html image id in HtmlView text.
                SecureSmtpAttachment ImageAttachment = new SecureSmtpAttachment(AttachmentType.Inline, "EmailImage.png", "IMAGE001");
                ImageAttachment.MediaType = "image/png";
                Related.AddPart(ImageAttachment);

                // Add file attachment to the email.
                // The recipient of the email will be able to save it as a file.
                SecureSmtpAttachment PdfAttachment = new SecureSmtpAttachment(AttachmentType.Attachment, "rfc2045.pdf");
                Mixed.AddPart(PdfAttachment);

                // send mail
                ConnectionGMail.SendMail(Message);
                MessageBox.Show("Email was successfully sent");
            }

            // catch exceptions
            catch (Exception Exp)
            {
                MessageBox.Show(Exp.Message);
            }
            return;
        }
示例#4
0
        /////////////////////////////////////////////////////////////////////
        // Example Two: Secure transmission and no password authentication.
        // Message content is HTML.
        // The HTML has an embedded image. The image is sent as an inline
        // attachment. This message is a two parts message. It has two related
        // parts, the html part and the inline image attachment part.
        // The two parts are separated by SecureSmtpMultipart boundary.
        /////////////////////////////////////////////////////////////////////
        private void ExampleSecureHtml
        (
            string Host,
            string UserName,
            string UserPassword,
            string FromName,
            string FromAddress,
            string ToName,
            string ToAddress,
            string MessageSubject,
            string MessageHtml
        )
        {
            try
            {
                // create smtp client
                if (ConnectionSecureHtml == null)
                {
                    ConnectionSecureHtml = new SecureSmtpClient(ConnectMethod.Secure, Host, UserName, UserPassword);
                }

                // create mail message object
                SecureSmtpMessage Message = new SecureSmtpMessage();

                // Set subject
                Message.Subject = MessageSubject;

                // Set mail from address and display name.
                Message.From = new MailAddress(FromAddress, FromName);

                // Add minimum one or more recipients.
                // in addition you can add CC or BCC recipients.
                Message.To.Add(new MailAddress(ToAddress, ToName));

                // This message has two related parts, the html part and the inline image attachment part.
                // The two parts are separated by SecureSmtpMultipart boundary.
                // the multipart boundary is the root part.
                SecureSmtpMultipart Related = new SecureSmtpMultipart(MultipartType.Related);
                Message.RootPart = Related;

                // The first part is the html content.
                // It is added to array of part content children of the multipart parent.
                SecureSmtpContent HtmlContent = new SecureSmtpContent(ContentType.Html, MessageHtml);
                Related.AddPart(HtmlContent);

                // The second part is the inline image attachement.
                SecureSmtpAttachment ImageAttachment = new SecureSmtpAttachment(AttachmentType.Inline, "EmailImage.png", "IMAGE001");
                ImageAttachment.MediaType = "image/png";
                Related.AddPart(ImageAttachment);

                // send mail
                ConnectionSecureHtml.SendMail(Message);
                MessageBox.Show("Email was successfully sent");
            }

            // catch exceptions
            catch (Exception Exp)
            {
                MessageBox.Show(Exp.Message);
            }
            return;
        }
示例#5
0
        /////////////////////////////////////////////////////////////////////
        // Send Mail prototype.
        // This method should be used as a prototype for your email
        // sending method.
        /////////////////////////////////////////////////////////////////////
        private void OnSendEmail(object sender, EventArgs e)
        {
            try
            {
                // read screen parameters
                TestParam Param = ReadScreen();

                // create one of three possible connection classes
                // the SecureSmtpClient is re-useable. If you send more than one email,
                // you can reuse the class. It is of real benefit for gmail.
                if (Connection != null && Param.CompareTo(LastParam) != 0)
                {
                    Connection = null;
                }
                if (Connection == null)
                {
                    switch (Param.Type)
                    {
                    case 0:
                        Connection = new SecureSmtpClient(Param.Host, Param.UserName, new SecureSmtpOAuth2(Param.RefreshToken));
                        break;

                    case 1:
                        Connection = new SecureSmtpClient(ConnectMethod.Secure, Param.Host, Param.UserName, Param.UserPassword);
                        break;

                    case 2:
                        Connection = new SecureSmtpClient(ConnectMethod.Unsecure, Param.Host, Param.UserName, Param.UserPassword);
                        break;
                    }
                    Connection.PortNo  = Param.Port;
                    Connection.Timeout = Param.Timeout;
                    LastParam          = Param;
                }

                // create mail message object
                SecureSmtpMessage Message = new SecureSmtpMessage();

                // Set subject
                Message.Subject = Subject;

                // Set mail from address and display name.
                Message.From = new MailAddress(Param.FromAddress, Param.FromName);

                // Add minimum one or more recipients.
                Message.To.Add(new MailAddress(Param.ToAddress, Param.ToName));

                // create mixed multipart boundary
                SecureSmtpMultipart Mixed = new SecureSmtpMultipart(MultipartType.Mixed);
                Message.RootPart = Mixed;

                // create alternative boundary
                SecureSmtpMultipart Alternative = new SecureSmtpMultipart(MultipartType.Alternative);
                Mixed.AddPart(Alternative);

                // Add plain text mail body contents.
                SecureSmtpContent PlainTextContent = new SecureSmtpContent(ContentType.Plain, PlainTextView);
                Alternative.AddPart(PlainTextContent);

                // create related boundary
                SecureSmtpMultipart Related = new SecureSmtpMultipart(MultipartType.Related);
                Alternative.AddPart(Related);

                // add html mail body content
                SecureSmtpContent HtmlContent = new SecureSmtpContent(ContentType.Html, HtmlView);
                Related.AddPart(HtmlContent);

                // add inline image attachment.
                // NOTE image id is set to IMAGE001 this id must match the html image id in HtmlView text.
                SecureSmtpAttachment ImageAttachment = new SecureSmtpAttachment(AttachmentType.Inline, "EmailImage.png", "IMAGE001");
                ImageAttachment.MediaType = "image/png";
                Related.AddPart(ImageAttachment);

                // add file attachment to the email.
                // The recipient of the email will be able to save it as a file.
                SecureSmtpAttachment PdfAttachment = new SecureSmtpAttachment(AttachmentType.Attachment, "rfc2045.pdf");
                Mixed.AddPart(PdfAttachment);

                // send mail
                Connection.SendMail(Message);
                MessageBox.Show("Email was successfully sent");
            }

            // catch exceptions
            catch (Exception Exp)
            {
                MessageBox.Show(Exp.Message);
            }
            return;
        }