Niloo
Niloo

Reputation: 1215

Send email with System.Web.Mail

I want send email in asp.

I use this code

using System.Web.Mail;

MailMessage msg = new MailMessage();
msg.To = "[email protected]";
msg.From = "[email protected]";
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?

Upvotes: 3

Views: 10777

Answers (3)

Niloo
Niloo

Reputation: 1215

I use This code .

 MailMessage msg = new MailMessage();
 msg.Body = "Body";

 string smtpServer = "mail.DomainName";
 string userName = "[email protected]";
 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 = "[email protected]";
    msg.Subject = "Subject";
    msg.BodyEncoding = System.Text.Encoding.UTF8;
    SmtpMail.SmtpServer = smtpServer;
   SmtpMail.Send(msg);

Upvotes: 4

Jas
Jas

Reputation: 419

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("[email protected]");
  mail.From = new MailAddress("[email protected]");
  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);

Upvotes: 0

Pleun
Pleun

Reputation: 8920

You might need to provide credentials.

example:

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

Upvotes: 1

Related Questions