Use ex.ToString() rather than ex.Message to get full details and in particuliar the base exception. More likely the problem is using the correct settings for the mail server you are using rather than really a coding issue. You are still
trying with Gmail ?
yes i am still trying with Gmail i don't know how to settings for the mail server
here is my code
sendemail.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace SendEmail.Models
{
public class SendEmail
{
[Required]
[RegularExpression(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$", ErrorMessage = "Invalid email")]
public string to { get; set; }
[Required]
public string subject { get; set; }
[Required]
public string body { get; set; }
public HttpPostedFileBase[] file { get; set; }
}
}
sendemail controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using System.IO;
namespace SendEmail.Controllers
{
public class SendEmailController : Controller
{
// GET: SendEmail
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Index(SendEmail.Models.SendEmail sendEmail)
{
// string path = "~/Content/Upload/" + Guid.NewGuid() + "/";
string path = "~/Content/Upload/" + Guid.NewGuid().ToString() + "/";
if (ModelState.IsValid)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("zhyanadil.it@gmail.com");
mailMessage.To.Add(new MailAddress(sendEmail.to));
mailMessage.CC.Add(new MailAddress("chnar.ise.1992@gmail.com"));
mailMessage.Bcc.Add(new MailAddress("chnar.ise.1992@gmail.com"));
mailMessage.Subject = sendEmail.subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = sendEmail.body;
if (sendEmail.file[0] != null)
{
Directory.CreateDirectory(Server.MapPath(path));
foreach (HttpPostedFileBase file in sendEmail.file)
{
string filePath = Server.MapPath(path + file.FileName);
file.SaveAs(filePath);
mailMessage.Attachments.Add(new Attachment(Server.MapPath(path + file.FileName)));
}
}
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("zhyanadil.it@gmail.com", "***");
client.Host = "relay-hosting.secureserver.net";
try
{
client.Send(mailMessage);
ViewBag.Result = "Mail Sent";
}
catch (Exception ex)
{
ViewBag.Result = ex.Message;
}
}
return View();
}
[HttpPost]
public ActionResult Reset()
{
return RedirectToAction("Index");
}
}
}
can you tell what is wrong with my code? and where should i use 587 port? can you run my sample on your side?
Your code never sets the port. The community has no idea what port "relay-hosting.secureserver.net" uses. As recommend in all your recent posts on this subject.... It is up to you to read the docs. In my experience, email relays are setup by the network
folks and only internal server can use the relay. If you purchased a relay service then READ THE DOCUMENTATION!!!!
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("zhyanadil.it@gmail.com", "***");
client.Host = "relay-hosting.secureserver.net";
client.Port = 25
Secondly, if you are trying to use your gmail login for the relay then you are totally lost.
Member
75 Points
513 Posts
error code via runing
Jul 27, 2020 12:28 PM|zhyanadil.it@gmail.com|LINK
when in i click on submit i get exception at this line
string path = "~/Content/Upload/" + Guid.NewGuid() + "/";
here is example
https://www.yogihosting.com/asp-net-send-email/
Contributor
4923 Points
4198 Posts
Re: error code via runing
Jul 27, 2020 01:11 PM|DA924|LINK
string path = "~/Content/Upload/" + Guid.NewGuid().tostring() + "/";
maybe, the line should look like above.
All-Star
48490 Points
18071 Posts
Re: error code via runing
Jul 27, 2020 01:12 PM|PatriceSc|LINK
Hi,
Or a compile time error? What if you try :
string
path =
"~/Content/Upload/"
+ Guid.NewGuid().ToString() +
"/"
;
Member
75 Points
513 Posts
Re: error code via runing
Jul 27, 2020 01:14 PM|zhyanadil.it@gmail.com|LINK
when i click on submit i get break point at this line
</div> </div>string
path =
"~/Content/Upload/"
+ Guid.NewGuid().ToString() +
"/"
;
the exception is not visible
Member
75 Points
513 Posts
Re: error code via runing
Jul 27, 2020 01:20 PM|zhyanadil.it@gmail.com|LINK
now i don't have any error and exception but the email is not send this message appear
Failure sending mail.
can you run that example on your side?i mentioned the link
All-Star
48490 Points
18071 Posts
Re: error code via runing
Jul 27, 2020 01:48 PM|PatriceSc|LINK
Use ex.ToString() rather than ex.Message to get full details and in particuliar the base exception. More likely the problem is using the correct settings for the mail server you are using rather than really a coding issue. You are still trying with Gmail ?
Member
75 Points
513 Posts
Re: error code via runing
Jul 27, 2020 03:21 PM|zhyanadil.it@gmail.com|LINK
yes i am still trying with Gmail i don't know how to settings for the mail server
here is my code
sendemail.cs
sendemail controller
index.cshtml
i don' have any error with code
can you tell me how can i setting up my mail server?
Contributor
4923 Points
4198 Posts
Re: error code via runing
Jul 27, 2020 03:50 PM|DA924|LINK
yes i am still trying with Gmail i don't know how to settings for the mail server
You need to be sending email on port 587.
https://www.hesk.com/knowledgebase/?article=72
Member
75 Points
513 Posts
Re: error code via runing
Jul 27, 2020 03:55 PM|zhyanadil.it@gmail.com|LINK
can you tell what is wrong with my code? and where should i use 587 port? can you run my sample on your side?
All-Star
53001 Points
23587 Posts
Re: error code via runing
Jul 27, 2020 04:56 PM|mgebhard|LINK
Your code never sets the port. The community has no idea what port "relay-hosting.secureserver.net" uses. As recommend in all your recent posts on this subject.... It is up to you to read the docs. In my experience, email relays are setup by the network folks and only internal server can use the relay. If you purchased a relay service then READ THE DOCUMENTATION!!!!
Secondly, if you are trying to use your gmail login for the relay then you are totally lost.
Member
75 Points
513 Posts
Re: error code via runing
Jul 27, 2020 05:59 PM|zhyanadil.it@gmail.com|LINK
also email was not sent i think i have error at this line
client.Host = "smtpout.asia.secureserver.net";
how can i find my gmail host?
please read this article https://www.yogihosting.com/asp-net-send-email/
Contributor
5961 Points
2466 Posts
Re: error code via runing
Jul 29, 2020 04:18 AM|KathyW|LINK
smtp.gmail.com, port 465, SSL
so: