我如何从Windows应用程序通过ms Outlook发送电子邮件 [英] how do i send emails via ms outlook from my windows application

查看:106
本文介绍了我如何从Windows应用程序通过ms Outlook发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过ms Outlook从Windows应用程序发送电子邮件.我面临的问题是,我需要先打开Outlook,然后才能发送邮件.无论ms Outlook是否正在运行,有没有一种我可以发送邮件的方法?我的代码如下所示:

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.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());
}

推荐答案



如果您正在使用Outlook(通过互操作),则Outlook实例必须正在运行(或由互操作启动).

如果要发送不带Outlook的电子邮件,则可以使用基于SMTP的发送.请参阅: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx [ ^ ]
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[^]


我面临的问题是,我需要先打开Outlook才能发送邮件.无论ms Outlook是否正在运行,有没有一种我可以发送邮件的方式
AFAIK,答案是否定的.要运行Outlook,必须先运行它.如果不是这样,将自动发生的第一步是打开Outlook,然后使用它的功能.

通过Winforms中的Outlook通过邮件休息:发送邮件带有或不带有附件的C#电子邮件:通用例程. [
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.[^]


这有望对您有所帮助:
MAPIEx:扩展MAPI包装器 [
This will hopefully help:
MAPIEx: Extended MAPI Wrapper[^]

Regards
Espen Harlinn


这篇关于我如何从Windows应用程序通过ms Outlook发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆