Click here to Skip to main content
15,924,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

MSIL
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.Text = "";
        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());
}
Posted

Hi,

If you're utilizing Outlook (via Interop) an instance of outlook must be running (or started by interop).

If you want to send an email without outlook, you could use SMTP based sending. See: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx[^]
 
Share this answer
 
Comments
Espen Harlinn 10-Apr-11 11:59am    
Good advice - 5ed!
Wendelius 10-Apr-11 12:59pm    
Thanks :)
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
AFAIK, the answer is No. Outlook has to be running in order to do it. In case it is not, first step would that would happen automatically is to open the Outlook and then use it's functionality.

Rest for mail via outlook in Winforms: Sending an Email in C# with or without attachments: generic routine.[^]
 
Share this answer
 
Comments
Espen Harlinn 10-Apr-11 12:01pm    
Good link - 5ed!
For a Royal Griff : http://www.elfwood.com/~kent/2005-Royal-griff.2905040.html
Sandeep Mewara 10-Apr-11 12:04pm    
Thanks.... That looks like a 'Buckbeak of Harry Potter' to me.
Espen Harlinn 10-Apr-11 12:17pm    
Possibly - Griffins have been around for quite some time :)
http://en.wikipedia.org/wiki/Griffin
Sandeep Mewara 10-Apr-11 12:39pm    
Yeah... :)
This will hopefully help:
MAPIEx: Extended MAPI Wrapper[^]

Regards
Espen Harlinn
 
Share this answer
 
Comments
Wendelius 10-Apr-11 13:01pm    
Good link, 5'd
Espen Harlinn 10-Apr-11 14:05pm    
Thanks Mika!
Espen Harlinn 10-Apr-11 14:23pm    
I guess you forgot to click on the vote "bar" :)
Wendelius 10-Apr-11 14:43pm    
Sorry, I thought I did... Well should be ok by now. If you don't see the vote, let me know. Sorry again!
Espen Harlinn 10-Apr-11 14:45pm    
Brilliant, thanks - just remember not to be miffed if I ever do the same - not that I would do so on purpose but it has happened on a few occasions, just send me a note and I'll fix it too :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900