hello, i have an issue, contact us in webapplication aspnet webform
i have an advertise website, in which people put their ads and they contact echother by any form of email address. the same when people will contact me with their defaqult email address. in my case, the problem is the client's creditial and host, which must be specific or defined. but how make it default or general?
protected void btnSend_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage(txtEmail.Text, emailto, txtSubject.Text, txtBody.Text); //creates a mail message
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient
{
Port = 25, //any client
Host = "smtp.gmail.com",//"smtp-mail.outlook.com", //"smtp.sendgrid.net", //or another email sender provider
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
Credentials = new NetworkCredential("[email protected]", 'my secret password') //but it must be sender, not me
};
try
{
client.Send(message);
lblResult.Text = "your mail has been sent successfully!";
}
catch (Exception ex) { lblResult.Text = ex.Message; }
finally { btnSend.Enabled = true; clearFields(); }
}
//////////////////////////////////////////////
i used another way, i made droppdownlist with different hosts (what stands after @ sign in email) and i used
client.Credentials = new NetworkCredential(txtEmail.Text, txtPassword.Text, domain);
but didn't worked??!!
how should i make default host??!! appreciate your respond, with codes. very kind regards

Prasad RaveendranRank #320 · 7.8K reputationPosted Aug 4, 2024, 12:35 PM EDT
To make your email sending functionality more flexible and user-friendly, you can implement a way to allow users to send emails using their own email credentials. Here’s how you can achieve this:
SmtpClient.Here's a sample implementation:
protected void btnSend_Click(object sender, EventArgs e) { // Capture user inputs string fromEmail = txtEmail.Text; string password = txtPassword.Text; string toEmail = txtToEmail.Text; string subject = txtSubject.Text; string body = txtBody.Text; string selectedProvider = ddlEmailProvider.SelectedValue; // Determine SMTP settings based on selected provider string smtpHost = ""; int smtpPort = 0; bool enableSsl = true; switch (selectedProvider) { case "Gmail": smtpHost = "smtp.gmail.com"; smtpPort = 587; break; case "Outlook": smtpHost = "smtp-mail.outlook.com"; smtpPort = 587; break; case "SendGrid": smtpHost = "smtp.sendgrid.net"; smtpPort = 587; break; // Add more providers as needed default: lblResult.Text = "Please select a valid email provider."; return; } // Create the mail message MailMessage message = new MailMessage(fromEmail, toEmail, subject, body); message.IsBodyHtml = true; // Configure the SMTP client SmtpClient client = new SmtpClient { Host = smtpHost, Port = smtpPort, EnableSsl = enableSsl, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromEmail, password) }; try { client.Send(message); lblResult.Text = "Your mail has been sent successfully!"; } catch (Exception ex) { lblResult.Text = "Error sending email: " + ex.Message; } finally { btnSend.Enabled = true; clearFields(); } }Note:
ddlEmailProvideris a dropdown list containing the supported email providers (e.g., Gmail, Outlook, SendGrid).HTML Code
venus najadRank #2,456 · 573 reputationPosted Aug 5, 2024, 1:52 PM EDT
hello mr Raveendran and thanksc for your respond...
as you know, people has different email domain. many hyas their websites email domain, many has their jobs email domain...
is sendgrid cover the rest of email domain than gmail and outlook? how should i define sendgrid in the projekt? or sendgrid is known in C#?
appreciate your respond... kind regards
venus najadRank #2,456 · 573 reputationPosted Aug 5, 2024, 1:54 PM EDT
sorry, i add that other domain's port are 25...
if sendgrid covers other email domain, then i use 25 for its port. shouldn't i?
venus najadRank #2,456 · 573 reputationPosted Aug 6, 2024, 4:10 PM EDT
the problem is solved using sendgrid...
Prasad RaveendranRank #320 · 7.8K reputationPosted Aug 6, 2024, 10:41 PM EDT
good to hear that issue has been resolved
Matthias Mueller3 reputationPosted Aug 6, 2024, 11:47 PM EDT
When setting up a "Contact Us" feature in a C# ASP.NET WebForms application, configuring the email domain properly is crucial. It’s a top search topic for developers looking to ensure reliable email delivery. Proper configuration of the email domain helps avoid issues with email delivery and spam filters, making sure that user inquiries are received and addressed efficiently.