我所要做的就是在mvc剃刀视图页面中使用sendgrid。这是我最大的努力,请帮助我。为什么我得到这个错误"SendGridMessage没有定义“,
我正在尝试改编https://github.com/sendgrid/sendgrid-csharp中的示例,我已经在web.config中添加了名称空间
<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>发布于 2015-09-11 07:17:53
您需要做的是在您的C#代码隐藏(如果使用Web Forms)或控制器(在MVC的情况下)中使用此代码。将此代码复制并粘贴到您在C#中提到的网站上。
我推荐使用NuGet来下载这个库。在Visual Studio中,工具--> NuGet包管理器-->管理Solution...Write SendGrid的NuGet包,并在项目中安装所需的库。然后按照网站上的说明编写以下代码。
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 key,链接在这里:https://sendgrid.com/docs/Classroom/Send/api_keys.html
希望这能有所帮助。
https://stackoverflow.com/questions/32493832
复制相似问题