如何使用asp.net c#

时间:2018-07-06 03:42:20

标签: c# sendgrid

//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;


        }

}
}

2 个答案:

答案 0 :(得分:0)

您的问题是您根本没有使用 SendGrid ,而是使用了SmtpClient

如果要使用 SendGrid ,则将需要下载nuget并编写 SendGrid 代码,而您所需要的都不是。

请仔细阅读以下示例和nuget包

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

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

答案 1 :(得分:0)

您应该使用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