-1

所以我有一个数据库程序,MVC4,C#,发布到 Azure。

代理将在数据库中创建记录,然后客户端将查看它们。记录只能由代理创建或编辑。

我遇到的问题是邮件功能。我想要一个功能,当代理点击保存时,无论是在编辑还是创建之后,都会向客户端发送一封电子邮件,引用记录。该电子邮件将包含一个记录定位器。客户电子邮件和记录定位器是每条记录中的项目。

我把表格弄好了,但是控制器和 System.Main.Net 有问题。我的编辑控制器代码如下。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TrepPortal.Models;
using System.Net.Mail;

    //
    // GET: /AgntActions/Edit/5

    public ActionResult Edit(int id = 0)
    {
        ClientActions clientactions = db.ClientActions.Find(id);
        if (clientactions == null)
        {
            return HttpNotFound();
        }
        return View(clientactions);
    }

    //
    // POST: /AgntActions/Edit/5


    [HttpPost]
    public ActionResult Edit(ClientActions clientactions)
    {
        if (ModelState.IsValid)
        {
            db.Entry(clientactions).State = EntityState.Modified;
            db.SaveChanges();

            //mail function
            MailMessage message = new MailMessage();
            message.From = new MailAddress("nick@cooktravel.net");
            //message.To.Add(new MailAddress("nick@cooktravel.net"));
            message.To.Add(clientactions.ClientEmail);
            message.Subject = "This is my subject";
            message.Body = "Your Cook Travel reservation has been updated. The record locator is "clientactions.SabreLocator" . Please visit cooktravelportal.net to view it.";

            //SmtpClient client = new SmtpClient("smtp.cooktravel.net, 495");//old
            SmtpClient client = new SmtpClient("smtp.cooktravel.net", 25);//

            client.UseDefaultCredentials = false;
            client.EnableSsl = false;
            client.Credentials = new System.Net.NetworkCredential("nick@cooktravel.net", "Password");
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            client.Send(message); //on debug, error comes here as SmtpException unhandled

            return RedirectToAction("Index");
        }
        return View(clientactions);
    }

现在我没有很多经验。我通过阅读许多文章和示例将其放在一起。另外,如果我遗漏了任何 using 语句,请告诉我。任何反馈表示赞赏。谢谢!

27SEP13 评论:“用户代码未处理 SmptException。” “邮件发送失败。” 此错误发生在上面标记的“client.Send(message)”行的调试中 好的,由于当前的答案,我更改了部分代码以正确指示上面标记的端口,但现在我收到以下错误。

[SocketException (0x271d): An attempt was made to access a socket in a way forbidden by its   access permissions 66.179.170.244:25]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +208
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +464

[WebException: Unable to connect to the remote server]
System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async,   IPAddress& address, Socket& abortSocket, Socket& abortSocket6) +6470692
System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) +307
System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +19
System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +324
System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +141
System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) +170
System.Net.Mail.SmtpClient.GetConnection() +44
System.Net.Mail.SmtpClient.Send(MailMessage message) +1554

"

4

5 回答 5

3

也许你需要修改这行代码:

SmtpClient client = new SmtpClient("smtp.cooktravel.net, 495");

SmtpClient client = new SmtpClient("smtp.cooktravel.net",495);
于 2013-09-27T05:24:49.347 回答
1

您可以尝试更改:

  SmtpClient client = new SmtpClient("smtp.cooktravel.net, 495");

到:

SmtpClient client = new SmtpClient("smtp.cooktravel.net");
client.Port = 495;

或者您可以显示使用异常详细信息吗?

于 2013-09-27T05:24:07.400 回答
0
view 
<% using(Html.BeginForm("Sendlink", "Home")) %>
    <% { %>
     <input type="text" id="toemail" value="" />
        <input type="submit" value="Send" />
    <% } %>
Controller
public ActionResult Sendlink()
{
    return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Sendlink(FormCollection formCollection)
{
    try
    {
        string message = Session["link"].ToString();
        string toemail = formCollection["toemail"];
        MailEngine.Send("mail@mail.com", toemail, "link", message);
        return RedirectToAction("CanvasShare");
    }
    catch
    {

    }
    return null;
}

Code (Model)




I am trying to send an email inside an action. However, the action always returns a blank screen.

View:

<% using(Html.BeginForm("Sendlink", "Home")) %>
    <% { %>
     <input type="text" id="toemail" value="" />
        <input type="submit" value="Send" />
    <% } %>

Controller:

public ActionResult Sendlink()
{
    return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Sendlink(FormCollection formCollection)
{
    try
    {
        string message = Session["link"].ToString();
        string toemail = formCollection["toemail"];
        MailEngine.Send("mail@mail.com", toemail, "link", message);
        return RedirectToAction("CanvasShare");
    }
    catch
    {

    }
    return null;
}

Class MailEngine:

public static void Send(string from, string to, string subject, string body)
{
    try
    {
        MailMessage mail = new MailMessage(from, to, subject, body);
        SmtpClient client = new SmtpClient("smtp.mymail.com");
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = false;
        client.Send(mail);
    }
    catch
    {

    }
}`enter code here`
于 2013-09-27T05:28:55.043 回答
0

System.Net.Mail 仅正确支持本地主机上的 25 端口,我花了很多时间才找到一篇对这个问题有完整解释的文章。

也许它会帮助你:

            string from = "";
            string fromPass = "";

            MailMessage eMail = new MailMessage();
            eMail.IsBodyHtml = true;
            eMail.Body = message;
            eMail.From = new MailAddress(from);
            eMail.To.Add(address);
            eMail.Subject = subject;
            SmtpClient SMTP = new SmtpClient();

            SMTP.Credentials = new NetworkCredential(from, fromPass);
            SMTP.Host = "localhost";
            SMTP.Send(eMail);

编辑:如果您需要使用 465 或其他端口,您需要使用单独的库

于 2013-09-27T06:28:15.237 回答
0

@Jayraj

显然,您的 try 块中有一些错误。它捕获了错误,但你没有在那里做任何事情。我怀疑这是您的 Session["link"] 为空。在 ActionResult 中返回 null 将导致空白屏幕。

于 2013-09-27T06:54:58.130 回答