Пример #1
0
        public static void Mail(string id, string pass, string from, string to, string subject, string body, string path)
        {
            //Console.WriteLine("Hello SMTP World!");

            /*
             * string id = "<gmailのログインID>";
             * string pass = "******";
             * string from = "<宛先>";
             * string to = "<自分のメール>";
             * string subject = "送信テスト : " + DateTime.Now.ToString();
             * string body = "from t.masuda";
             */
#if false
            var smtp = new System.Net.Mail.SmtpClient();
            smtp.Host        = "smtp.gmail.com";                           //SMTPサーバ
            smtp.Port        = 587;                                        //SMTPポート
            smtp.EnableSsl   = true;
            smtp.Credentials = new System.Net.NetworkCredential(id, pass); //認証
            var msg = new System.Net.Mail.MailMessage(from, to, subject, body);
            smtp.Send(msg);                                                //メール送信
#else
            var smtp = new MailKit.Net.Smtp.SmtpClient();
            smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.Auto);
            smtp.Authenticate(id, pass);

            var mail    = new MimeKit.MimeMessage();
            var builder = new MimeKit.BodyBuilder();

            mail.From.Add(new MimeKit.MailboxAddress("", from));
            mail.To.Add(new MimeKit.MailboxAddress("", to));
            mail.Subject     = subject;
            builder.TextBody = body;
            //mail.Body = builder.ToMessageBody();

            //var path = @"C:\Windows\Web\Wallpaper\Theme2\img10.jpg"; // 添付したいファイル
            var attachment = new MimeKit.MimePart("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                Content                 = new MimeKit.MimeContent(File.OpenRead(path)),
                ContentDisposition      = new MimeKit.ContentDisposition(),
                ContentTransferEncoding = MimeKit.ContentEncoding.Base64,
                FileName                = System.IO.Path.GetFileName(path)
            };

            var multipart = new MimeKit.Multipart("mixed");
            multipart.Add(builder.ToMessageBody());
            multipart.Add(attachment);

            mail.Body = multipart;

            smtp.Send(mail);
            smtp.Disconnect(true);
#endif

            Console.WriteLine("メールを送信しました");
        }
Пример #2
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);
            }
        }