0

I want to sent mail to admin after customer validate commande,but when click on button valider I get this error : Mailbox not available letters. The server response was: 5.7.3 Requested Action aborted; user not authenticated. I didn't found any solution in internet.

 [HttpPost]
    public async Task<ActionResult> Index2(LigneCommandeT LigneCommandeT , string submit)
    {
        switch (submit)
        { 
            case "Ajouter" : 
        context.LigneCde.Add(LigneCommandeT);
        context.SaveChanges();
        return RedirectToAction("Index2");
             break;
            case  "Valider" :
            // var message = EMailTemplate("Welcome");
            await  MessageServices.SendEmailAsync("Namegmail.com", "Welcome!",  "message");

             return RedirectToAction("Index3");
            break;
            default :
            return RedirectToAction("Index2");
            break;
        }
    }

This is the SendMail Method :

 public async static Task SendEmailAsync(string email , string subject ,string message)
    {
        try
        {
            var _email="Name@gmail.com"; // sender's email 
            var _epass = ConfigurationManager.AppSettings["EmailPassword"]; // get our password from web.config
            var _dispName = "Sabrine";
            MailMessage  MyMessage = new MailMessage();
            MyMessage.To.Add(email);
            MyMessage.From = new  MailAddress(_email,_dispName);
            MyMessage.Subject = subject ;
            MyMessage.Body = message ;
            MyMessage.IsBodyHtml = true;

            using(SmtpClient smtp = new SmtpClient())
            {
                smtp.EnableSsl =true ;
                smtp.Host = "smtp.live.com";
                smtp.Port = 587 ;
                smtp.UseDefaultCredentials = false ;
                smtp.Credentials = new NetworkCredential(_email,_epass);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.SendCompleted   += (s,e) => {smtp.Dispose();} ; 
                await smtp.SendMailAsync(MyMessage);


            }
        }
        catch(Exception ex)
        {
            throw ex ; 
        }

This is a part of Web.config :

<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="EmailPassword" value=""  />
   </appSettings>

<system.net>
  <mailSettings>
     <smtp deliveryMethod="Network">
       <network enableSsl="true" host="smtp.live.com" port="587" defaultCredentials="false" userName="Name@gmail.com" password=""/>
     </smtp>
  </mailSettings>

Thanks,

Sarra
  • 99
  • 1
  • 2
  • 14
  • there's you haven't set any password in your config – jamiedanq Apr 24 '16 at 14:08
  • I set my password , and I changed "smtp.live.com" to "smtp.gmail.com" and I got this error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. – Sarra Apr 24 '16 at 14:20

2 Answers2

0

please edit your code as follows;

        SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
        var mail = new MailMessage();
        mail.From = new MailAddress("crazy_akin399999@hotmail.com");
        mail.To.Add("akin.********@gmail.com");
        mail.Subject = "Your Sub";
        mail.IsBodyHtml = true;
        string htmlBody;
        htmlBody = "HTML code";
        mail.Body = htmlBody;
        SmtpServer.Port = 587;

        SmtpServer.UseDefaultCredentials = false;

        SmtpServer.Credentials = new System.Net.NetworkCredential("****abdull*h**lu@gmail.com", "*****");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);

Source Link : Send email via Hotmail to gmail

Community
  • 1
  • 1
rootturk
  • 316
  • 4
  • 20
  • This is code didn"t work for me . T get the same error :( :'( . – Sarra Apr 24 '16 at 13:58
  • Hmm, I understand your problem. I try my gmail name outlook account information, I get your error, and try hotmail account information and mail sending successfully. Please change from email address different Sender email address, and try. sample : NetworkCrential("email@hotmail.com") From Mail address can not email@hotmail.com. Please change and try. I Edit my Answers, please check. – rootturk Apr 24 '16 at 14:26
  • It worked with smtp.gmail.com , I fixed the problem , by using another account with less security , thanks for your help :D – Sarra Apr 24 '16 at 19:54
  • I'm happy, No problem. :) – rootturk Apr 24 '16 at 20:06
0

The line below in your code is not getting any password or rather getting password as empty string:

var _epass = ConfigurationManager.AppSettings["EmailPassword"];

As a result of this setting your config:

<add key="EmailPassword" value=""  />

If you look clearly the value is an empty string . You have to replace it with the required Password like the example below:

<add key="EmailPassword" value="YourPassword"  />
jamiedanq
  • 967
  • 7
  • 12
  • I set my password , and I changed "smtp.live.com" to "smtp.gmail.com" and I got this error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. – Sarra Apr 24 '16 at 14:21
  • @Sarra if you are just testing if it works I suggest you maintain the `smtp.live.com` becaue it works – jamiedanq Apr 24 '16 at 14:34
  • Thanks for your time and your help , I fixed the problem , I desactivate security on my account gmail. it works :D :) – Sarra Apr 24 '16 at 19:44