2013-06-24 197 views
2

(成品RESULT去编辑/ EDIT AT BOTTOM)ASP.net剃须刀 - 使用Gmail SMTP网站发送电子邮件

正如标题状态我想使用Gmail网站发送电子邮件SMTP如ASP.net教程中所述:http://www.asp.net/web-pages/tutorials/email-and-search/11-adding-email-to-your-web-site

在早期解决ISAPI.dll处理程序映射错误之后,Webmatrix错误控制台中没有更多错误,只是“GET”和“POST”操作的成功标记;但是,该网页正在返回try catch错误,“电子邮件未发送....”

在SQL Server管理器中,我尝试查看是否为服务器和管理员角色设置了邮件,但我可以'没有看到代理属性,我明白,因为我有快速版SQL Management Studio。也就是说,如果服务器角色或邮件设置出现问题,肯定会生成错误消息?

有人可以请客气一下,快速浏览SMTP设置和整体代码,请提供错误或建议吗?

(顺便说一句,我在示例代码改变了我的电子邮件地址和密码,重复的webmail.username和webmail.from我的电子邮件地址)

感谢。

这里是EmailRequest.cshtml

 @{ 

    } 

    <!DOCTYPE html> 
    <html> 
    <head> 
     <title>Request for Assistance</title> 
    </head> 
    <body> 
     <h2>Submit Email Request for Assistance</h2> 
     <form method="post" action="ProcessRequest.cshtml"> 
     <div> 
      Your name: 
      <input type="text" name="customerName" /> 
     </div> 

     <div> 
      Your email address: 
      <input type="text" name="customerEmail" /> 
     </div> 

     <div> 
      Details about your problem: <br /> 
      <textarea name="customerRequest" cols="45" rows="4"></textarea> 
     </div> 

     <div> 
      <input type="submit" value="Submit" /> 
     </div> 
     </form> 
    </body> 
    </html> 

**Here is ProcessRequest.cshtml** 

     @{ 
     var customerName = Request["customerName"]; 
     var customerEmail = Request["customerEmail"]; 
     var customerRequest = Request["customerRequest"]; 
     var errorMessage = "true"; 
     var debuggingFlag = false; 
     try { 
      // Initialize WebMail helper 
      WebMail.SmtpServer = "smtp.gmail.com"; 
      WebMail.EnableSsl = true; 
      WebMail.SmtpPort = 465; 
      WebMail.UserName = "MY EMAIL ADDRESS.com"; 
      WebMail.Password = "MYPASSWORD"; 
      WebMail.From = "MY EMAIL ADDRESS.com"; 

      // Send email 
      WebMail.Send(to: customerEmail, 
       subject: "Help request from - " + customerName, 
       body: customerRequest 
      ); 
     } 
     catch (Exception ex) { 
      errorMessage = ex.Message; 
     } 
    } 
    <!DOCTYPE html> 
    <html> 
    <head> 
     <title>Request for Assistance</title> 
    </head> 
    <body> 
     <p>Sorry to hear that you are having trouble, <b>@customerName</b>.</p> 
     @if(errorMessage == ""){ 
      <p>An email message has been sent to our customer service 
      department regarding the following problem:</p> 
      <p><b>@customerRequest</b></p> 
     } 
     else{ 
      <p><b>The email was <em>not</em> sent.</b></p> 
      <p>Please check that the code in the ProcessRequest page has 
       correct settings for the SMTP server name, a user name, 
       a password, and a "from" address. 
      </p> 
      if(debuggingFlag){ 
       <p>The following error was reported:</p> 
       <p><em>@errorMessage</em></p> 
      } 
     } 
    </body> 
    </html> 
____________________________________________________ 

编辑:

感谢您的帮助迈克和格克汗。今天早上,我再仔细看看。果然,昨晚从网页发送了两封电子邮件;因此,在某些时候所有的因素都是正确的。

我曾尝试过端口25,465和587,但是,我正在按照教程和'不是代码'。该教程在我看来有点误导,并造成混乱 - 我正在错误的收件箱中寻找电子邮件。人们会认为通常会将问题报告发送给主机,而不是报告问题的用户。

此外,即使电子邮件正在发送,try catch错误仍然显示。当然,错误信息应该通过发出错误来激活。错误消息似乎只有在[var errorMessage =“true”;]设置为true时才会出现。

我错过了什么?任何人都可以请解释本教程如何尝试捕获方法的作品?

无论如何,这一切都有效 - 我总是发现迈克的评论令人放心。

我使用另一个教程将代码下载到了一个页面,并添加了一些表单验证,然后修改了代码,以便将电子邮件发送给主机而不是用户。

这是新的EmailRequest。CSHTML:

@{ 

    Layout = "/_SiteLayout.cshtml"; 
    Page.Title = "Compare"; 

    Validation.RequireField("emailAddress", "customerName is required."); 
    Validation.RequireField("emailSubject", "customerEmail is required."); 
    Validation.RequireField("emailBody", "customerRequest is required."); 

     var message = ""; 
     var companyname = Request.Form["emailAddress"]; 
     var contactname = Request.Form["emailSubject"]; 
     var employeecount = Request.Form["emailBody"]; 


    try{ 
     if (IsPost && Validation.IsValid()) { 

     // Send email 
     WebMail.Send(to:"ADMIN-EMAIL-ADDRESS.COM", 
      subject: Request.Form["emailSubject"], 

      body: "Help request from - " + Request.Form["customerName"] + " - " + "Email Subject - " + Request.Form["emailSubject"] + " - " + "Customer email - " + Request.Form["emailAddress"] 
      ); 
      message = "Email sent!"; 
     } 
    } 
    catch(Exception ex){ 
     message = "Email could not be sent!"; 
    } 
} 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8" /> 
    <title>Test Email</title> 

       <style type="text/css"> 
      .validation-summary-errors {font-weight:bold; color:red; font-size: 11pt;} 
      .validation-summary-valid { 
    display: none; 
} 

     </style> 

    </head> 
    <body> 
    <h1>Test Email</h1> 
    <form method="post"> 
     @Html.ValidationSummary("Errors with your submission:") 

     <p> 
     <label for="customerName">Name:</label> 
     <input type="text" name="customerName" value="@Request.Form["customerName"]" /> 


     </p> 

     <p> 
     <label for="emailAddress">Email address:</label> 
     <input type="text" name="emailAddress" value="@Request.Form["emailAddress"]" /> 


     </p> 
     <p> 
     <label for="emailSubject">Subject:</label> 
     <input type="text" name="emailSubject" value="@Request.Form["emailSubject"]" /> 

     </p> 
     <p> 
     <label for="emailBody">Text to send:</label><br/> 
     <textarea name="emailBody" rows="6"></textarea> 

     </p> 
    <p><input type="submit" value="Send!" /></p> 
    @if(IsPost){ 
     <p>@message</p> 
    } 
    </form> 
    </body> 
</html> 

最后,这里是_AppStart代码一起去,我昨天没有发布例如:

@{ 
     WebSecurity.InitializeDatabaseConnection("StarterSite", "UserProfile", "UserId", "Email", true); 

     // WebMail.SmtpServer = "mailserver.example.com"; 
     // WebMail.UserName = "[email protected]"; 
     // WebMail.Password = "your-password"; 
     // WebMail.From = "[email protected]"; 
     // WebMail.EnableSsl = true; (add this if smtp is encrypted) 
     // WebMail.SmtpPort = 587; 
    } 

编辑/ EDIT

我在在提交之后在textarea框中保留数据/值有问题以及验证textarea框。更多的研究提醒了我,在这种情况下,两个“必需”属性并提供了一个整洁的工具提示。

因此,我已经剥离了保留文本框值并验证了表单的前面的代码,并使用“required”属性代替 - 更加整洁。

此外,处理代码和成功消息现在已内置到电子邮件请求页面中,因此不再需要ProcessRequest.cshtml页面。

下面是一个联系表格的完成代码,用于向静态电子邮件地址发送请求 - 除非您在页面中添加邮件服务器验证,否则不要忘记您还需要使用_AppStart页面代码 - 两者都需要。但是,请注意,在用户能够查看它们的页面中添加服务器电子邮件设置是不安全的。我建议使用_AppStart页面作为以Asp.net中的下划线开头的文件。Razor无法在线查看。

@{ 

    Layout = "/_SiteLayout.cshtml"; 
    Page.Title = "Compare"; 

    var message = ""; 
    var companyname = Request.Form["emailAddress"]; 
    var contactname = Request.Form["emailSubject"]; 
    var employeecount = Request.Form["emailBody"]; 


    try{ 
     if (IsPost && Validation.IsValid()) { 

     // Send email 
     WebMail.Send(to:"ADMIN EMAIL.COM", 
      subject: Request.Form["emailSubject"], 

      body: "Help request from - " + Request.Form["customerName"] + " - " + "Email Subject - " + Request.Form["emailSubject"] + " - " + "Customer email - " + Request.Form["emailAddress"] 
      ); 
      message = "Email sent!"; 
     } 
    } 
    catch(Exception ex){ 
     message = "Email could not be sent!"; 
    } 
} 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>Test Email</title> 


</head> 

    <body> 
    <h1>Test Email</h1> 

    <form method="post"> 

     <p> 
     <label for="customerName">Name:</label> 
     <input type="text" name="customerName" required /> 
     </p> 

     <p> 
     <label for="emailAddress">Email address:</label> 
     <input type="text" name="emailAddress" required /> 
     </p> 
     <p> 
     <label for="emailSubject">Subject:</label> 
     <input type="text" name="emailSubject" required /> 

     </p> 

     <p> 
     <label for="emailBody">Text to send:</label><br/> 
     <textarea name="emailBody" rows="6" required></textarea> 
     </p> 

    <p><input type="submit" value="Send!" /></p> 
    @if(IsPost){ 
     <p>@message</p> 
    } 
    </form> 
    </body> 
</html> 
+0

上面的代码应该可以正常工作。只需将SMTP端口更改为587. –

回答

2

我已经写了很久以前的项目的代码。尝试这个。

public void SendEmail(string from, string to, string subject, string body) 
{ 
    string host = "smtp.gmail.com"; 
    int port = 587; 

    MailMessage msg = new MailMessage(from, to, subject, body); 
    SmtpClient smtp = new SmtpClient(host, port); 

    string username = "[email protected]"; 
    string password = "1234567890"; 

    smtp.Credentials = new NetworkCredential(username, password); 
    smtp.EnableSsl = true; 
    smtp.UseDefaultCredentials = false; 

    try 
    {  
     smtp.Send(msg); 
    } 
    catch (Exception exp) 
    { 
     //Log if any errors occur 
    } 
} 
+0

感谢您的答复。当我再次打开我的电脑时,我会在早上与您的代码一起去,我会让你知道我如何继续。 – DaniB

0

如果您想用剃须刀丰富电子邮件正文,您可以使用Mailzory。另外,您可以从here下载nuget包。用法很简单:

// template path 
var viewPath = Path.Combine("Views/Emails", "hello.cshtml"); 
// read the content of template and pass it to the Email constructor 
var template = File.ReadAllText(viewPath); 

var email = new Email(template); 

// set ViewBag properties 
email.ViewBag.Name = "Johnny"; 
email.ViewBag.Content = "Mailzory Is Funny"; 

// send email 
var task = email.SendAsync("[email protected]", "subject"); 
task.Wait() 
相关问题