N
nathan
Hi, I'm getting the outlook warning message: "A program is trying to access
e-mail addresses you have stored in Outlook..." Of course followed by the "A
program is trying to automatically send e-mail..." I've looked exhaustively
on MSDN and the newsgroups to find a solution that I understand how to use.
I need to have my solution packaged with my installation script. I don't
really want the "ClickYes" utility, because I'm hoping to have a solution
that installs everything it needs automatically. As far as I understand, the
best thing for me to do is to create a Add In which exposes the Outlook
properties I need to use? As it is now: here's my Outlook Accessing class:
public class CPOutlook {
private Outlook.Application outlook;
private Outlook._NameSpace nameSpace;
private Outlook._MailItem mail;
private Outlook.Recipient recip;
private Outlook._ContactItem contactItem;
public CPOutlook() {
outlook = new Outlook.ApplicationClass();
nameSpace = (Outlook.NameSpace)outlook.GetNamespace("MAPI");
}
public void CreateMailItem() {
mail = (Outlook.MailItem)outlook.CreateItem(Outlook.OlItemType.olMailItem);
}
public void AddMessage(string messageString)
{
mail.Body = messageString.ToString();
}
public string getSender()
{
return mail.SenderEmailAddress.ToString();
}
public string getCurrentUser()
{
return nameSpace.CurrentUser.Name.ToString();
}
public void AddSubject(string subject)
{
mail.Subject = subject.ToString();
}
public void AddAttachments(string[] attachmentArray)
{
for (int i=0; i<attachmentArray.Length; i++)
{
string sSource = attachmentArray;
if (sSource != "")
{
string sDisplayName = attachmentArray;
int iPosition = (int)mail.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach =
mail.Attachments.Add(sSource,iAttachType,iPosition,sDisplayName);
}
}
}
public void SendMessage()
{
mail.Save();
mail.Send();
}
public void SendVCard(int contactID) {
DataSet ds = new
Contact(GlobalConfiguration.GetInstance().ConnectionString).GetContact(contactID);
contactItem =
(Outlook.ContactItem)outlook.CreateItem(Outlook.OlItemType.olContactItem);
contactItem.Email1Address = ds.Tables[0].Rows[0]["EmailName"].ToString();
contactItem.LastName = ds.Tables[0].Rows[0]["LastName"].ToString();
contactItem.FirstName = ds.Tables[0].Rows[0]["FirstName"].ToString();
contactItem.JobTitle = ds.Tables[0].Rows[0]["Title"].ToString();
contactItem.CompanyName = ds.Tables[0].Rows[0]["CompanyName"].ToString();
contactItem.MailingAddressStreet =
ds.Tables[0].Rows[0]["Address"].ToString() + '\r' +
ds.Tables[0].Rows[0]["Suite"].ToString();
contactItem.MailingAddressCity = ds.Tables[0].Rows[0]["City"].ToString();
contactItem.MailingAddressState =
ds.Tables[0].Rows[0]["StateOrProvince"].ToString();
contactItem.MailingAddressPostalCode =
ds.Tables[0].Rows[0]["PostalCode"].ToString();
contactItem.BusinessTelephoneNumber =
CPUtilities.FormatPhone(ds.Tables[0].Rows[0]["WorkPhone"].ToString());
contactItem.HomeTelephoneNumber =
CPUtilities.FormatPhone(ds.Tables[0].Rows[0]["Home Phone"].ToString());
contactItem.BusinessFaxNumber =
CPUtilities.FormatPhone(ds.Tables[0].Rows[0]["FaxNumber"].ToString());
contactItem.MobileTelephoneNumber =
CPUtilities.FormatPhone(ds.Tables[0].Rows[0]["MobilePhone"].ToString());
if(contactItem != null) {
mail = contactItem.ForwardAsVcard();
}
else {
mail =
(Outlook.MailItem)outlook.CreateItem(Outlook.OlItemType.olMailItem);
}
}
public void AddRecipients(string[] recipients) {
for(int i = 0;i < recipients.Length;i++) {
if(recipients.Length > 0) {
recip = mail.Recipients.Add(recipients);
recip.Type = (int)Outlook.OlMailRecipientType.olBCC;
recip.Resolve();
}
}
}
public void AddRecipients(string[] recipients, bool useTo) {
for(int i = 0;i < recipients.Length;i++) {
if(recipients.Length > 0) {
recip = mail.Recipients.Add(recipients);
recip.Type = (int)Outlook.OlMailRecipientType.olTo;
recip.Resolve();
}
}
}
public void ShowOutlook() {
mail.Display(false);
}
}
Then I have a windows pop up that sends email: the code for that is here:
public class EmailPopup : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox EmailTextBox;
private System.Windows.Forms.Label EmailMessageLabel;
private System.Windows.Forms.TextBox RecipientTextBox;
private System.Windows.Forms.Label RecipientLabel;
private System.Windows.Forms.Button button1;
private int ContactID;
private string connectionString;
private string userName;
//private string[] attachmentArray = new string[100];
//private int attachmentCount = 0;
private System.Windows.Forms.Button AttachmentButton;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.TextBox attachmentsTextBox;
private System.Windows.Forms.Button CloseButton;
private System.Windows.Forms.Label subjectLabel;
private System.Windows.Forms.TextBox subjectTextBox;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public EmailPopup()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
public void setRecipients(string recipients)
{
this.RecipientTextBox.Text = recipients.ToString();
}
public void setContactID(int newContactID)
{
this.ContactID = newContactID;
}
public void setUserName(string user)
{
this.userName = user;
}
public void setConnectionString(string newConnectionString)
{
this.connectionString = newConnectionString;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.EmailTextBox = new System.Windows.Forms.TextBox();
this.EmailMessageLabel = new System.Windows.Forms.Label();
this.RecipientTextBox = new System.Windows.Forms.TextBox();
this.RecipientLabel = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.AttachmentButton = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.CloseButton = new System.Windows.Forms.Button();
this.attachmentsTextBox = new System.Windows.Forms.TextBox();
this.subjectLabel = new System.Windows.Forms.Label();
this.subjectTextBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// EmailTextBox
//
this.EmailTextBox.AcceptsReturn = true;
this.EmailTextBox.AcceptsTab = true;
this.EmailTextBox.AllowDrop = true;
this.EmailTextBox.Location = new System.Drawing.Point(16, 144);
this.EmailTextBox.Multiline = true;
this.EmailTextBox.Name = "EmailTextBox";
this.EmailTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.EmailTextBox.Size = new System.Drawing.Size(568, 216);
this.EmailTextBox.TabIndex = 0;
this.EmailTextBox.Text = "";
//
// EmailMessageLabel
//
this.EmailMessageLabel.Location = new System.Drawing.Point(16, 128);
this.EmailMessageLabel.Name = "EmailMessageLabel";
this.EmailMessageLabel.Size = new System.Drawing.Size(100, 16);
this.EmailMessageLabel.TabIndex = 1;
this.EmailMessageLabel.Text = "Message:";
//
// RecipientTextBox
//
this.RecipientTextBox.Location = new System.Drawing.Point(16, 32);
this.RecipientTextBox.Multiline = true;
this.RecipientTextBox.Name = "RecipientTextBox";
this.RecipientTextBox.ScrollBars =
System.Windows.Forms.ScrollBars.Vertical;
this.RecipientTextBox.Size = new System.Drawing.Size(568, 40);
this.RecipientTextBox.TabIndex = 2;
this.RecipientTextBox.Text = "";
//
// RecipientLabel
//
this.RecipientLabel.Location = new System.Drawing.Point(16, 16);
this.RecipientLabel.Name = "RecipientLabel";
this.RecipientLabel.Size = new System.Drawing.Size(100, 16);
this.RecipientLabel.TabIndex = 3;
this.RecipientLabel.Text = "Recipient list:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(416, 432);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(168, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Send Message";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// AttachmentButton
//
this.AttachmentButton.Location = new System.Drawing.Point(16, 376);
this.AttachmentButton.Name = "AttachmentButton";
this.AttachmentButton.Size = new System.Drawing.Size(104, 23);
this.AttachmentButton.TabIndex = 4;
this.AttachmentButton.Text = "Attachments...";
this.AttachmentButton.Click += new
System.EventHandler(this.AttachmentButton_Click);
//
// CloseButton
//
this.CloseButton.Location = new System.Drawing.Point(288, 432);
this.CloseButton.Name = "CloseButton";
this.CloseButton.Size = new System.Drawing.Size(112, 23);
this.CloseButton.TabIndex = 5;
this.CloseButton.Text = "Cancel";
this.CloseButton.Click += new System.EventHandler(this.CancelButton_Click);
//
// attachmentsTextBox
//
this.attachmentsTextBox.Location = new System.Drawing.Point(136, 376);
this.attachmentsTextBox.Multiline = true;
this.attachmentsTextBox.Name = "attachmentsTextBox";
this.attachmentsTextBox.ScrollBars =
System.Windows.Forms.ScrollBars.Vertical;
this.attachmentsTextBox.Size = new System.Drawing.Size(448, 40);
this.attachmentsTextBox.TabIndex = 6;
this.attachmentsTextBox.Text = "";
//
// subjectLabel
//
this.subjectLabel.Location = new System.Drawing.Point(16, 80);
this.subjectLabel.Name = "subjectLabel";
this.subjectLabel.Size = new System.Drawing.Size(100, 16);
this.subjectLabel.TabIndex = 7;
this.subjectLabel.Text = "Subject";
//
// subjectTextBox
//
this.subjectTextBox.Location = new System.Drawing.Point(16, 96);
this.subjectTextBox.Name = "subjectTextBox";
this.subjectTextBox.Size = new System.Drawing.Size(568, 20);
this.subjectTextBox.TabIndex = 8;
this.subjectTextBox.Text = "";
//
// EmailPopup
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(602, 466);
this.ControlBox = false;
this.Controls.Add(this.subjectTextBox);
this.Controls.Add(this.subjectLabel);
this.Controls.Add(this.attachmentsTextBox);
this.Controls.Add(this.CloseButton);
this.Controls.Add(this.AttachmentButton);
this.Controls.Add(this.button1);
this.Controls.Add(this.RecipientLabel);
this.Controls.Add(this.RecipientTextBox);
this.Controls.Add(this.EmailTextBox);
this.Controls.Add(this.EmailMessageLabel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "EmailPopup";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
try
{
if(this.RecipientTextBox.Text.Trim() != String.Empty)
{
string delimStr = ",;";
char [] delimiter = delimStr.ToCharArray();
string callLog = "";
string[] attachmentArray =
this.attachmentsTextBox.Text.ToString().Split(delimiter,100);
CPOutlook cpo = new CPOutlook();
cpo.CreateMailItem();
callLog += "From: " + System.Environment.UserName.ToString() + "\r\n";
string[] recipients = new string[1] {this.RecipientTextBox.Text};
cpo.AddRecipients(recipients,true);
callLog += "To: ";
for (int i=0;i<recipients.Length;i++)
{
if (i > 0)
callLog += ", ";
callLog += recipients.ToString();
}
callLog += "\r\n";
string emailMessage = this.EmailTextBox.Text.ToString();
if (emailMessage == "")
emailMessage = "Message Body Empty.";
cpo.AddMessage(emailMessage);
cpo.AddSubject(this.subjectTextBox.Text.ToString());
callLog += "Subject: ";
callLog += this.subjectTextBox.Text.ToString();
callLog += "\r\n";
callLog += "Attachments: ";
callLog += this.attachmentsTextBox.Text.ToString();
callLog += "\r\n";
callLog += "Message: ";
string removeBreaksMessage = emailMessage.Replace("\r"," ");
string removeLinesMessage = emailMessage.Replace("\n"," ");
callLog += removeLinesMessage;
cpo.AddAttachments(attachmentArray);
cpo.SendMessage();
Calls.Insert(this.connectionString,this.ContactID,callLog);
//cpo.ShowOutlook();
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AttachmentButton_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
//here goes the code to add an attachment and show it with a
//link to remove it
string filePath = openFileDialog1.FileName.ToString();
this.attachmentsTextBox.Text += filePath;
this.attachmentsTextBox.Text += ";";
//this.attachmentArray[this.attachmentCount] = filePath;
//this.attachmentCount++;
}
}
private void CancelButton_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
This code works fine except for the incessant outlook warning. So, I tried
adding an Outlook Addin to my solution, then I added the code from my outlook
referencing class to that file, and changed my references around, but I was
getting a build error. I'm pretty lost as far as this goes. Thanks for the
help.
Nathan
e-mail addresses you have stored in Outlook..." Of course followed by the "A
program is trying to automatically send e-mail..." I've looked exhaustively
on MSDN and the newsgroups to find a solution that I understand how to use.
I need to have my solution packaged with my installation script. I don't
really want the "ClickYes" utility, because I'm hoping to have a solution
that installs everything it needs automatically. As far as I understand, the
best thing for me to do is to create a Add In which exposes the Outlook
properties I need to use? As it is now: here's my Outlook Accessing class:
public class CPOutlook {
private Outlook.Application outlook;
private Outlook._NameSpace nameSpace;
private Outlook._MailItem mail;
private Outlook.Recipient recip;
private Outlook._ContactItem contactItem;
public CPOutlook() {
outlook = new Outlook.ApplicationClass();
nameSpace = (Outlook.NameSpace)outlook.GetNamespace("MAPI");
}
public void CreateMailItem() {
mail = (Outlook.MailItem)outlook.CreateItem(Outlook.OlItemType.olMailItem);
}
public void AddMessage(string messageString)
{
mail.Body = messageString.ToString();
}
public string getSender()
{
return mail.SenderEmailAddress.ToString();
}
public string getCurrentUser()
{
return nameSpace.CurrentUser.Name.ToString();
}
public void AddSubject(string subject)
{
mail.Subject = subject.ToString();
}
public void AddAttachments(string[] attachmentArray)
{
for (int i=0; i<attachmentArray.Length; i++)
{
string sSource = attachmentArray;
if (sSource != "")
{
string sDisplayName = attachmentArray;
int iPosition = (int)mail.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach =
mail.Attachments.Add(sSource,iAttachType,iPosition,sDisplayName);
}
}
}
public void SendMessage()
{
mail.Save();
mail.Send();
}
public void SendVCard(int contactID) {
DataSet ds = new
Contact(GlobalConfiguration.GetInstance().ConnectionString).GetContact(contactID);
contactItem =
(Outlook.ContactItem)outlook.CreateItem(Outlook.OlItemType.olContactItem);
contactItem.Email1Address = ds.Tables[0].Rows[0]["EmailName"].ToString();
contactItem.LastName = ds.Tables[0].Rows[0]["LastName"].ToString();
contactItem.FirstName = ds.Tables[0].Rows[0]["FirstName"].ToString();
contactItem.JobTitle = ds.Tables[0].Rows[0]["Title"].ToString();
contactItem.CompanyName = ds.Tables[0].Rows[0]["CompanyName"].ToString();
contactItem.MailingAddressStreet =
ds.Tables[0].Rows[0]["Address"].ToString() + '\r' +
ds.Tables[0].Rows[0]["Suite"].ToString();
contactItem.MailingAddressCity = ds.Tables[0].Rows[0]["City"].ToString();
contactItem.MailingAddressState =
ds.Tables[0].Rows[0]["StateOrProvince"].ToString();
contactItem.MailingAddressPostalCode =
ds.Tables[0].Rows[0]["PostalCode"].ToString();
contactItem.BusinessTelephoneNumber =
CPUtilities.FormatPhone(ds.Tables[0].Rows[0]["WorkPhone"].ToString());
contactItem.HomeTelephoneNumber =
CPUtilities.FormatPhone(ds.Tables[0].Rows[0]["Home Phone"].ToString());
contactItem.BusinessFaxNumber =
CPUtilities.FormatPhone(ds.Tables[0].Rows[0]["FaxNumber"].ToString());
contactItem.MobileTelephoneNumber =
CPUtilities.FormatPhone(ds.Tables[0].Rows[0]["MobilePhone"].ToString());
if(contactItem != null) {
mail = contactItem.ForwardAsVcard();
}
else {
mail =
(Outlook.MailItem)outlook.CreateItem(Outlook.OlItemType.olMailItem);
}
}
public void AddRecipients(string[] recipients) {
for(int i = 0;i < recipients.Length;i++) {
if(recipients.Length > 0) {
recip = mail.Recipients.Add(recipients);
recip.Type = (int)Outlook.OlMailRecipientType.olBCC;
recip.Resolve();
}
}
}
public void AddRecipients(string[] recipients, bool useTo) {
for(int i = 0;i < recipients.Length;i++) {
if(recipients.Length > 0) {
recip = mail.Recipients.Add(recipients);
recip.Type = (int)Outlook.OlMailRecipientType.olTo;
recip.Resolve();
}
}
}
public void ShowOutlook() {
mail.Display(false);
}
}
Then I have a windows pop up that sends email: the code for that is here:
public class EmailPopup : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox EmailTextBox;
private System.Windows.Forms.Label EmailMessageLabel;
private System.Windows.Forms.TextBox RecipientTextBox;
private System.Windows.Forms.Label RecipientLabel;
private System.Windows.Forms.Button button1;
private int ContactID;
private string connectionString;
private string userName;
//private string[] attachmentArray = new string[100];
//private int attachmentCount = 0;
private System.Windows.Forms.Button AttachmentButton;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.TextBox attachmentsTextBox;
private System.Windows.Forms.Button CloseButton;
private System.Windows.Forms.Label subjectLabel;
private System.Windows.Forms.TextBox subjectTextBox;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public EmailPopup()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
public void setRecipients(string recipients)
{
this.RecipientTextBox.Text = recipients.ToString();
}
public void setContactID(int newContactID)
{
this.ContactID = newContactID;
}
public void setUserName(string user)
{
this.userName = user;
}
public void setConnectionString(string newConnectionString)
{
this.connectionString = newConnectionString;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.EmailTextBox = new System.Windows.Forms.TextBox();
this.EmailMessageLabel = new System.Windows.Forms.Label();
this.RecipientTextBox = new System.Windows.Forms.TextBox();
this.RecipientLabel = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.AttachmentButton = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.CloseButton = new System.Windows.Forms.Button();
this.attachmentsTextBox = new System.Windows.Forms.TextBox();
this.subjectLabel = new System.Windows.Forms.Label();
this.subjectTextBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// EmailTextBox
//
this.EmailTextBox.AcceptsReturn = true;
this.EmailTextBox.AcceptsTab = true;
this.EmailTextBox.AllowDrop = true;
this.EmailTextBox.Location = new System.Drawing.Point(16, 144);
this.EmailTextBox.Multiline = true;
this.EmailTextBox.Name = "EmailTextBox";
this.EmailTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.EmailTextBox.Size = new System.Drawing.Size(568, 216);
this.EmailTextBox.TabIndex = 0;
this.EmailTextBox.Text = "";
//
// EmailMessageLabel
//
this.EmailMessageLabel.Location = new System.Drawing.Point(16, 128);
this.EmailMessageLabel.Name = "EmailMessageLabel";
this.EmailMessageLabel.Size = new System.Drawing.Size(100, 16);
this.EmailMessageLabel.TabIndex = 1;
this.EmailMessageLabel.Text = "Message:";
//
// RecipientTextBox
//
this.RecipientTextBox.Location = new System.Drawing.Point(16, 32);
this.RecipientTextBox.Multiline = true;
this.RecipientTextBox.Name = "RecipientTextBox";
this.RecipientTextBox.ScrollBars =
System.Windows.Forms.ScrollBars.Vertical;
this.RecipientTextBox.Size = new System.Drawing.Size(568, 40);
this.RecipientTextBox.TabIndex = 2;
this.RecipientTextBox.Text = "";
//
// RecipientLabel
//
this.RecipientLabel.Location = new System.Drawing.Point(16, 16);
this.RecipientLabel.Name = "RecipientLabel";
this.RecipientLabel.Size = new System.Drawing.Size(100, 16);
this.RecipientLabel.TabIndex = 3;
this.RecipientLabel.Text = "Recipient list:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(416, 432);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(168, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Send Message";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// AttachmentButton
//
this.AttachmentButton.Location = new System.Drawing.Point(16, 376);
this.AttachmentButton.Name = "AttachmentButton";
this.AttachmentButton.Size = new System.Drawing.Size(104, 23);
this.AttachmentButton.TabIndex = 4;
this.AttachmentButton.Text = "Attachments...";
this.AttachmentButton.Click += new
System.EventHandler(this.AttachmentButton_Click);
//
// CloseButton
//
this.CloseButton.Location = new System.Drawing.Point(288, 432);
this.CloseButton.Name = "CloseButton";
this.CloseButton.Size = new System.Drawing.Size(112, 23);
this.CloseButton.TabIndex = 5;
this.CloseButton.Text = "Cancel";
this.CloseButton.Click += new System.EventHandler(this.CancelButton_Click);
//
// attachmentsTextBox
//
this.attachmentsTextBox.Location = new System.Drawing.Point(136, 376);
this.attachmentsTextBox.Multiline = true;
this.attachmentsTextBox.Name = "attachmentsTextBox";
this.attachmentsTextBox.ScrollBars =
System.Windows.Forms.ScrollBars.Vertical;
this.attachmentsTextBox.Size = new System.Drawing.Size(448, 40);
this.attachmentsTextBox.TabIndex = 6;
this.attachmentsTextBox.Text = "";
//
// subjectLabel
//
this.subjectLabel.Location = new System.Drawing.Point(16, 80);
this.subjectLabel.Name = "subjectLabel";
this.subjectLabel.Size = new System.Drawing.Size(100, 16);
this.subjectLabel.TabIndex = 7;
this.subjectLabel.Text = "Subject";
//
// subjectTextBox
//
this.subjectTextBox.Location = new System.Drawing.Point(16, 96);
this.subjectTextBox.Name = "subjectTextBox";
this.subjectTextBox.Size = new System.Drawing.Size(568, 20);
this.subjectTextBox.TabIndex = 8;
this.subjectTextBox.Text = "";
//
// EmailPopup
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(602, 466);
this.ControlBox = false;
this.Controls.Add(this.subjectTextBox);
this.Controls.Add(this.subjectLabel);
this.Controls.Add(this.attachmentsTextBox);
this.Controls.Add(this.CloseButton);
this.Controls.Add(this.AttachmentButton);
this.Controls.Add(this.button1);
this.Controls.Add(this.RecipientLabel);
this.Controls.Add(this.RecipientTextBox);
this.Controls.Add(this.EmailTextBox);
this.Controls.Add(this.EmailMessageLabel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "EmailPopup";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
try
{
if(this.RecipientTextBox.Text.Trim() != String.Empty)
{
string delimStr = ",;";
char [] delimiter = delimStr.ToCharArray();
string callLog = "";
string[] attachmentArray =
this.attachmentsTextBox.Text.ToString().Split(delimiter,100);
CPOutlook cpo = new CPOutlook();
cpo.CreateMailItem();
callLog += "From: " + System.Environment.UserName.ToString() + "\r\n";
string[] recipients = new string[1] {this.RecipientTextBox.Text};
cpo.AddRecipients(recipients,true);
callLog += "To: ";
for (int i=0;i<recipients.Length;i++)
{
if (i > 0)
callLog += ", ";
callLog += recipients.ToString();
}
callLog += "\r\n";
string emailMessage = this.EmailTextBox.Text.ToString();
if (emailMessage == "")
emailMessage = "Message Body Empty.";
cpo.AddMessage(emailMessage);
cpo.AddSubject(this.subjectTextBox.Text.ToString());
callLog += "Subject: ";
callLog += this.subjectTextBox.Text.ToString();
callLog += "\r\n";
callLog += "Attachments: ";
callLog += this.attachmentsTextBox.Text.ToString();
callLog += "\r\n";
callLog += "Message: ";
string removeBreaksMessage = emailMessage.Replace("\r"," ");
string removeLinesMessage = emailMessage.Replace("\n"," ");
callLog += removeLinesMessage;
cpo.AddAttachments(attachmentArray);
cpo.SendMessage();
Calls.Insert(this.connectionString,this.ContactID,callLog);
//cpo.ShowOutlook();
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AttachmentButton_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
//here goes the code to add an attachment and show it with a
//link to remove it
string filePath = openFileDialog1.FileName.ToString();
this.attachmentsTextBox.Text += filePath;
this.attachmentsTextBox.Text += ";";
//this.attachmentArray[this.attachmentCount] = filePath;
//this.attachmentCount++;
}
}
private void CancelButton_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
This code works fine except for the incessant outlook warning. So, I tried
adding an Outlook Addin to my solution, then I added the code from my outlook
referencing class to that file, and changed my references around, but I was
getting a build error. I'm pretty lost as far as this goes. Thanks for the
help.
Nathan