Imports System.IO.Ports Imports System.Management Imports System.Text.RegularExpressions Imports System.Security.Cryptography Imports System.Text Imports System.Net.WebUtility Public Class Form1 Private dt_sw As Integer = 0 Dim myPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location Dim appPath As String = System.IO.Path.GetDirectoryName(myPath) Private Const AesIV As String = "ABCDEFGHIJKLMN12" '16文字x8ビット=>128ビット Private Const AesKey As String = "ABCDEFGHIJK12345" '16文字x8ビット=>128ビット Public Function into_code(ByVal txt As String) As String into_code = "" ' 暗号化 Try Dim aes As New AesCryptoServiceProvider() aes.BlockSize = 128 aes.KeySize = 128 aes.IV = Encoding.UTF8.GetBytes(AesIV) aes.Key = Encoding.UTF8.GetBytes(AesKey) aes.Mode = CipherMode.CBC aes.Padding = PaddingMode.PKCS7 ' 文字列をバイト型配列に変換 Dim src As Byte() = Encoding.Unicode.GetBytes(txt) ' 暗号化する Using enc As ICryptoTransform = aes.CreateEncryptor() Dim dest As Byte() = enc.TransformFinalBlock(src, 0, src.Length) ' バイト型配列からBase64形式の文字列に変換 Return Convert.ToBase64String(dest) End Using Catch ex As Exception into_code = Nothing End Try End Function Public Function outfrom_code(ByVal txt As String) As String outfrom_code = "" '復調 Try Dim aes As New AesCryptoServiceProvider() aes.BlockSize = 128 aes.KeySize = 128 aes.IV = Encoding.UTF8.GetBytes(AesIV) aes.Key = Encoding.UTF8.GetBytes(AesKey) aes.Mode = CipherMode.CBC aes.Padding = PaddingMode.PKCS7 'Dim cd As String = "" 'For i = 0 To txt.Length / 2 - 1 ' cd = cd & Chr("&h" & txt.Substring(i * 2, 2)) 'Next ' Base64形式の文字列からバイト型配列に変換 Dim src As Byte() = System.Convert.FromBase64String(HtmlDecode(txt)) ' 複号化する Using dec As ICryptoTransform = aes.CreateDecryptor() Dim dest As Byte() = dec.TransformFinalBlock(src, 0, src.Count) Return Encoding.Unicode.GetString(dest) End Using Catch ex As Exception outfrom_code = Nothing End Try End Function Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed SerialPort1.Close() End Sub Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load Me.Location = New Point((Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2) Me.MinimumSize = New Size(327, 467) Me.MaximumSize = New Size(327, 467) 'サーバ情報の読込 If System.IO.File.Exists("Alone.ini") Then Dim sr As New System.IO.StreamReader("Alone.ini", System.Text.Encoding.GetEncoding("shift_jis")) Dim s As String = sr.ReadToEnd() sr.Close() TextBoxPass.PasswordChar = "*"c Dim sLines As String() = Split(s, vbCr) For i = 0 To sLines.Length - 1 Dim x As String() = Split(Replace(sLines(i), vbLf, ""), vbTab) Select Case x(0) Case LabelFromAdd.Text TextBoxFromAdd.Text = x(1) Case LabelFromName.Text TextBoxFromName.Text = x(1) Case LabelSMTP.Text TextBoxSMTP.Text = x(1) Case LabelPort.Text TextBoxPort.Text = x(1) Case LabelID.Text TextBoxID.Text = x(1) Case LabelPass.Text TextBoxPass.Text = outfrom_code(x(1)) Case "連絡先リスト" ListBoxSendList.Items.Add(x(1)) Case "検知したら" If x(1) = "True" Then RadioButton1.Checked = True RadioButton2.Checked = False Else RadioButton1.Checked = False RadioButton2.Checked = True End If End Select Next End If '送信原稿の読込 If System.IO.File.Exists(appPath & "\Data\SendText.txt") Then Dim sr2 As New System.IO.StreamReader(appPath & "\Data\SendText.txt", System.Text.Encoding.GetEncoding("shift_jis")) Dim s2 As String = sr2.ReadToEnd() sr2.Close() Dim y As String() = s2.Split(vbTab) TextBoxSubject.Text = y(0) TextBoxBody.Text = y(1) End If '利用可能なシリアルポート名の配列を取得する. Dim PortList As String() PortList = SerialPort.GetPortNames() ComboBoxPortNames.Items.Clear() For Each PortName In PortList ComboBoxPortNames.Items.Add(PortName) Next PortName ' Timer1.Interval = 60000 If RadioButton1.Checked = False Then Timer1.Enabled = True Else Timer1.Enabled = False End If End Sub Private Delegate Sub Delegate_RcvDataToTextBox(data As String) Private Sub recieve() Dim com As IO.Ports.SerialPort = Nothing com = My.Computer.Ports.OpenSerialPort(ComboBoxPortNames.SelectedItem.ToString) 'シリアルポートをオープンしていない場合、処理を行わない. If com.IsOpen = False Then Return End If Try '受信データを読み込む. Dim data As String data = com.ReadExisting() '受信したデータをテキストボックスに書き込む. Dim args(0) As Object args(0) = data 'Invoke(New Delegate_RcvDataToTextBox(AddressOf Me.RcvDataToTextBox), args) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Delegate Sub DisplayTextDelegate(ByVal strDisp As String) Private Sub SerialPort1_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived 'データ受信 Dim arrByte As Byte() = New Byte(SerialPort1.BytesToRead - 1) {} '受信バッファー内のデータのバイト数分 'SerialPort の入力バッファーからバイト数を読み取り SerialPort1.Read(arrByte, 0, arrByte.GetLength(0)) 'デリゲート生成 Dim dlg As New DisplayTextDelegate(AddressOf DisplayText) '受信バイト配列を文字列変換 Dim str As String = System.Text.Encoding.GetEncoding("SHIFT-JIS").GetString(arrByte) 'デリゲート関数をコールする Me.Invoke(dlg, New Object() {str}) End Sub Private Sub DisplayText(ByVal strDisp As String) 'テキストBOXに文字列を追加 TextBoxRecieve.Text &= strDisp '人を検知した場合 If InStr(strDisp, "Status 1") <> 0 Then Dim sw As New System.IO.StreamWriter(appPath & "\Data\Record.txt", True, System.Text.Encoding.GetEncoding("shift_jis")) sw.Write(Format(Now, "yyyy/MM/dd hh:mm:ss").ToString & vbCrLf) sw.Close() If RadioButton1.Checked = True Then For i = 0 To ListBoxSendList.Items.Count - 1 Dim x As String = ListBoxSendList.Items(i).ToString Dim y As String() = x.Split(",") mailsending(y(0), y(1)) '名前,アドレス Next LabelMsg.Text = "送信が完了しました" End If End If TextBoxRecieve.SelectionStart = TextBoxRecieve.Text.Length TextBoxRecieve.Focus() TextBoxRecieve.ScrollToCaret() End Sub Private Sub RcvDataToTextBox(data As String) If IsNothing(data) = False Then TextBoxRecieve.AppendText(data) End If End Sub Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) 'SerialPort1.Open() Dim rr As String = SerialPort1.ReadLine() TextBoxRecieve.Text = rr End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click If ComboBoxPortNames.SelectedIndex <> -1 Then If SerialPort1.IsOpen = False Then SerialPort1.PortName = ComboBoxPortNames.SelectedItem.ToString ' シリアルポートの通信速度指定 SerialPort1.BaudRate = 9600 ' シリアルポートのパリティ指定 SerialPort1.Parity = IO.Ports.Parity.None ' シリアルポートのビット数指定 SerialPort1.DataBits = 8 ' シリアルポートのストップビット指定 SerialPort1.StopBits = IO.Ports.StopBits.One ' シリアルポートのオープン SerialPort1.Open() Button1.Text = "Close" Else SerialPort1.Close() Button1.Text = "Open" End If End If End Sub Private Sub Button3_Click_1(sender As System.Object, e As System.EventArgs) Handles Button3.Click If SerialPort1.IsOpen Then 'Dim myPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location 'Dim appPath As String = System.IO.Path.GetDirectoryName(myPath) If System.IO.File.Exists(appPath & "\Data\Program.txt") Then LabelMsg.Text = "" TextBoxRecieve.Text = "" Dim sr As New System.IO.StreamReader(appPath & "\Data\Program.txt", System.Text.Encoding.GetEncoding("shift_jis")) Dim s As String = sr.ReadToEnd() sr.Close() SerialPort1.Write(Chr(3)) System.Threading.Thread.Sleep(1000) Dim sLines As String() = Split(s, vbCr) For i = 0 To sLines.Length - 1 SerialPort1.WriteLine(Replace(sLines(i), vbLf, "") & vbCrLf) Next Else LabelMsg.Text = "プログラムファイルが見つかりません" End If Else LabelMsg.Text = "ポートが閉じています" End If End Sub Private Sub checkDate() '午前0時に実行される Dim dtNow As String = Format(Now.AddDays(-1), "yyyy/MM/dd").ToString 'Dim myPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location 'Dim appPath As String = System.IO.Path.GetDirectoryName(myPath) Select Case dt_sw Case 0 '最初に確認して、活動が無ければ、メールを送る If System.IO.File.Exists(appPath & "\Data\Record.txt") Then Dim sr As New System.IO.StreamReader(appPath & "\Data\Record.txt", System.Text.Encoding.GetEncoding("shift_jis")) Dim s As String = sr.ReadToEnd() sr.Close() If InStr(s, dtNow) = 0 Then '活動無しで、通知が必要 For i = 0 To ListBoxSendList.Items.Count - 1 Dim x As String = ListBoxSendList.Items(i).ToString Dim y As String() = x.Split(",") mailsending(y(0), y(1)) '名前,アドレス Next LabelMsg.Text = "送信が完了しました" Else '活動が見られるので、メール送信は行わない End If End If TextBoxRecieve.Text = "" dt_sw = 1 Case 1 Case Else End Select End Sub Public Sub mailsending(ByRef name As String, ByRef add As String) '受取人の名前とアドレス Dim txt As String = Replace(TextBoxBody.Text, "**漢字氏名**", name) Try Dim msg As New System.Net.Mail.MailMessage() msg.From = New System.Net.Mail.MailAddress(TextBoxFromAdd.Text, TextBoxFromName.Text) 'アドレス、名前 msg.To.Add(New System.Net.Mail.MailAddress(add, name)) 'アドレス、名前 msg.Subject = TextBoxSubject.Text '本文 msg.IsBodyHtml = True msg.Body = txt 'SMTPサーバーなどを設定する Dim sc As New System.Net.Mail.SmtpClient() sc.Host = TextBoxSMTP.Text sc.Port = Integer.Parse(TextBoxPort.Text) 'ユーザー名とパスワードを設定する sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network sc.Credentials = New System.Net.NetworkCredential(TextBoxID.Text, TextBoxPass.Text) 'ID、パスワード 'メッセージを送信する sc.Send(msg) msg.Dispose() sc.Dispose() Catch ex As Exception LabelMsg.Text = "送信エラー" End Try End Sub Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick '午前0時に確認 Dim hrNow As Integer = Integer.Parse(Format(Now, "HH").ToString) If hrNow = 0 Then checkDate() Else If hrNow = 23 Then dt_sw = 0 End If End If End Sub Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click '条件の保存 Dim x As String = "エラー:" If Len(TextBoxFromAdd.Text) = 0 Then MsgBox(x & LabelFromAdd.Text) : Exit Sub If Len(TextBoxFromName.Text) = 0 Then MsgBox(x & LabelFromName.Text) : Exit Sub If Len(TextBoxSMTP.Text) = 0 Then MsgBox(x & LabelSMTP.Text) : Exit Sub If Len(TextBoxPort.Text) = 0 Then MsgBox(x & LabelPort.Text) : Exit Sub If Len(TextBoxID.Text) = 0 Then MsgBox(x & LabelID.Text) : Exit Sub If Len(TextBoxPass.Text) = 0 Then MsgBox(x & LabelPass.Text) : Exit Sub Dim fileno As String = FreeFile() FileOpen(fileno, "Alone.ini", OpenMode.Output) PrintLine(fileno, LabelFromAdd.Text & vbTab & TextBoxFromAdd.Text) PrintLine(fileno, LabelFromName.Text & vbTab & TextBoxFromName.Text) PrintLine(fileno, LabelSMTP.Text & vbTab & TextBoxSMTP.Text) PrintLine(fileno, LabelPort.Text & vbTab & TextBoxPort.Text) PrintLine(fileno, LabelID.Text & vbTab & TextBoxID.Text) PrintLine(fileno, LabelPass.Text & vbTab & into_code(TextBoxPass.Text)) For i = 0 To ListBoxSendList.Items.Count - 1 PrintLine(fileno, "連絡先リスト" & vbTab & ListBoxSendList.Items(i).ToString) Next PrintLine(fileno, "検知したら" & vbTab & RadioButton1.Checked.ToString) FileClose(fileno) LabelMsg2.Text = "保管しました" End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click '通知先リストに追加 If Len(TextBoxSendAdd.Text) = 0 Then Exit Sub If Len(TextBoxSendName.Text) = 0 Then Exit Sub ListBoxSendList.Items.Add(TextBoxSendName.Text & "," & TextBoxSendAdd.Text) End Sub Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click '通知先リストからの削除 If ListBoxSendList.SelectedIndex = -1 Then Exit Sub Dim n As Integer = ListBoxSendList.SelectedIndex ListBoxSendList.Items.RemoveAt(n) End Sub Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click 'メール送信文の保管 If Len(TextBoxSubject.Text) = 0 Then Exit Sub If Len(TextBoxBody.Text) = 0 Then Exit Sub 'Dim myPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location 'Dim appPath As String = System.IO.Path.GetDirectoryName(myPath) Dim sw As New System.IO.StreamWriter(appPath & "\Data\SendText.txt", False, System.Text.Encoding.GetEncoding("shift_jis")) sw.Write(TextBoxSubject.Text & vbTab & TextBoxBody.Text) sw.Close() End Sub Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click '自分宛てにテストメール Dim x As String = "エラー:" If Len(TextBoxFromAdd.Text) = 0 Then MsgBox(x & LabelFromAdd.Text) : Exit Sub If Len(TextBoxFromName.Text) = 0 Then MsgBox(x & LabelFromName.Text) : Exit Sub If Len(TextBoxSMTP.Text) = 0 Then MsgBox(x & LabelSMTP.Text) : Exit Sub If Len(TextBoxPort.Text) = 0 Then MsgBox(x & LabelPort.Text) : Exit Sub If Len(TextBoxID.Text) = 0 Then MsgBox(x & LabelID.Text) : Exit Sub If Len(TextBoxPass.Text) = 0 Then MsgBox(x & LabelPass.Text) : Exit Sub If Len(TextBoxSubject.Text) = 0 Then MsgBox(x & "Subjectが有りません") : Exit Sub If Len(TextBoxBody.Text) = 0 Then MsgBox(x & "本文が有りません") : Exit Sub mailsending(TextBoxFromName.Text, TextBoxFromAdd.Text) LabelMsg2.Text = "テストメールを送信しました" End Sub Private Sub RadioButton1_CheckedChanged(sender As Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged If RadioButton1.Checked = True Then RadioButton2.Checked = False Timer1.Enabled = False Else RadioButton2.Checked = True Timer1.Enabled = True End If End Sub End Class