I want to send emails from my windows application through ms outlook. The problem I am facing is that, i need to have outlook open before the mails can be sent. Is there a way i can send the mails regardless of whether ms outlook is running or not? my code is show below:
exceptionhandling validator = new exceptionhandling()
try
{
OutLook.Application outlookObj = new OutLook.Application()
OutLook.MailItem Mail = (OutLook.MailItem)outlookObj.CreateItem(OutLook.OlItemType.olMailItem)
if (cbTo.Text == "")
{
MessageBox.Show("Please enter receipient's email address", "Forward Applicant", MessageBoxButtons.OK, MessageBoxIcon.Question)
cbTo.Focus()
return
}
validator.IsEmail(cbTo.Text)
if (cbCopy.Text != "")
{
validator.IsEmail(cbCopy.Text)
}
if (tbSubject.Text == "")
{
DialogResult rlst = MessageBox.Show("Do you want to send this email without a subject", "Forward Applicant", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
if (rlst == DialogResult.No)
{
tbSubject.Focus()
return
}
}
validator.IsAlphaNumeric(tbSubject.Text)
if (rtbMsg.Text == "")
{
DialogResult rlst = MessageBox.Show("Do you want to send an empty email", "Forward Applicant", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
if (rlst == DialogResult.No)
{
rtbMsg.Focus()
return
}
}
validator.IsAlphaNumeric(rtbMsg.Text)
Mail.To = cbTo.Text
Mail.CC = cbCopy.Text
Mail.Subject = tbSubject.Text
Mail.Body = rtbMsg.Text
if (tbAttchmnt.Text != "")
{
Mail.Attachments.Add(tbAttchmnt.Text, (int)OutLook.OlAttachmentType.olByValue, (int)Mail.Body.Length + 1, "Attached File")
}
Mail.Send()
MessageBox.Show("Applicant has been forwarded", "Forward Applicant", MessageBoxButtons.OK, MessageBoxIcon.Information)
}
catch (ArgumentException)
{
DialogResult rslt = MessageBox.Show("Please a valid email address", "Email Problem", MessageBoxButtons.OK, MessageBoxIcon.Error)
if (rslt == DialogResult.OK)
{
cbTo.Focus()
}
}
catch (FormatException)
{
DialogResult rslt = MessageBox.Show("Please enter only alphanumeric characters in subject and message body", "Email Problem", MessageBoxButtons.OK, MessageBoxIcon.Error)
if (rslt == DialogResult.OK)
{
tbSubject.Focus()
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString())
}