3
I would like to know how to send an email using ASP.NET. The idea will be to click on a button called send and send a support mail Thank you.
3
I would like to know how to send an email using ASP.NET. The idea will be to click on a button called send and send a support mail Thank you.
1
Here’s an example with Asp.net mvc4 and javascript:
MVC:
public ActionResult SendEmail()
{
var fromAddress = new System.Net.Mail.MailAddress("[email protected]", "From Name");
var toAddress = new System.Net.Mail.MailAddress("[email protected]", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new System.Net.Mail.SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new System.Net.Mail.MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
return View("Index");
}
HTML:
<a href="javascript:;" class="btn btn-primary" id="enviar">Enviar</a>
Javascript:
<script type="text/javascript">
$("#enviar").click(function () {
$.ajax({
url: '@Url.Action("SendEmail", "Home")',
type: 'POST',
data: { },
success: function (result) {
alert('Um email foi enviado com sucesso');
}
});
});
</script>
Gives "Unable to connect to the server" error. Some ideas?
@Nelsonsoares, you have to configure your Customer to send emails by smtp.sapo.pt
, the following address may be useful to you: Configuration POP/ IMAP/ SMTP... possibly Host = "smtp.sapo.pt", Port = 587, EnableSsl = true
or Host = "smtp.sapo.pt", Port = 25, EnableSsl = false
... remembering that you should use your real email and passwords.
0
Doing it in a different way, but with the same methods as @Spectron’s answer, it would look like this:
public ActionResult EnviarEmail(){
using (var smtp = new SmtpClient())
{
var message = new MailMessage();
message.To.Add(new MailAddress("[email protected]"));
message.From = new MailAddress("[email protected] ");
message.Subject = "Assunto";
message.Body = "Seu Texto Aqui";
message.IsBodyHtml = true;
var credential = new NetworkCredential
{
UserName = "[email protected]",
Password = "Senha do E-mail aqui"
};
smtp.Credentials = credential;
smtp.Host = "HOST aqui (ex: smtp.google.com)";
smtp.Port = 587; //(Porta aqui)
smtp.EnableSsl = true; //(SSL Enable)
smtp.Send(message);//Enviar mensagem
}
}
return View();
}
However, I advise you to use the Postal.MVC to make the shipment. At this Link has an example of the project author how to implement, just download the project.
Browser other questions tagged asp.net-mvc asp.net asp
You are not signed in. Login or sign up in order to post.
http://codigosimples.net/2016/03/29/envio-de-emails-simples-com-gmail/
– Jhonathan
Your question in Google will find several answers. Here http://www.mundoasp.net/enviar- email-com-asp-dot-net/ has very detailed what you need. In fact you only need to use some of the properties cited.
– Marconi
Gives "Unable to connect to the server" error. Some ideas?
– Nelson Soares