3

I want send email in asp.

I use 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 error :

Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.

How to send email with asp?

Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62
Niloo
  • 1,205
  • 5
  • 29
  • 53
  • The error message pretty clearly states: `"This mail server requires authentication"`. You don't appear to be providing any authentication. How you provide it depends on what the mail server requires. What does it require? – David Feb 02 '13 at 13:45
  • 1
    You don't have the smtp service running on the system or you procide invalid credentials (I bet on the former). – Wiktor Zychla Feb 02 '13 at 13:45

3 Answers3

4

I use 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);
Niloo
  • 1,205
  • 5
  • 29
  • 53
1

You might need to provide credentials.

example:

smtpMail.Credentials = new NetworkCredential("username", "password")
Pleun
  • 8,856
  • 2
  • 30
  • 50
0

If you are trying to send email without authenticating, I am afraid that's impossible. If any users in your web site can send emails without password, that's horrible. It will allow user to send email from other person account. So considering security, sending email will required to provide 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 didn't noticed that. They both used for same purpose. If you are using higher version (2.0 or later) of .Net framework, then use System.Net.Mail. If you use System.Web.Mail it only shows a warning saying this is deprecated. But that 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);
Jas
  • 419
  • 1
  • 6
  • 14