using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using ORiN2.interop.CAO; using System.Runtime.InteropServices; namespace CaoSmtpSample { public partial class frmCaoSmtpSample : Form { private CaoEngine m_caoEng; private CaoWorkspaces m_caoWss; private CaoWorkspace m_caoWs; private CaoControllers m_caoCtrls; private CaoController m_caoCtrl; private CaoFiles m_cFiles; private CaoFile m_cFile; // Initialize public frmCaoSmtpSample() { InitializeComponent(); } // Form Load private void frmCaoSmtpSample_Load(object sender, EventArgs e) { m_caoEng = new CaoEngine(); m_caoWss = m_caoEng.Workspaces; m_caoWs = m_caoWss.Item(0); m_caoCtrls = m_caoWs.Controllers; m_caoCtrl = m_caoWs.AddController("TestCtrl", "CaoProv.SMTP"); m_cFiles = m_caoCtrl.Files; cmbAuth.SelectedIndex = 1; cmbSecurity.SelectedIndex = 0; cmbCharSet.SelectedIndex = 1; cmbPriority.SelectedIndex = 1; } // Form Closed private void frmCaoSmtpSample_FormClosed(object sender, FormClosedEventArgs e) { m_cFiles.Clear(); if (m_cFile != null) { Marshal.ReleaseComObject(m_cFile); m_cFile = null; } m_caoCtrls.Clear(); Marshal.ReleaseComObject(m_cFiles); m_cFiles = null; Marshal.ReleaseComObject(m_caoCtrl); m_caoCtrl = null; m_caoWss.Clear(); Marshal.ReleaseComObject(m_caoCtrls); m_caoCtrls = null; Marshal.ReleaseComObject(m_caoWs); m_caoWs = null; Marshal.ReleaseComObject(m_caoWss); m_caoWss = null; Marshal.ReleaseComObject(m_caoEng); m_caoEng = null; } // AddFile Click private void btnAddFile_Click(object sender, EventArgs e) { try { DeleteFile(); m_cFile = m_caoCtrl.AddFile("mail1", String.Format("Server={0},Port={1},Auth={2},Security={3},CharSet={4}", txtServer.Text,txtPort.Text, cmbAuth.Text, cmbSecurity.Text, cmbCharSet.Text)); if (m_cFile != null) MessageBox.Show(this, "Was created"); else MessageBox.Show(this, "I failed to create"); } catch(Exception ex) { MessageBox.Show(this, ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } // DelFile Click private void btnDelFile_Click(object sender, EventArgs e) { DeleteFile(); MessageBox.Show(this, "Has been deleted"); } // DeleteFile private void DeleteFile() { if (m_caoCtrl.Files.Count > 0) m_caoCtrl.Files.Remove(0); } // SendExecute private bool SendExecute(string strCommand, object objData) { try { m_cFile.Execute(strCommand, objData); return true; } catch (Exception ex) { MessageBox.Show(this, ex.Message); return false; } } // btnClearTo Click private void btnClearTO_Click(object sender, EventArgs e) { bool bRet = false; if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } bRet = SendExecute("DelRecipients", null); if (bRet) MessageBox.Show(this, "Was successful"); else MessageBox.Show(this, "Has failed"); } // btnClearCC Click private void btnClearCC_Click(object sender, EventArgs e) { bool bRet = false; if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } bRet = SendExecute("DelCCRecipients", null); if (bRet) MessageBox.Show(this, "Was successful"); else MessageBox.Show(this, "Has failed"); } // btnClearBCC Click private void btnClearBCC_Click(object sender, EventArgs e) { bool bRet = false; if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } bRet = SendExecute("DelBCCRecipients", null); if (bRet) MessageBox.Show(this, "Was successful"); else MessageBox.Show(this, "Has failed"); } // btnMessage Click private void btnClearMessage_Click(object sender, EventArgs e) { bool bRet = false; if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } bRet = SendExecute("DelMsgLines", null); if (bRet) MessageBox.Show(this, "Was successful"); else MessageBox.Show(this, "Has failed"); } // btnClearAttachment Click private void btnClearAttachment_Click(object sender, EventArgs e) { bool bRet = false; if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } bRet = SendExecute("DelAttachments", null); if (bRet) MessageBox.Show(this, "Was successful"); else MessageBox.Show(this, "Has failed"); } // btnClearAll private void btnClearAll_Click(object sender, EventArgs e) { bool bRet = false; if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } bRet = SendExecute("ClearMessage", null); if (bRet) MessageBox.Show(this, "Was successful"); else MessageBox.Show(this, "Has failed"); } // btnInputAreaClear Click private void btnInputAreaClear_Click(object sender, EventArgs e) { txtServer.Text = ""; txtPort.Text = ""; txtLogin.Text = ""; txtPassword.Text = ""; txtSenderMail.Text = ""; txtSenderName.Text = ""; txtReplyTo.Text = ""; txtTO.Text = ""; txtCC.Text = ""; txtBCC.Text = ""; txtTitle.Text = ""; txtMessage.Text = ""; txtSendFileName.Text = ""; txtAttachment.Text = ""; } // btnToggle Click private void btnToggle_Click(object sender, EventArgs e) { bool bChk = false; if (chkLogin.Checked) bChk = false; else bChk = true; SetAllCheck(bChk); } // SetAllCheck private void SetAllCheck(bool bChk) { chkLogin.Checked = bChk; chkPassword.Checked = bChk; chkSenderMail.Checked = bChk; chkSenderName.Checked = bChk; chkReplyTo.Checked = bChk; chkTO.Checked = bChk; chkCC.Checked = bChk; chkBCC.Checked = bChk; chkTitle.Checked = bChk; chkMessage.Checked = bChk; chkSendFileName.Checked = bChk; chkAttach.Checked = bChk; chkPriority.Checked = bChk; } // btnFile Click private void btnFile_Click(object sender, EventArgs e) { if (dlgOpenFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) txtAttachment.Text = dlgOpenFile.FileName; } // btnExecute Click private void btnExecute_Click(object sender, EventArgs e) { byte[] binBuffer; if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } try { if(chkLogin.Checked) m_cFile.Execute("SetLogin",txtLogin.Text); if(chkPassword.Checked) m_cFile.Execute("SetPassword",txtPassword.Text); if(chkSenderMail.Checked) m_cFile.Execute("SetSenderMail",txtSenderMail.Text); if(chkSenderName.Checked) m_cFile.Execute("SetSenderName",txtSenderName.Text); if(chkReplyTo.Checked) m_cFile.Execute("SetReplyTo",txtReplyTo.Text); if(chkTO.Checked) m_cFile.Execute("AddRecipient",txtTO.Text); if(chkCC.Checked) m_cFile.Execute("AddCCRecipient",txtCC.Text); if(chkBCC.Checked) m_cFile.Execute("AddBCCRecipient",txtBCC.Text); if(chkTitle.Checked) m_cFile.Execute("SetSubject",txtTitle.Text); if(chkMessage.Checked) m_cFile.Execute("AddMsgLine",txtMessage.Text); if(chkSendFileName.Checked) m_cFile.Execute("SetAttachFileName",txtSendFileName.Text); if(chkAttach.Checked) { if(chkBin.Checked) { binBuffer = File.ReadAllBytes(txtAttachment.Text); m_cFile.Execute("AddAttachment", binBuffer); } else { m_cFile.Execute("AddAttachment", txtAttachment.Text); } } if(chkPriority.Checked) m_cFile.Execute("SetXPriority",cmbPriority.Text); MessageBox.Show(this, "Was successful"); } catch (Exception ex) { MessageBox.Show(this, ex.Message); return; } } // btnSend Click private void btnSend_Click(object sender, EventArgs e) { bool bRet = false; if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } bRet = SendExecute("Send", null); if (bRet) MessageBox.Show(this, "Was successful"); else MessageBox.Show(this, "Has failed"); } // btnGetValue Click private void btnGetValue_Click(object sender, EventArgs e) { try { if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } txtMessage.Text = Convert.ToString(m_cFile.Value); MessageBox.Show(this, "Was successful"); } catch (Exception ex) { MessageBox.Show(this, ex.Message); } } // btnPutValue Click private void btnPutValue_Click(object sender, EventArgs e) { try { if (m_cFile == null || m_caoCtrl.Files.Count == 0) { MessageBox.Show(this, "Please press the AddFileButton first"); return; } m_cFile.Value = txtMessage.Text; MessageBox.Show(this, "Was successful"); } catch (Exception ex) { MessageBox.Show(this, ex.Message); } } } }