C# 无法使用SMTP服务器和AJAX发送邮件
我是asp.NETMVC新手。我正在尝试在asp.NETMVC中使用SMTP服务器和AJAX发送带有附件的邮件。邮件无法发送 HomeController.csC# 无法使用SMTP服务器和AJAX发送邮件,c#,ajax,asp.net-mvc,smtp,outlook-2007,C#,Ajax,Asp.net Mvc,Smtp,Outlook 2007,我是asp.NETMVC新手。我正在尝试在asp.NETMVC中使用SMTP服务器和AJAX发送带有附件的邮件。邮件无法发送 HomeController.cs public JsonResult SendMailToUser() { bool result = false; result = SendEmail("Receiver_mail", "Test", "<p>Hi abc,<br/>This message is for
public JsonResult SendMailToUser()
{
bool result = false;
result = SendEmail("Receiver_mail", "Test", "<p>Hi abc,<br/>This message is for testing purpose. So don't be upset.<br/>Kind Regards,<br/>abc</p>");
return Json(result, JsonRequestBehavior.AllowGet);
}
public bool SendEmail(string toEmail, string subject, string emailBody)
{
try
{
string senderEmail = System.Configuration.ConfigurationManager.AppSettings["Sender_Mail.com"].ToString();
string senderPassword = System.Configuration.ConfigurationManager.AppSettings["Outlook@123"].ToString();
SmtpClient client = new SmtpClient("smtp-mail.outlook.com", 587);
client.EnableSsl = false;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(senderEmail, senderPassword);
MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = UTF8Encoding.UTF8;
client.Send(mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
public static Microsoft.Office.Interop.Outlook.Application GetActiveOutlookApplication()
{
return (Microsoft.Office.Interop.Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
}
public void SendMailToUser()
{
try
{
Outlook.Application app = new Outlook.Application();
Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Subject = "Test mail";
mail.Body = "Test";
Outlook.AddressEntry currentUser = app.Session.CurrentUser.AddressEntry;
if (currentUser.Type == "EX")
{
Outlook.ExchangeUser user = currentUser.GetExchangeUser();
// Add recipient using display name, alias, or smtp address
mail.Recipients.Add(user.PrimarySmtpAddress);
mail.Recipients.ResolveAll();
mail.Attachments.Add(@"URL", Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
mail.Send();
}
}
catch (Exception ex)
{
}
}
public JsonResult SendMailToUser()
{
布尔结果=假;
结果=发送电子邮件(“收件人邮件”、“测试”和“Hi abc,
此消息用于测试目的。所以不要心烦意乱。
亲切问候,
abc”;
返回Json(结果,JsonRequestBehavior.AllowGet);
}
public bool sendmail(字符串到邮件、字符串主题、字符串emailBody)
{
尝试
{
字符串senderEmail=System.Configuration.ConfigurationManager.AppSettings[“Sender_Mail.com”].ToString();
字符串senderPassword=System.Configuration.ConfigurationManager.AppSettings[“Outlook@123“].ToString();
SmtpClient=newsmtpclient(“smtpmail.outlook.com”,587);
client.enablesl=false;
client.Timeout=100000;
client.DeliveryMethod=SmtpDeliveryMethod.Network;
client.UseDefaultCredentials=false;
client.Credentials=新的网络凭据(senderEmail、senderPassword);
MailMessage MailMessage=新邮件(senderEmail、toEmail、subject、emailBody);
mailMessage.IsBodyHtml=true;
mailMessage.BodyEncoding=UTF8Encoding.UTF8;
client.Send(mailMessage);
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
Layout.cshtml
<button type="submit" id ="export" onclick="SendEmail()" class="Layoutbuttons" style="vertical-align:middle"><span>get report via mail</span></button>
<script>
var SendEmail = function () {
$.ajax({
type: "Post",
url: "/Home/SendMailToUser",
success: function (data) {
alert("Success");
}
})
}
</script>
<button type="submit" id ="export" onclick="SendEmail()" class="Layoutbuttons" style="vertical-align:middle"><span>Get report via mail</span></button>
<script>
var SendEmail = function () {
$.ajax({
type: "Post",
url: '@Url.Action("SendMailToUser", "Home")',
success: function () {
alert("Mail sent successfully!");
}
})
}
</script>
通过邮件获取报告
var sendmail=函数(){
$.ajax({
类型:“Post”,
url:“/Home/SendMailToUser”,
成功:功能(数据){
警惕(“成功”);
}
})
}
Web.config
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="SenderEmail" value="Sender_Mail" />
<add key="SenderPassword" value="Outlook@123" />
</appSettings>
在Outlook 2007设置中是否需要执行任何操作?outlook 2007在Exchange server上运行 谢谢你的建议!我找到了以下解决方案。它在localhost中工作得很好,但在发布后失败!请建议一条出路 HomeController.cs
public JsonResult SendMailToUser()
{
bool result = false;
result = SendEmail("Receiver_mail", "Test", "<p>Hi abc,<br/>This message is for testing purpose. So don't be upset.<br/>Kind Regards,<br/>abc</p>");
return Json(result, JsonRequestBehavior.AllowGet);
}
public bool SendEmail(string toEmail, string subject, string emailBody)
{
try
{
string senderEmail = System.Configuration.ConfigurationManager.AppSettings["Sender_Mail.com"].ToString();
string senderPassword = System.Configuration.ConfigurationManager.AppSettings["Outlook@123"].ToString();
SmtpClient client = new SmtpClient("smtp-mail.outlook.com", 587);
client.EnableSsl = false;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(senderEmail, senderPassword);
MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = UTF8Encoding.UTF8;
client.Send(mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
public static Microsoft.Office.Interop.Outlook.Application GetActiveOutlookApplication()
{
return (Microsoft.Office.Interop.Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
}
public void SendMailToUser()
{
try
{
Outlook.Application app = new Outlook.Application();
Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Subject = "Test mail";
mail.Body = "Test";
Outlook.AddressEntry currentUser = app.Session.CurrentUser.AddressEntry;
if (currentUser.Type == "EX")
{
Outlook.ExchangeUser user = currentUser.GetExchangeUser();
// Add recipient using display name, alias, or smtp address
mail.Recipients.Add(user.PrimarySmtpAddress);
mail.Recipients.ResolveAll();
mail.Attachments.Add(@"URL", Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
mail.Send();
}
}
catch (Exception ex)
{
}
}
Layout.cshtml
<button type="submit" id ="export" onclick="SendEmail()" class="Layoutbuttons" style="vertical-align:middle"><span>get report via mail</span></button>
<script>
var SendEmail = function () {
$.ajax({
type: "Post",
url: "/Home/SendMailToUser",
success: function (data) {
alert("Success");
}
})
}
</script>
<button type="submit" id ="export" onclick="SendEmail()" class="Layoutbuttons" style="vertical-align:middle"><span>Get report via mail</span></button>
<script>
var SendEmail = function () {
$.ajax({
type: "Post",
url: '@Url.Action("SendMailToUser", "Home")',
success: function () {
alert("Mail sent successfully!");
}
})
}
</script>
通过邮件获取报告
var sendmail=函数(){
$.ajax({
类型:“Post”,
url:'@url.Action(“SendMailToUser”,“Home”),
成功:函数(){
警报(“邮件已成功发送!”);
}
})
}
您的问题是什么?有什么例外吗?哪一个?哪一行代码?您是否调查Exchange日志文件?是否存在任何错误/警告?他们有任何代码或其他东西,我们可以用谷歌搜索吗?我在点击按钮后在网页上收到了一条警告信息,称为“成功”,但收件人端没有收到邮件。alert(“Success”)如果服务器响应状态代码为200,则将显示代码>。您的控制器操作SendMailToUser
始终返回状态代码200,如我所见。再说一遍:你的问题是什么?有什么例外吗?哪一个?哪一行代码?您是否调查Exchange日志文件?是否存在任何错误/警告?他们有任何代码或其他东西,我们可以用谷歌搜索吗?@Nelson Kingsley,你是否尝试将邮件发送到其他电子邮件地址而不是outlook?只需检查你的发送邮件代码是否正常