0

I have a SendMail action method and a SendMailByAttach method but I don't know why my SenMailByAttach method is not working and going to catch, I set enable ssl and ... but it does not work!

My SendMail action method is working correctly, but I think my problem is in the SendMailByAttach method, I get my input in html correctly:

public static bool SendMailByAttach(string subject, string body, string attachment, params string[] toMails)
{
    try
    {
        var mailMsg = new MailMessage();
        mailMsg.BodyEncoding = Encoding.UTF8;
        mailMsg.HeadersEncoding = Encoding.UTF8;
        mailMsg.SubjectEncoding = Encoding.UTF8;
        mailMsg.Priority = MailPriority.High;
        mailMsg.Subject = subject;
        mailMsg.Body = body;
        mailMsg.IsBodyHtml = true;
        mailMsg.From = new MailAddress("maxspeed201@gmail.com", "مهران ", Encoding.UTF8);
        mailMsg.Sender = new MailAddress("maxspeed201@gmail.com", "مهران ", Encoding.UTF8);
        mailMsg.Attachments.Add(new Attachment(attachment));
        foreach (var mail in toMails)
        {
            mailMsg.To.Add(new MailAddress(mail));
        }
        var smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = true;
        smtp.Timeout = 1000;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential("MyEmail", "MyPasswordEmail");
        smtp.Send(mailMsg);
        return true;
    }
    catch
    {
        return false;
    }
}

This is my action method:

public ActionResult SendMail(string recivers, string title, string text, HttpPostedFileBase attachment)
{
    bool result = false;
    if (attachment != null)
    {
        string path = Server.MapPath("~") + "\\Files\\Attachment\\" + Path.GetFileName(attachment.FileName);
        attachment.SaveAs(path);                
        //az ultility sendemailbyAttachment etefade kardim
        result = MailSender.SendMailByAttach(title, text, path, recivers.Split(','));
    }
    else
    {
        result = MailSender.SendMail(title, text, recivers.Split(','));
    }
    if (result)
    { //Success in sending
        return MessageBox.Show("پیام با موفقیت ارسال شد", MessageType.Success);
    }
    else
    {
        return MessageBox.Show("پیام ارسال نشد", MessageType.Error);
    }
}

My SendMailByAttach method is going to catch

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mehran
  • 45
  • 1
  • 6
  • 1
    Debugging 101: in the `catch` block - inspect the **exception** object and see what it tells you! – marc_s Dec 24 '18 at 09:04
  • 2
    Did you do this? https://stackoverflow.com/a/32475872/2946329 – Salah Akbari Dec 24 '18 at 09:06
  • You could -- for instance -- actually check WHAT exception is thrown. This could give you a hint what's going wrong ... – derpirscher Dec 24 '18 at 10:08
  • i trace my code and every thing is okay, and My inputs like my Reciver's,title and text taken and send to my method but idk why going to exception Thats Error Exception(The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at) !!!! – Mehran Dec 24 '18 at 18:56

1 Answers1

0

you may be trying to send email using gmail in localhost, if that is the case then login to your google account from which you are sending an email and go to this link and allow less secure apps.

If you are facing the problem at the remote server then you have to disable 'Captcha' and need to allow less secure app by visiting above mentioned link. To disable 'Captcha', go to this link.

Hope this helps.

ecstatic
  • 114
  • 1
  • 9