[英]Send Email from an Asp.Net Webform Application using Microsoft Outlook
我有一个asp.net
Web表单,单击按钮即可发送,我希望将电子邮件发送到我的Microsoft Outlook电子邮件帐户。 我在其他方面做到了
网站,但使用的是hotmail,但所有外部网络电子邮件提供商均被公司防火墙阻止(因为我尝试设置gmail帐户)
我需要使用Outlook,但我不知道如何实施该解决方案,而我在Google上看到的解决方案似乎也不起作用。 我不知道它会不会
差异与否,但我已经得知用户密码每30天出现一次,因此我怀疑我需要使用Windows身份验证或其他功能
但不确定。
我不确定Outlook如何发送电子邮件,如我从使用hotmail的以往经验中所知道的那样,只需单击按钮即可发送电子邮件,但是我
不知道Outlook是否会打开电子邮件窗口供用户单击“发送”按钮。 如果是这样,我需要在网络表单上捕获的信息是
电子邮件中包含的内容以及电子邮件正文的内容均不得更改(如果可以这样做,请再次确认是否可以更改,但不能确定是否存在问题)
不能)。
下面是我尝试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>" + " " + fldPlace.Text
+ Environment.NewLine.ToString() +
"<b>" + lblLocation.Text + "</b>" + " " + fldLocation.Text
+ Environment.NewLine.ToString() +
"<b>" + lblName.Text + "</b>" + " " + 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.