所以我有一个数据库程序,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
"