使用 Microsoft Outlook 从 Asp.Net Webform 应用程序发送电子邮件

Send Email from an Asp.Net Webform Application using Microsoft Outlook

我有一个 asp.net 网络表单,点击按钮发送我想将一封电子邮件发送到我的 Microsoft Outlook 电子邮件帐户。我在其他

上做过

网站,但它使用的是 hotmail,但所有外部网络电子邮件提供商都被公司防火墙阻止(因为我尝试设置一个 gmail 帐户)所以

我需要使用 Outlook,但我不知道如何实施,我在 Google 上看到的解决方案似乎不起作用。我不知道它是否会成为

区别与否,但我被告知用户密码每 30 天更新一次,所以我怀疑我需要使用 windows 身份验证或其他东西

但不确定。

我不确定 Outlook 如何发送电子邮件,因为我从过去使用 hotmail 的经验中知道,电子邮件只是在单击按钮时发送,但我

不确定 outlook 是否会打开电子邮件 window 供用户单击发送按钮。如果是这样,我需要在网络表单上捕获的信息是

包含在电子邮件中且电子邮件正文的内容不可更改(如果可以,再次不确定是否可以,但如果可以,则没有问题

不能)。

下面是我尝试使用 gmail 时使用的代码,但正如我所说,我被告知不允许这样做。

using System.Configuration;
using System.Net.Mail;
using System.Net;
using System.IO;

protected void BtnSuggestPlace_Click(object sender, EventArgs e)
        {
            #region Email
            try
            {
                //Creates the email object to be sent
                MailMessage msg = new MailMessage();

                //Adds your email address to the recipients
                msg.To.Add("MyEmailAddress@Test.co.uk");

                //Configures the address you are sending the email from
                MailAddress address = new MailAddress("EmailAddress@Test.com");
                msg.From = address;

                //Allows HTML to be used when setting up the email body
                msg.IsBodyHtml = true;

                //Email subjects title
                msg.Subject = "Place Suggestion";

                msg.Body = "<b>" + lblPlace.Text + "</b>" + "&nbsp;" + fldPlace.Text
                           + Environment.NewLine.ToString() +
                           "<b>" + lblLocation.Text + "</b>" + "&nbsp;" + fldLocation.Text
                           + Environment.NewLine.ToString() +
                           "<b>" + lblName.Text + "</b>" + "&nbsp;" + fldName.Text;

                //Configures the SmtpClient to send the mail
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.EnableSsl = true; //only enable this if the provider requires it

                //Setup credentials to login to the sender email address ("UserName", "Password")
                NetworkCredential credentials = new NetworkCredential("MyEmailAddress@Test.co.uk", "MyPassword");
                client.Credentials = credentials;

                //Send the email
                client.Send(msg);
            }
            catch
            {
                //Lets the user know if the email has failed
                lblNotSent.Text = "<div class=\"row\">" + "<div class=\"col-sm-12\">" + "There was a problem sending your suggestion. Please try again." 

+ "</div>" + "</div>" + "<div class=\"form-group\">" + "<div class=\"col-sm-12\">" + "If the error persists, please contact Antony." + "</div>" + 

"</div>";
            }
            #endregion
}

编辑 2:现在我们已经确定了您的经历,这就是我的代码一直以来的工作方式

    SmtpClient sptmClient = new SmtpClient("exchange server name")
MailMessage m = new MailMessage();
m.To.Add(new MailAddress("Address"));
m.From = new MailAddress("");
m.Subject = "";
m.Body = "";
m.IsBodyHtml = true;
sptmClient.Send(m);

但这里还有另一个答案,它使用 outlook 互操作,可能更适合您

对于 Exchange 它必须有效。

测试一下:

using Outlook = Microsoft.Office.Interop.Outlook;

 private void SendWithExchange()
            {

                Outlook.Application oApp = new Outlook.Application();
                Outlook.MailItem mail = oApp.CreateItem(
                    Outlook.OlItemType.olMailItem) as Outlook.MailItem;
                mail.Subject = "Exemple à tester";
                Outlook.AddressEntry currentUser =
                    oApp.Session.CurrentUser.AddressEntry;
                if (currentUser.Type == "EX")
                {
                    Outlook.ExchangeUser manager =
                        currentUser.GetExchangeUser();
                    mail.Recipients.Add(manager.PrimarySmtpAddress);
                    mail.Recipients.ResolveAll();
                    //mail.Attachments.Add(@"c:\sales reports\fy06q4.xlsx",
                    //    Outlook.OlAttachmentType.olByValue, Type.Missing,
                    //    Type.Missing);
                    mail.Send();
                }
            }