If you try to send email without authentication, I am afraid that this is not possible. If any users on your website can send emails without a password, this is terrible. This will allow the user to send email from another account. Therefore, given security, sending an email will be necessary for the email address and password.
var fromAddress = ""; // Email Address here. This will be the sender. string fromPassword = ""; // Password for above mentioned email address. var toAddress = "";// Receiver email address here string subject = "Hi"; string body = "Body Text here"; var smtp = new System.Net.Mail.SmtpClient(); { smtp.Host = "smtp.gmail.com"; // this is for gmail. smtp.Port = 587; smtp.EnableSsl = true; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); smtp.Timeout = 20000; } smtp.Send(fromAddress, toAddress, subject, body);
[Edited] Sorry, my mistake, I did not notice this. Both were used for the same purpose. If you are using a higher version (2.0 or later) of the .Net framework, use System.Net.Mail. If you use System.Web.Mail, it only shows a warning saying it is out of date. But it will work.
Here is the answer for System.web.mail
MailMessage mail = new MailMessage(); mail.To.Add(" to@domain.com "); mail.From = new MailAddress(" from@domain.com "); mail.Subject = "Email using Gmail"; mail.Body = ""; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; smtp.Credentials = new System.Net.NetworkCredential(mail.From,"YourPassword"); smtp.Send(mail);
source share