hello everybody
i have created contact us form, but when i send an email from email address, it doesnt work???!!! my codes are as bellow:
btnSend.Enabled = false;
MailMessage message = new MailMessage(txtEmail.Text, "[email protected]", txtSubject.Text, txtBody.Text); //creates a mail message
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //"smtp-mail.outlook.com"
client.EnableSsl = true;
client.Credentials = new NetworkCredential(txtEmail.Text, txtPassword.Text);
client.Send(message);
lblResult.Text = "your mail has been sent successfully!";
}
catch { lblResult.Text = "there was an error.\r\n make sure that your internet is connected"; }
finally { btnSend.Enabled = true; }//either successful or not enables the send button
appreciate your help with codes or corrections

Jignesh KumarRank #91 · 22.9K reputationPosted Feb 5, 2024, 8:56 PM EST
Hello Venus,
You can use below code and set your sender email with parameter so it should work,
// Sender's email address and password are mandatory, // you can change from here based on your requirement and pass as parameter string senderEmail = "[email protected]"; string senderPassword = "your-password"; // Receiver's email address here you can pass Receiver mail id to whom you want to send an email string receiverEmail = "[email protected]"; // SMTP server details string smtpServer = "smtp.example.com"; int smtpPort = 587; // smtp port : 587 for TLS or 465 for SSL // Create a new SMTP client using SmtpClinet SmtpClient smtpClient = new SmtpClient(smtpServer, smtpPort); smtpClient.EnableSsl = true; // Enable SSL/TLS encryption // Here you need to set credentials (sender's email address and password) smtpClient.Credentials = new NetworkCredential(senderEmail, senderPassword); //Here you can Create a new MailMessage object MailMessage mailMessage = new MailMessage(senderEmail, receiverEmail); //Here you can Set email subject and body mailMessage.Subject = "Test Email"; mailMessage.Body = "This is a test email sent from ASP.NET WebForms."; try { //Now we are ready to send the email smtpClient.Send(mailMessage); Response.Write("Email sent successfully."); } catch (Exception ex) { // Log error message here incase it is failing Response.Write("Failed to send email: " + ex.Message); }venus najadRank #2,456 · 573 reputationPosted Feb 6, 2024, 2:40 PM EST
dear mr kumar, i have already put the working codes... but the issue is the SmtpClient client = new SmtpClient("smtp.gmail.com", 587); which requires only gmail users can send email... in my case it doesnt work and i receive error, why, i dont know... i tried send email by other mail addresses and it didnt worked, because the host is smtp.gmail.com...
as soon as i change it to SmtpClient client = new SmtpClient("smtp-mail.outlook.com", 587); i could send message from outlook and it worked, but not sending from gmail... becasue the host is smtp-mail.outlook.com...
what i need is sending email from any email addresses... how should i make it general? stmp server must be valid for any sender's email...
if you check my codes,
MailMessage message = new MailMessage(senders email, receivers email, Subje, Body);
SmtpClient client = new SmtpClient(host, port); //"smtp-mail.outlook.com"
client.EnableSsl = true;
client.Credentials = new NetworkCredential(sender's email, it's Password);
client.Send(message);
lblResult.Text = "your mail has been sent successfully!";
your codes are same as my codes, only its design is different... it doesnt solve my problem... i need general host, smtp."something".com
appreciate your respond
Jayraj ChhayaRank #304 · 8.2K reputationPosted Feb 7, 2024, 4:13 AM EST
Hi venus najad,
The process involves configuring the SMTP (Simple Mail Transfer Protocol) settings for each email domain.
To send emails from different email domains, you need to specify the SMTP server settings for each domain. Here's an example of how you can send emails using different email domains in Dot Net:
using System; using System.Net; using System.Net.Mail; public class EmailSender { public void SendEmail(string fromAddress, string toAddress, string subject, string body) { // Create a new MailMessage object MailMessage mail = new MailMessage(); // Set the From, To, Subject, and Body properties mail.From = new MailAddress(fromAddress); mail.To.Add(toAddress); mail.Subject = subject; mail.Body = body; // Set the SMTP server settings based on the email domain SmtpClient smtpClient = new SmtpClient(); if (fromAddress.EndsWith("@outlook.com")) { smtpClient.Host = "smtp-mail.outlook.com"; smtpClient.Port = 587; smtpClient.EnableSsl = true; } else if (fromAddress.EndsWith("@yahoo.com")) { smtpClient.Host = "smtp.mail.yahoo.com"; smtpClient.Port = 587; smtpClient.EnableSsl = true; } else if (fromAddress.EndsWith("@gmail.com")) { smtpClient.Host = "smtp.gmail.com"; smtpClient.Port = 587; smtpClient.EnableSsl = true; } // Set the credentials for the SMTP server smtpClient.Credentials = new NetworkCredential("your-email-address", "your-password"); // Send the email smtpClient.Send(mail); } }Remember to replace
"your-email-address"and"your-password"with your actual email credentials.Abhishek YadavRank #217 · 11.3K reputationPosted Feb 7, 2024, 6:51 AM EST
you can use smtp protocol or can build api as most of modern antivirus program kill the smtp request
venus najadRank #2,456 · 573 reputationPosted Feb 9, 2024, 2:44 PM EST
dear mr Chhaya, thank´nks for your solution... i became very happy, but i get the following error: The SMTP host was not specified... how should i solve it?
i have used another solution with a general smtp, but i get error on connection attempted long time period and it didnt responded...
before, my codes worked, but something is wron and i cannot find it???!!
venus najadRank #2,456 · 573 reputationPosted Feb 9, 2024, 3:08 PM EST
dear mr Menoth, thenks for your respond, when i ran your codes i received the following error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.
i have tried mr Chhaya's solution, whitout any result???!!!
Jithu ThomasRank #437 · 5.7K reputationPosted Feb 7, 2024, 12:59 AM EST
f you want to send emails using a general SMTP server that works with any email address, you can use a third-party email service that supports SMTP. Here are two commonly used options:
Using SendGrid
Sign up for a free SendGrid account: SendGrid Sign Up
After signing up, you will get an API key. Use this API key in your application.
Update your code to use SendGrid's SMTP settings as below: -
SmtpClient client = new SmtpClient("smtp.sendgrid.net", 587); client.EnableSsl = true; client.Credentials = new NetworkCredential("your-sendgrid-username", "your-sendgrid-api-key");Using Mailgun
Sign up for a free Mailgun account: Mailgun Sign Up
After signing up, you will get an API key and a domain. Use these in your application.
Update your code to use Mailgun's SMTP settings:
SmtpClient client = new SmtpClient("smtp.mailgun.org", 587); client.EnableSsl = true; client.Credentials = new NetworkCredential("your-mailgun-username", "your-mailgun-api-key");Rajeesh MenothRank #118 · 18.4K reputationPosted Feb 7, 2024, 5:40 AM EST
Hi,
It appears that you are utilizing the SMTP server of Gmail and it is likely that the problem pertains to the requirement of a secure connection by the SMTP server. At the moment, you are replacing the original exception with a custom message. I suggest that you throw the original exception first before proceeding to follow the guide I have published for sending emails via various types of SMTP servers.
Note: The code I have provided in my article is working fine and try it out.
Reference:
https://www.c-sharpcorner.com/UploadFile/2a6dc5/how-to-send-a-email-using-Asp-Net-C-Sharp/
https://www.c-sharpcorner.com/UploadFile/2a6dc5/send-multiple-email-with-attachment-using-Asp-Net-with-C-Sharp/
Rajeesh MenothRank #118 · 18.4K reputationPosted Feb 12, 2024, 12:20 AM EST
Hi,
You can go through the solution suggested by the Google team.
https://support.google.com/mail/thread/146949535/the-server-response-was-5-7-0-authentication-required?hl=en
venus najadRank #2,456 · 573 reputationPosted Feb 13, 2024, 2:51 PM EST
hello mr thomas, i ran your solution without any result???!!! thansk anyway
venus najadRank #2,456 · 573 reputationPosted Feb 13, 2024, 2:54 PM EST
dear mr chhaya, i am receiver (receiptant) and people who will send me email are credentials... i have run with both my info and the senders info, without any result... i dont know what is wrong... thanks if you respond, kind regards
venus najadRank #2,456 · 573 reputationPosted Feb 16, 2024, 12:20 PM EST
dear mr thomas, it is not me will send email... people will send me email by using a webform...
i have signed up in sendgrid, but it doesnt work???!!!
they are asking to create a sender, but i dont know who is going to send me email... it is public...
kind regards