Javascript ReferenceError:未定义SendGridMessage
我所要做的就是从mvc razor视图页面使用sendgrid。这是我最好的尝试,请帮忙。 为什么我得到这个错误SendGridMessage没有定义 我正试图从中改编这个例子 我已经在web.config中添加了名称空间Javascript ReferenceError:未定义SendGridMessage,javascript,asp.net-mvc,sendgrid,Javascript,Asp.net Mvc,Sendgrid,我所要做的就是从mvc razor视图页面使用sendgrid。这是我最好的尝试,请帮忙。 为什么我得到这个错误SendGridMessage没有定义 我正试图从中改编这个例子 我已经在web.config中添加了名称空间 <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Net.Mail" />
<add namespace="SendGrid" />
</namespaces>
这是密码
<input type="button" value="Send Booking" class="btn k-button" onclick="sendGrid1()" />
<script type="text/javascript">
function sendGrid1() {
var myMessage = new SendGridMessage();
myMessage.From = new MailAddress("john@example.com");
myMessage.AddTo("john@example.com");
myMessage.Subject = "Testing the SendGrid Library";
//Add the HTML and Text bodies
myMessage.Html = "<p>Hello World!</p>";
myMessage.Text = "Hello World plain text!";
var username = "xxxx";
var pswd = "xxxxxxx";
var credentials = new NetworkCredential(username, pswd);
// Create an Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email.
// You can also use the **DeliverAsync** method, which returns an awaitable task.
transportWeb.Deliver(myMessage,function(err, json) {
if (err) { console.error(err); }
console.log(json);
});
}
</script>
如果使用Web窗体,则需要在C代码中使用此代码;如果使用MVC,则需要在控制器中使用此代码。将此代码复制并粘贴到您在C中提到的网站上 我建议使用NuGet下载此库。在Visual Studio中,工具->NuGet软件包管理器->管理解决方案的NuGet软件包…编写SendGrid并在项目中安装所需的库。然后按照网站上给出的代码编写以下代码
using System;
using System.Net;
using System.Net.Mail;
using SendGridMail;
using SendGridMail.Transport;
SendGrid mail = SendGrid.GetInstance();
mail.From = new MailAddress("you@youremail.com");
mail.AddTo("test@sendgrid.com");
mail.Subject = "Sending with SendGrid is Fun";
mail.Text = "and easy to do anywhere, even with C#";
var credentials = new NetworkCredential(api_user, api_key);
var transportWeb = new Web(credentials);
transportWeb.Deliver(mail);
您只需获得API密钥,链接如下:
希望这有帮助。您将csharp代码放在javascript标记中,从而将csharp与javascript混淆