在C#asp.net中发送电子邮件时出错

时间:2022-07-21 01:41:11

I have started building my "Contact Us" page that sends email using outlook interop. It throws this exception:

我已经开始构建我的“联系我们”页面,该页面使用outlook interop发送电子邮件。它抛出了这个异常:

System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} 
failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). 
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType) 
at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType) 
at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, 
Object[] props, Boolean bNewObj) at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, 
Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, 
Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, 
Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at 
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean 
fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean
nonPublic) at System.Activator.CreateInstance(Type type) at 
OnlinePayslip.Forms.Support.sendEMailThroughOUTLOOK() in D:\OnlinePayslip\OnlinePayslip\OnlinePayslip\Forms\Support.aspx.cs:line 42

My code is:

我的代码是:

using Outlook = Microsoft.Office.Interop.Outlook;
using System.Net.Mail;

public void sendEMail()
{
    try
    {
        // Create the Outlook application.
        Outlook.Application oApp = new Outlook.Application();

        // Create a new mail item.
        Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

        // Set HTMLBody. 
        //add the body of the email
        oMsg.HTMLBody = txtEmailBody.Text + "</br> From " + Session["User"].ToString();
        int iPosition = (int)oMsg.Body.Length + 1;               
        oMsg.Subject = "Email Alert";
        Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
        Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("kim@npc.com.sa");
        oRecip.Resolve();
        oMsg.Send();                   
        oRecip = null;
        oRecips = null;
        oMsg = null;
        oApp = null;
        lblMsg.Text = "Email Sent";
    }
    catch (Exception ex)
    {
        lblMsg.Text = ex.ToString();
    }
}

Can you help me understand the Access is denied/UnauthorizedAccessException and how I can resolve this?

你能帮我理解Access被拒绝/ UnauthorizedAccessException以及我如何解决这个问题?

1 个解决方案

#1


3  

You can just use the MailMessage class. I'll give you an example.

您可以使用MailMessage类。我给你举个例子。

Create a class then call it MailHelper. Then paste this code.

创建一个类,然后将其称为MailHelper。然后粘贴此代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;

namespace SampleEMailSender
{
    public class MailHelper
    {

        public void SendingEmail(string subject, string body) {

            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add("<email to send>");
            mailMessage.From = new MailAddress("<email to use in sending>");
            mailMessage.Subject = subject; //the email subject
            mailMessage.Body = body; //the email body
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential("<gmail account>", "<gmail password>");
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Send(mailMessage);
        }
    }
}

As you can see, on the smtpClient.Credentials and smtpClient, I used a GMail account. It is because they provide free port to use in sending emails. It's free, don't worry.

如您所见,在smtpClient.Credentials和smtpClient上,我使用了GMail帐户。这是因为它们提供了用于发送电子邮件的免费端口。它是免费的,不用担心。

Now you can call this class from your main page.

现在,您可以从主页面调用此类。

MailHelper m = new MailHelper();

m.SendingEmail(<subject>, <body>);

And that's it. That how you can send email through your C# code.

就是这样。您可以通过C#代码发送电子邮件。

#1


3  

You can just use the MailMessage class. I'll give you an example.

您可以使用MailMessage类。我给你举个例子。

Create a class then call it MailHelper. Then paste this code.

创建一个类,然后将其称为MailHelper。然后粘贴此代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;

namespace SampleEMailSender
{
    public class MailHelper
    {

        public void SendingEmail(string subject, string body) {

            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add("<email to send>");
            mailMessage.From = new MailAddress("<email to use in sending>");
            mailMessage.Subject = subject; //the email subject
            mailMessage.Body = body; //the email body
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential("<gmail account>", "<gmail password>");
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Send(mailMessage);
        }
    }
}

As you can see, on the smtpClient.Credentials and smtpClient, I used a GMail account. It is because they provide free port to use in sending emails. It's free, don't worry.

如您所见,在smtpClient.Credentials和smtpClient上,我使用了GMail帐户。这是因为它们提供了用于发送电子邮件的免费端口。它是免费的,不用担心。

Now you can call this class from your main page.

现在,您可以从主页面调用此类。

MailHelper m = new MailHelper();

m.SendingEmail(<subject>, <body>);

And that's it. That how you can send email through your C# code.

就是这样。您可以通过C#代码发送电子邮件。