mrjimoy_05
mrjimoy_05

Reputation: 3568

Sending e-mail in ASP .Net

How to send e-mail on ASP .Net using outlook address??

I've tried this code but no luck:

Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

mailMessage.From = New System.Net.Mail.MailAddress("fromAddress")
mailMessage.To.Add(New System.Net.Mail.MailAddress("toAddress"))

mailMessage.Priority = Net.Mail.MailPriority.High
mailMessage.Subject = "Subject"
mailMessage.Body = "test"

Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("xxx.outlook.com", portNumber)

smtpClient.Send(mailMessage) //--> got error here

But while I'm execute the code, it got this error message:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

I've tried to add a line of code:

smtpClient.Credentials = New System.Net.NetworkCredential(username, password)

But still can't send the e-mail.

Anyone can help?

Upvotes: 2

Views: 2854

Answers (6)

Dennis Bottjer
Dennis Bottjer

Reputation: 11

was having the same error, moved the client.UseDefaultCredentials = false; before setting client.Credentials and everything works.

Upvotes: 0

Vikram Rathore
Vikram Rathore

Reputation: 1

      public static bool SendMail(string mailAccount, string password, string to, string subject, string message)
      {
        try
        {
         NetworkCredential loginInfo = new NetworkCredential(mailAccount, password);
         MailMessage msg = new MailMessage();
         msg.From = new MailAddress(from);
         msg.To.Add(new MailAddress(to));
         msg.Subject = subject;
         msg.Body = message;
         msg.IsBodyHtml = false;
         SmtpClient client = new SmtpClient("smtp.gmail.com");
         client.EnableSsl = true;
         client.UseDefaultCredentials = false;
         client.Credentials = loginInfo;
         client.Send(msg);

         return true;
        }
       catch (Exception)
       {
         return false;
       }

   }

I use this code to send mail from my gmail account.

Upvotes: 0

Niranjan Singh
Niranjan Singh

Reputation: 18290

Try these settings check for .EnableSSL property to true/false and the smtp post number on which your mail server listen for outgoing mail.

When you do not set enable the SSL settings in outlook for gmail outlook configuration then it gives same error but the reason was found the SSL settings..

well try this may it will solve your problem..

msg.IsBodyHtml = true;
            msg.Body = mailContent;

            SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
            NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = NetCrd;
            mailClient.EnableSsl = false;
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(msg);

if it does not solve your problem then check your mail server/ client for the problem.

Upvotes: 0

Grrbrr404
Grrbrr404

Reputation: 1805

  • Try smtpClient.UseDefaultCredentials = false; before you set new Credentials
  • Try to set smtpClient.EnableSsl to true / false depending on your environment

Upvotes: 1

prema
prema

Reputation: 427

I did it using c#.This may help you.Please check.

   MailMessage msg1 = new MailMessage();
                        msg1.From = strEFrom;
                        msg1.To = strETo;
                        msg1.Cc = strECC;
                        msg1.Subject = "Hi";
                        msg1.Priority = MailPriority.High;
                        msg1.BodyFormat = MailFormat.Html;
                        msg1.Body = strBody;
                        SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
                        SmtpMail.Send(msg1);

and in web.config file

<appSettings>
          <add key="MailServer" value=""/>
</appSettings>

Upvotes: 0

Henning Krause
Henning Krause

Reputation: 5422

I'm guessing you are using Exchange 2007 or later as backend?

Anyway, your mail server does not allow you to send mails anonymously. You'll either need to supply a username/password in your code or allow unauthenticated relaying from your webserver.

Talk to your IT guys what they prefer.

Upvotes: 1

Related Questions