Send an email using System.Web.Mail

I want to send an email to asp.

I am using this code

using System.Web.Mail; MailMessage msg = new MailMessage(); msg.To = " aspnet@yahoo.com "; msg.From = " info@mysite.com "; msg.Subject = "Send mail sample"; msg.BodyFormat = MailFormat.Html; string msgBody="Hello My Friend. This is a test."; msg.Body = msgBody ; SmtpMail.SmtpServer = "localhost"; SmtpMail.Send(msg); 

But I get the error:

Bad sequence of commands. Server response: This mail server requires authentication when trying to send to a non-local email address. Check your mail client settings or contact your administrator to make sure that the domain or address is defined for this server.

How to send an email with asp?

+3
source share
3 answers

I am using this code.

  MailMessage msg = new MailMessage(); msg.Body = "Body"; string smtpServer = "mail.DomainName"; string userName = " info@mysite.com "; string password = "MyPassword"; int cdoBasic = 1; int cdoSendUsingPort = 2; if (userName.Length > 0) { msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer); msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25); msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort); msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic); msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName); msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); } msg.To = user.Email; msg.From = " info@Mysite.com "; msg.Subject = "Subject"; msg.BodyEncoding = System.Text.Encoding.UTF8; SmtpMail.SmtpServer = smtpServer; SmtpMail.Send(msg); 
+3
source

You may need to provide credentials.

Example:

 smtpMail.Credentials = new NetworkCredential("username", "password") 
+2
source

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); 
+1
source

Source: https://habr.com/ru/post/1234289/


All Articles