【问题标题】:how to send mail using SendGrid Api Key in azure portal using asp.net c#如何使用 asp.net c# 在 azure 门户中使用 SendGrid Api Key 发送邮件
【发布时间】:2018-07-06 03:42:20
【问题描述】:
//in class a create object to classb and send data to classb sendmail method

class a{
classb b=new classb();
b.sendmail(jsonData);

}


// classb class recevie data and send the mail

class classb(){

//method
public void sendmail(classname obj){

        string to = "test.123@gmail.com";
        string from = "test@mail";
        MailMessage message = new MailMessage(from, to);
        message.Subject = "Demo Requsted from User";
        message.Body = mailbody;
        message.BodyEncoding = Encoding.UTF8;
        message.IsBodyHtml = true;

// where to give azure api key .i have only send-grid api key .please help me

        var smtp = new SmtpClient
        {
            Host = "smtp.sendgrid.net",// azure server
            Port = 587,
            EnableSsl = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = true,
            Credentials = new System.Net.NetworkCredential("mail", "pwd")
        };

        try
        {
            smtp.Send(message);

        }
        catch (Exception ex)
        {
            throw ex;


        }

}
}

【问题讨论】:

  • 提示,在这里闲逛的人都是经验丰富的程序员,我们大多数人都有严重的强迫症,讨厌乱七八糟的代码,并且一生都在努力让事情看起来整洁优雅。如果你问一个问题,花 5 分钟整理你的代码以便按下大按钮,你会发现它总是值得的

标签: c# sendgrid


【解决方案1】:

你的问题是你根本没有使用 SendGrid,你使用的是SmtpClient

如果你想使用 SendGrid,你需要下载 nugets 并编写 SendGrid 代码,你不是。

请查看以下示例和 nuget 包

https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/csharp.html

https://github.com/sendgrid/sendgrid-csharp

【讨论】:

  • @TheGenral 谢谢你的回复 var myMessage = new SendGrid.SendGridMessage();发送gridemsg();这个方法出错了,我添加了 nugets 并编写了 sendgrid 代码,但同样的错误
  • public void redemomail(adddemoreq body) { string mailbody = System.IO.File.ReadAllText(filename); mailbody = mailbody.Replace("##Demotype##", body.Demotype); mailbody = mailbody.Replace("##Username##", body.Username); mailbody = mailbody.Replace("##Useremail##", body.Useremail);
【解决方案2】:

你应该使用SendGridClient类通过SendGrid发送邮件,然后将apiKey作为参数传递给它

        static async Task Execute()
        {
            var apiKey = // your key
            var client = new SendGridClient(apiKey);
            var msg = new SendGridMessage()
            {
                From = new EmailAddress("test@example.com", "DX Team"),
                Subject = "Hello World from the SendGrid CSharp SDK!",
                PlainTextContent = "Hello, Email!",
                HtmlContent = "<strong>Hello, Email!</strong>"
            };
            msg.AddTo(new EmailAddress("test@example.com", "Test User"));
            var response = await client.SendEmailAsync(msg);
        }

更多详情请看这里https://docs.microsoft.com/en-us/azure/sendgrid-dotnet-how-to-send-email

【讨论】:

  • static async Task Execute() 如何从 CLass A .in 这个方法中获取数据。
  • 你只需要像'Execute.Wait();'一样调用这个方法从你的方法
  • 好的,但是在 classa 类中,我们将数据发送到 classb send-mail() 方法 - 就像参数传递一样。
  • 这个方法可以添加相同的参数,也可以使用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多