0

在我的网站上,我有“联系我们”页面,我希望经理能够通过电子邮件回复客户。所有过程都需要在服务器端。需要注意的是,我使用 Webmatrix 开发环境...

因此,考虑到我的 Web 开发,我添加发送电子邮件功能的简单方法是什么?

我的 HTML 代码是:

foreach(var row in db.Query(displayApplicant,nameOfcustomer))
                {
                <tr>
                    <td class="dispExpertActScreen">@row.messegeID</td>
                    <td class="dispExpertActScreen">@row.name</td>
                    <td class="dispExpertActScreen">@row.email</td>
                    <td class="dispExpertActScreen">@row.isCustomer</td>
                    <td class="dispExpertActScreen">@row.userID</td>
                    <td class="dispExpertActScreen">@row.content</td>
                    <td class="dispExpertActScreen"><a href="#" onclick="answerBox('@row.messegeID','@row.userID')" style="color: #b04e4e">answer the question</a></td>
                    <td class="dispExpertActScreen"><a href="#" onclick="reqeustToDelete('@row.messegeID')" style="color: #b04e4e">remove</a></td>
                </tr>
                } 

javascript函数:(将详细信息保存在隐藏字段中)

<script type="text/javascript">

function answerBox(messegeID,userID) {
        var msg = prompt('answer to customer:');
        document.getElementById('answer').value = msg;
        document.getElementById('ansMode').value = 'true';
        document.getElementById('msgID').value = messegeID;
        document.getElementById('user').value = userID;
        document.getElementById('ansMessege').submit();
    }

</script>

隐藏字段:

<form method="post" id="ansMessege" style="font-size: medium; margin-top: 10%" dir="rtl">
    <input type="hidden" name="answer" id="answer" value="">
    <input type="hidden" name="msgID" id="msgID" value="">
    <input type="hidden" name="user" id="user" value="">
    <input type="hidden" name="ansMode" id="ansMode" value="">
</form> 

asp.net 代码:

<%@ Import Namespace="System.Web.Mail" %>

@{
    Layout = "~/_ManagerLayout.cshtml";
    Page.Title = "Management Applications";

 }
@{
    var db = Database.Open("MyProjectSite");
    var display="no";
    var displayApplicant="";
    var nameOfcustomer="";
    var category="";
    var yesNo="";
     if(IsPost)
    {
     if(Request.Form["ansMode"] == "true")
          {
              var selectQuery="SELECT * FROM messegesFromCustomers";
              var id=Request.Form["msgID"];
              var msg=Request.Form["answer"];
              var user=Request.Form["user"];

              foreach(var row in db.Query(selectQuery))
              {
                  if(row.messegeID == Convert.ToInt32(id))
                  {
                      if(row.isCustomer == "yes")// **send the messege to customer account**
                      {
                          var insertQuery="UPDATE messegesFromCustomers SET answer=@0 WHERE messegeID=@1";
                          db.Execute(insertQuery,msg,id);
                          Response.Write("<script>alert('your answer sent successfully');</script>");
                          break;
                      }
                      else
                      {
                          // **Send messege to Occasional customer via email**
                         // **here I want to add code for sending email...**


                          string from = "ofirhgy@gmail.com";
                          string to = "ofirhgy@gmail.com";
                          string subject = "Hi!";
                          string body = "How are you?";
                          SmtpMail.SmtpServer = "mail.gmail.com";
                          SmtpMail.Send(from, to, subject, body);

                          break;
                      }
                  }
              }

          }

 }



}

如您所见,我尝试使用 WebMail.Send(...) 但我不知道如何...除此之外,我看到有人写道需要向 Web.config 添加一些代码或类似的东西那....

谢谢你的帮助。

编辑:我编辑代码并输入以下内容:

<%@ Import Namespace="System.Web.Mail" %>

在 asp.net 页面的顶部,但我收到此错误:

解析器错误消息:在“@”字符后遇到空格或换行符。只有有效的标识符、关键字、注释、“(”和“{”在代码块的开头是有效的,它们必须紧跟在“@”之后,中间没有空格。

任何人都可以为我提供确切的代码吗?

4

2 回答 2

0

首先,我们需要导入System.Web.Mail命名空间:

<%@ Import Namespace="System.Web.Mail" %>

发送消息是SmtpMail.Send()使用以下参数调用的问题: senderrecipient和。例如,我们会用 C# 发送一封电子邮件,如下所示:subjectbody

string from = "sender@example.com";
 string to = "recipient@example.com";
 string subject = "Hi!";
 string body = "How are you?";
 SmtpMail.SmtpServer = "mail.example.com";
 SmtpMail.Send(from, to, subject, body);

SmtpMail.SmtpServer让您指定用于传递您的消息的邮件服务器。

来自这里的内容

于 2012-12-03T10:28:46.793 回答
0

我找到了解决方案

在我的代码中,我添加了这个并成功发送了电子邮件:

 if(isCustomer == "yes")
               {

                  db.Execute(insertQuery,msg,id);
                  db.Execute(deleteQuery,id);
               }

               else
               {
                   WebMail.SmtpServer = "smtp.gmail.com";
                   WebMail.EnableSsl = true;
                   WebMail.SmtpPort = 587;//25
                   WebMail.UserName = "my gmail user name";
                   WebMail.Password = "my gmail password";
                   WebMail.From = "my address of email";
                   // Send email
                   WebMail.Send(
                    to: email,
                    subject: "Message from "some name of site" - Site",
                    body: msg);

                    db.Execute(deleteQuery,id);    

                }
于 2012-12-06T10:52:54.163 回答