我有一个asp.net
网页表单,点击按钮发送后,我希望将电子邮件发送到我的Microsoft Outlook电子邮件帐户。我在其他
网站,但使用的是Hotmail,但所有外部网络电子邮件提供商都被公司防火墙阻止(因为我尝试设置了一个Gmail帐户),所以
我需要使用Outlook,但我不知道如何实现这一点,我在谷歌上看到的解决方案似乎不起作用。我不知道它是否会成为
差异与否,但我被告知用户密码每隔30天epxires所以我怀疑我需要使用Windows身份验证或其他东西但不确定。
我不确定Outlook如何发送电子邮件,因为我从过去使用hotmail的经验中知道电子邮件只是点击按钮发送但我
不确定outlook是否会打开电子邮件窗口以供用户单击“发送”按钮。如果是这样,我需要在webform上捕获的信息
包含电子邮件和电子邮件正文的内容不得更改(如果可以这样做,再次不确定是否可以但不是问题
不能)。
以下是我在尝试使用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
}
答案 0 :(得分:1)
编辑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 interoperlation
答案 1 :(得分:1)
使用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();
}
}