【问题标题】:Send simple Email ASP.NET MVC, IIS7发送简单电子邮件 ASP.NET MVC, IIS7
【发布时间】:2014-10-22 01:17:03
【问题描述】:

我正在尝试发送一些简单的电子邮件到例如 test@blabla.com

首先,我已经在我的本地主机中尝试过了,它成功了,问题是当我将它上传到我的服务器时

我使用的服务器是 windows server 2012 R2,带有 IIS 7(不太确定版本,但我相信它是 7 及更高版本) 成功托管没有问题,只是发送电子邮件方法不起作用......

我已经添加了 SMTP 功能,设置了 SMTP E-MAIL(是的,在 IIS 7 及以上版本中没有 SMTP 虚拟服务器)

这是我的控制器

public ActionResult SendTestEmail()
{
    var test_address = "test@blabla.com";
    try
    {
        // Initialize WebMail helper
        WebMail.SmtpServer = "localhost";
        WebMail.SmtpPort = 25;   // Or the port you've been told to use
        WebMail.EnableSsl = false;
        WebMail.UserName = "";
        WebMail.Password = "";
        WebMail.From = "admin@testserver.com"; // random email

        WebMail.Send(to: test_address,
            subject: "Test email message",
            body: "This is a debug email message"
        );
    }
    catch (Exception ex)
    {
        ViewBag.errorMessage = ex.Message; // catch error
    }

    return View();
}

这是我的 SMTP 电子邮件设置:

错误信息是:邮箱不可用。服务器响应是:5.7.1 无法为 test@blabla.com 中继(当我将电子邮件地址文本框留空时会发生这种情况,当我输入任何电子邮件格式时也会发生这种情况,例如 sample@email.com /我的主要电子邮件)

【问题讨论】:

  • 这通常是由不正确的登录信息引起的。是否为 smtp 指定了用户?
  • hmmm... 你的意思是我用来访问服务器的用户名和密码吗?如果我有这种情况,但对于 smtp,我想我没有..
  • 是的,有时需要根据设置指定域用户。也看看这个:stackoverflow.com/questions/3165721/…
  • Erm,当您执行 WebMail.Send(to: test_address 时,您正在设置 test@blabla.com,所以不要惊讶它总是试图发送给它......猜猜是什么......它不存在!
  • @PaulZahra:test@blabla.com 只是这里的一个示例,我实际上已经将它发送到我存在的辅助电子邮件中......

标签: c# asp.net-mvc email iis smtp


【解决方案1】:

您需要将您的 smtp 设置设置为真正的 smtp 服务器....

Try some smtp servers off this list 如果您无法访问自己的...

这里有一段不错的代码...Taken from here

MailModel.cs

public class MailModel
{
    public string From { get; set; }
    public string To { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

SendMailerController.cs - 注意声明 smtp 设置的位置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc; 

namespace SendMail.Controllers
{
    public class SendMailerController : Controller
    {
        // GET: /SendMailer/ 
        public ActionResult Index()
        {
            return View();
        } 

        [HttpPost]
        public ViewResult Index(SendMail.Models.MailModel _objModelMail)
        {
            if (ModelState.IsValid)
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(_objModelMail.To);
                mail.From = new MailAddress(_objModelMail.From);
                mail.Subject = _objModelMail.Subject;
                string Body = _objModelMail.Body;
                mail.Body = Body;
                mail.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential
                ("username", "password");// Enter senders User name and password
                smtp.EnableSsl = false;
                smtp.Send(mail);

                return View("Index", _objModelMail);
            }
            else
            {
                return View();
            }
        }
    }
}

Index.cshtml

@model SendMail.Models.MailModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<fieldset>
    <legend>Send Email</legend>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary()
        <p>From: </p>
        <p>@Html.TextBoxFor(m=>m.From)</p>
        <p>To: </p>
        <p>@Html.TextBoxFor(m=>m.To)</p>
        <p>Subject: </p>
        <p>@Html.TextBoxFor(m=>m.Subject)</p>
        <p>Body: </p>
        <p>@Html.TextAreaFor(m=>m.Body)</p>
        <input type ="submit" value ="Send" />
    }
</fieldset>

更新 How to setup SMTP server on IIS7

  1. 开始->管理工具->服务器管理器,进入功能,选择“添加功能”,勾选“smtp服务器”(如果尚未安装),选择安装所需的“远程服务器管理工​​具” "

  2. 检查以确认“简单邮件传输协议 (SMTP)”服务正在运行,如果是,我们就可以开始了。

  3. 开始->管理工具>internet info services(iis) 6.0

  4. 确保 SMTP 虚拟服务器/默认 smtp 服务器正在运行,如果没有,请右键单击,然后选择“开始”

  5. 在IIS7中,进入网站/虚拟目录,双击“SMTP E-mail”,点击“Deliver e-mail to SMTP server”,勾选“Use localhost”勾

    李>

【讨论】:

  • 非常感谢您的回答,我已经尝试了这些方法并且它有效,但是它使用 gmail smtp,我想使用我的服务器 smtp...我不知道我的服务器是否实际上可以有或没有 smtp 服务器...无论如何谢谢 :D
  • 但我以为你说你没有 smtp 服务器的详细信息?
  • 抱歉回复晚了,在和我的经理讨论后,他同意使用gmail smtp,但临时使用......我使用你的代码,按照所有步骤但是,异常返回: SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。了解更多...
  • @user3848402 基本上...您需要一个 gmail 帐户才能发送来自...accounts.google.com/…
  • 对不起,因为周末我终于休假了。我试过你的方法,我认为问题出在我的服务器上,我试过我公司的 smtp,它实际上发送了电子邮件但很长一段时间,大约 2 小时......谢谢保罗,我用过你的方法:D
【解决方案2】:

为什么不尝试使用 gmail 作为电子邮件发件人?它易于使用。

我总是只构建简单的方法

public void SendEmail()
{
    MailMessage mail = new MailMessage("xxx@gmail.com", "sendTo", "mailSubject", "mailBody");
    mail.From = new MailAddress("xxx@gmail.com", "nameEmail");
    mail.IsBodyHtml = true; // necessary if you're using html email

    NetworkCredential credential = new NetworkCredential("xxx@gmail.com", "xxxxx");
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = credential;
    smtp.Send(mail);
}

如果您想等待电子邮件发送,只需使用async/await

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2011-10-27
    • 2020-09-11
    相关资源
    最近更新 更多