} //SSL設定 //インスタンスの取得 public static Conhig GetInstance() { if (instance == null) { instance = new Conhig(); } return(instance); }
private void btDefault_Click(object sender, RoutedEventArgs e) { Conhig cf = (Conhig.GetInstance()).getDefaultStatic(); tbSmtp.Text = cf.Smtp; tbPort.Text = cf.Port.ToString(); tbSender.Text = tbUserName.Text = cf.MailAddress; tbPassWord.Password = cf.PassWord; cbSsl.IsChecked = cf.Ssl; }
//設定画面ロード時に一度だけ呼び出される private void Window_Loaded(object sender, RoutedEventArgs e) { Conhig stf = (Conhig.GetInstance()); tbUserName.Text = stf.MailAddress; tbPassWord.Password = stf.PassWord; tbSmtp.Text = stf.Smtp; cbSsl.IsChecked = stf.Ssl; tbSender.Text = stf.MailAddress; tbPort.Text = stf.Port.ToString(); }
private void Window_Closed(object sender, EventArgs e) { try { Conhig.GetInstance().Serialise(); } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } }
//適用(更新) private void btApply_Click(object sender, RoutedEventArgs e) { if (tbSmtp.Text == "" || tbUserName.Text == "" || tbPassWord.Password == "" || tbPort.Text == "") { MessageBox.Show("警告"); } else { //更新処理を呼び出す (Conhig.GetInstance()).UpdateStatus(tbSmtp.Text, tbUserName.Text, tbPassWord.Password, int.Parse(tbPort.Text), cbSsl.IsChecked ?? false); } }
//初期値取得 public Conhig getDefaultStatic() { Conhig obj = new Conhig { Smtp = "smtp.gmail.com", MailAddress = "*****@*****.**", PassWord = "******", Port = 587, Ssl = true, }; return(obj); }
//メインウィンドウがロードされるタイミングで呼び出される private void Window_Loaded(object sender, RoutedEventArgs e) { try { Conhig.GetInstance().DeSerialise();//逆シリアル化 XML → オブジェクト } catch (FileNotFoundException) { ConfigWindowShow();//ファイルが存在しないので設定画面を先に表示 } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } }
public void Serialise() //シリアル化 P305参照 { try { XmlSerializer xs = new XmlSerializer(typeof(Conhig)); StreamWriter sw = new StreamWriter("Config.xml"); Conhig cf = Conhig.GetInstance(); xs.Serialize(sw, cf); } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } //using (var writer = XmlWriter.Create("config.xml")) //{ // var serializer = new XmlSerializer(instance.GetType()); // serializer.Serialize(writer, instance); //} }
public void DeSerialise() //逆シリアル化 P307参照 { try { using (StreamReader sr = new StreamReader("Config.xml")) { XmlSerializer xs = new XmlSerializer(typeof(Conhig)); instance = xs.Deserialize(sr) as Conhig; } } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } //using (var reader = XmlReader.Create("config.xml")) //{ // var serializer = new XmlSerializer(typeof(Conhig)); // instance = serializer.Deserialize(reader) as Conhig; //} }
//メール送信 private void btOk_Click(object sender, RoutedEventArgs e) { try { var ch = Conhig.GetInstance(); MailMessage msg = new MailMessage(/*"*****@*****.**"*/ ch.MailAddress, tbTo.Text, tbTitle.Text, tbBody.Text); if (tbCc.Text != "") { msg.CC.Add(tbCc.Text); // } if (tbBcc.Text != "") { msg.Bcc.Add(tbBcc.Text); } //msg.Subject = tbTitle.Text; //件名 //msg.Body = tbBody.Text; //本文 sc.Host = /*"smtp.gmail.com"*/ ch.Smtp; //SMTPサーバーの設定 sc.Port = /*Conhig.GetInstance().Port*/ ch.Port; //PORTの設定 sc.EnableSsl = /*true*/ ch.Ssl; sc.Credentials = new NetworkCredential(/*"*****@*****.**", "ojsInfosys2020"*/ ch.MailAddress, ch.PassWord); //sc.Send(msg); //送信 sc.SendMailAsync(msg); foreach (var item in tbTemp.Items) { msg.Attachments.Add(new Attachment(item.ToString())); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }