简体   繁体   中英

how to send mail using SendGrid Api Key in azure portal using asp.net c#

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


        }

}
}

Your problem is you aren't using SendGrid at all, you are using SmtpClient

If you want to use SendGrid you will need to download the nugets and write SendGrid code, none of what you is.

Please have a look through the following for examples and the nuget package

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

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

You should use SendGridClient class to send mail by SendGrid and then pass apiKey as a parameter into it

        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);
        }

For more details see here https://docs.microsoft.com/en-us/azure/sendgrid-dotnet-how-to-send-email

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM