コード例 #1
0
        private void SendMail()
        {
            string mailSubject = "終業打刻忘れ通知";
            string mailBody    = "終業打刻がされてないっぽいです";

            var addrList = _smtpMailToRaw.Split(";");

            var message = new MimeKit.MimeMessage();

            message.From.Add(new MimeKit.MailboxAddress(_smtpAccount));
            foreach (var addr in addrList)
            {
                message.To.Add(new MimeKit.MailboxAddress(addr.Trim()));
            }
            message.Subject = mailSubject;

            var textpart = new MimeKit.TextPart(MimeKit.Text.TextFormat.Plain);

            textpart.Text = mailBody;
            message.Body  = textpart;

            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                client.Connect(this._smtpServer, this._smtpPort);
                client.Authenticate(_smtpAccount, this._smtpPass);
                client.Send(message);
                client.Disconnect(true);
            }
        }
コード例 #2
0
        /// <summary>
        /// メール送信試験
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void SendTestButton_Click(object sender, EventArgs e)
        {
            try
            {
                var message = new MimeKit.MimeMessage();
                message.From.Add(new MimeKit.MailboxAddress(SenderNameTextBox.Text, SenderAddressTextBox.Text));

                string[] name      = ReceiverNameTextBox.Text.Replace(",", ";").Split(';');
                string[] addresses = ReceiverAddressTextBox.Text.Replace(",", ";").Split(';');


                try
                {
                    for (int i = 0; i < addresses.Length; i++)
                    {
                        message.To.Add(new MimeKit.MailboxAddress(name[i].Trim(), addresses[i].Trim()));
                    }
                }
                catch (Exception)
                {
                    this.ShowErrorDialog("宛先・送信先エラー", "宛先又は送信先が不正です。");
                    return;
                }

                message.Subject = MailTitleTextBox.Text;
                var textPart = new MimeKit.TextPart(MimeKit.Text.TextFormat.Plain);
                textPart.Text = MailTextBox.Text;
                message.Body  = textPart;
                using (var client = new MailKit.Net.Smtp.SmtpClient())
                {
                    try
                    {
                        await client.ConnectAsync(MailHostTextBox.Text, int.Parse(PortNoTextBox.Text));

                        if (!string.IsNullOrEmpty(UserNameTextBox.Text) && !string.IsNullOrEmpty(PasswordTextBox.Text))
                        {
                            await client.AuthenticateAsync(UserNameTextBox.Text, PasswordTextBox.Text);
                        }
                        await client.SendAsync(message);

                        await client.DisconnectAsync(true);

                        this.ShowDialog("送信成功", "メール送信に成功しました!!");
                    }
                    catch (Exception ex)
                    {
                        this.ShowErrorDialog("メール送信失敗", ex.Message);
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                throw Program.ThrowException(ex);
            }
        }
コード例 #3
0
        public async void SendMailAsync(string to, string from, string subject, string text)
        {
            var message = new MimeKit.MimeMessage();

            // 宛先情報
            message.From.Add(new MimeKit.MailboxAddress("<宛名>", from));
            // 送り元情報
            message.To.Add(new MimeKit.MailboxAddress("<送信者名>", to));
            // 表題
            message.Subject = subject;
            // 内容
            var textPart = new MimeKit.TextPart(MimeKit.Text.TextFormat.Plain);

            textPart.Text = text;
            message.Body  = textPart;

            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
                try
                {
                    // SMTPサーバに接続
                    await client.ConnectAsync(SmtpHost, SmtpPort);

                    Debug.WriteLine("接続完了");

                    // SMTPサーバ認証(あれば)
                    await client.AuthenticateAsync(User, Pass);

                    // 送信
                    await client.SendAsync(message);

                    // 切断
                    await client.DisconnectAsync(true);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("{0} Exception caught.", e);
                }
            }
        }
コード例 #4
0
 private async Task SendHtmlMail(string smtpserver, int port, bool usessh, string account, string pwd, string FromNickName, string FromAccount, string ToNickName, string ToAccount, string Subject, string HtmlContent)
 {
     try
     {
         var message = new MimeKit.MimeMessage();
         message.From.Add(new MimeKit.MailboxAddress(FromNickName, FromAccount));
         message.To.Add(new MimeKit.MailboxAddress(ToNickName, ToAccount));
         message.Subject = Subject;
         var html = new MimeKit.TextPart("html")
         {
             Text = HtmlContent
         };
         // create an image attachment for the file located at path
         var alternative = new MimeKit.Multipart("alternative");
         alternative.Add(html);
         // now create the multipart/mixed container to hold the message text and the
         // image attachment
         var multipart = new MimeKit.Multipart("mixed");
         multipart.Add(alternative);
         message.Body = multipart;
         using (var client = new MailKit.Net.Smtp.SmtpClient())
         {
             client.Connect(smtpserver, port, usessh);
             // Note: since we don't have an OAuth2 token, disable
             // the XOAUTH2 authentication mechanism.
             client.AuthenticationMechanisms.Remove("XOAUTH2");
             // Note: only needed if the SMTP server requires authentication
             var mailFromAccount = account;
             var mailPassword    = pwd;
             client.Authenticate(mailFromAccount, mailPassword);
             client.Send(message);
             client.Disconnect(true);
         }
     }
     catch (Exception ex)
     {
         throw new InvalidOperationException(ex.Message);
     }
 }
コード例 #5
0
        /// <summary>
        /// メール送信を実行します。
        /// </summary>
        /// <param name="fromUser"></param>
        /// <param name="fromAddress"></param>
        /// <param name="toUser"></param>
        /// <param name="toAddress"></param>
        /// <param name="title"></param>
        /// <param name="msg"></param>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="attachList"></param>
        public static void Send(string fromUser, string fromAddress, string toUser, string toAddress, string title, string msg, string host, int port, string userName, string password, Dictionary <int, string> attachList)
        {
            try
            {
                List <string> tmpAttachPathList = new List <string>();
                List <Stream> attachStreamList  = new List <Stream>();
                var           message           = new MimeKit.MimeMessage();

                string[] name      = toUser.Replace(",", ";").Split(';');
                string[] addresses = toAddress.Replace(",", ";").Split(';');

                message.From.Add(new MimeKit.MailboxAddress(fromUser, fromAddress));

                try
                {
                    for (int i = 0; i < addresses.Length; i++)
                    {
                        message.To.Add(new MimeKit.MailboxAddress(name[i].Trim(), addresses[i].Trim()));
                    }
                }
                catch (Exception)
                {
                    throw new Exception("送信先名・送信先アドレスの数が一致しません。");
                }

                message.Subject = title;
                var textPart = new MimeKit.TextPart(MimeKit.Text.TextFormat.Plain);
                textPart.Text = msg;

                if (HasAttach(attachList))
                {
                    var multipart = new MimeKit.Multipart("mixed");
                    multipart.Add(textPart);
                    foreach (var attach in attachList)
                    {
                        if (!string.IsNullOrEmpty(attach.Value))
                        {
                            if (!System.IO.File.Exists(attach.Value))
                            {
                                throw new Exception("添付ファイル : [" + attach.Value + "] が見つかりませんでした。");
                            }
                            var attachment = new MimeKit.MimePart();
                            attachment.Content = new MimeKit.MimeContent(System.IO.File.OpenRead(attach.Value));
                            attachStreamList.Add(attachment.Content.Stream);
                            attachment.ContentDisposition      = new MimeKit.ContentDisposition();
                            attachment.ContentTransferEncoding = MimeKit.ContentEncoding.Base64;
                            attachment.FileName = System.IO.Path.GetFileName(attach.Value);
                            multipart.Add(attachment);
                        }
                    }
                    message.Body = multipart;
                }
                else
                {
                    message.Body = textPart;
                }
                using (var client = new MailKit.Net.Smtp.SmtpClient())
                {
                    client.Connect(host, port);
                    if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
                    {
                        client.Authenticate(userName, password);
                    }
                    client.Send(message);
                    client.Disconnect(true);
                }
                //メール添付ファイルのストリームを閉じる
                foreach (var s in attachStreamList)
                {
                    s.Close();
                }
            }
            catch (Exception ex)
            {
                throw Program.ThrowException(ex);
            }
        }