user6383418
user6383418

Reputation: 415

How to add replyto with sendgrid in C#

I am using sendgrid with a .NET Core site I built. It has a contact form on it so a client can fill out the form and it is sent to my email address. I am using the Sendgrid nuget package to generate and send the email. The issue is that when I get the email I dont want to replyto myself and when I make the replyto equal the email address entered - sendgrid wont send the email because that email isnt entered into the Single Sender Verification. There must be a way to configure things so in my email when I click reply it will go to the person who sent the email. Right?

string key = _iConfig.GetSection("Email").GetSection("SendGridKey").Value;
var client = new SendGridClient(key);

var subject = "Form submission";

var fromemail = _iConfig.GetSection("Email").GetSection("From").Value;
var from = new EmailAddress(fromemail, model.Name);

var toemail = _iConfig.GetSection("Email").GetSection("To").Value;
var to = new EmailAddress(toemail, "Admin");

var htmlContent = "<strong>From: " + model.Name + "<br>Email: " + model.Email + "<br>Message: " + model.Message;
                
var msg = MailHelper.CreateSingleEmail(from, to, subject, model.Message, htmlContent);
var response = await client.SendEmailAsync(msg);
return response.IsSuccessStatusCode;

Upvotes: 0

Views: 2002

Answers (1)

philnash
philnash

Reputation: 73029

Twilio SendGrid developer evangelist here.

To set the reply-to address in C# you need the setReplyTo method, like this:

var msg = MailHelper.CreateSingleEmail(from, to, subject, model.Message, htmlContent);

// Add the reply-to email address here.
var replyTo = new EmailAddress(model.Email, model.Name);
msg.setReplyTo(replyTo);

var response = await client.SendEmailAsync(msg);
return response.IsSuccessStatusCode;

Upvotes: 3

Related Questions