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