如何从用户的Outlook邮箱发送邮件

时间:2014-03-03 16:56:20

标签: c# asp.net

您好我知道如何通过以下mailcode从smpt发送邮件。但是没有得到任何关于如何从用户的outlook.so发送邮件的线索,用户可以在他的已发送邮件文件夹中找到他的邮件 请帮帮我..

以下是发送邮件的网络配置代码

            <mailSettings>
        <smtp>
            <network host="11.111.111.1" port="25" defaultCredentials="true"/>
        </smtp>
    </mailSettings>

这是我的发送邮件方式:

        public static void SendMessage(string sbj, string bd, string bccadd, string    attachFile1,string buyeremail)
    {
        MailMessage message = new MailMessage();
        SmtpClient client = new SmtpClient();
        message.From = new MailAddress(buyeremail);
        message.To.Add(new MailAddress(bccadd));
        message.Subject = sbj.Trim();
        message.Body = bd.Trim();
        SmtpClient mysmptclient = new SmtpClient();
        mysmptclient.DeliveryMethod = SmtpDeliveryMethod.Network;


            message.Attachments.Add(new Attachment(attachFile1));
            try
            {
                message.Attachments.Add(new Attachment(attachFile5));
            }
            catch
            {
            }
                mysmptclient.Send(message);

    }

我刚修改了我的代码如下:

        try
        {


            Outlook.Application oApp = new Outlook.Application();
            Outlook.NameSpace oNamespace = new Outlook.NameSpace("MAPI");
            Outlook.MailItem oMailItem =  (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
            oMailItem.HTMLBody = bd.Trim();

            oMailItem.Subject = sbj.Trim();
            Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients;
            Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(bccadd);
            oRecip.Resolve();
            oMailItem.Send();
            oRecip = null;
            oRecips = null;
            oMailItem = null;
            oApp = null;
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message + "');</script>");
            //string script = "<script>alert('" + ex.Message + "');</script>";

        }

现在它显示错误: 由于以下错误,检索具有CLSID {0006F03A-0000-0000-C000-000000000046}的组件的COM类工厂失败:80070005访问被拒绝。 (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))。 请帮帮我

2 个答案:

答案 0 :(得分:3)

您可以使用Microsoft Exchange Web服务(EWS)托管API来创建和发送电子邮件。

http://msdn.microsoft.com/en-us/library/office/dd633628(v=exchg.80).aspx

MSDN上显示的代码:

// Create an email message and identify the Exchange service.
EmailMessage message = new EmailMessage(service);

// Add properties to the email message.
message.Subject = "Interesting";
message.Body = "The merger is finalized.";
message.ToRecipients.Add("user1@contoso.com");

// Send the email message and save a copy.
message.SendAndSaveCopy();

答案 1 :(得分:-1)

我相信您需要使用outlook API来执行此操作,因为MailObject和SMTP将使用上述参数在内部发送邮件,这样的内容应该有助http://www.codeproject.com/Tips/165548/C-Code-snippet-to-send-an-Email-with-attachment-fr

可能重复:Can only send email via Outlook if Outlook is open