[英]Error in sending email in C# asp.net
我已經開始構建我的“聯系我們”頁面,該頁面使用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
我的代碼是:
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();
}
}
您能幫助我理解Access is denied
/ UnauthorizedAccessException以及如何解決嗎?
您可以只使用MailMessage類。 我舉一個例子。
創建一個類,然后將其稱為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);
}
}
}
如您所見,在smtpClient.Credentials
和smtpClient
,我使用了一個GMail帳戶。 這是因為它們提供了用於發送電子郵件的免費端口。 它是免費的,不用擔心。
現在,您可以從主頁上調用該類。
MailHelper m = new MailHelper();
m.SendingEmail(<subject>, <body>);
就是這樣。 這就是您如何通過C#代碼發送電子郵件的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.