Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

how i can send email automatically to gmail, yahoo or hotmail?
Actually when i Refresh the page a rough email will send to desired email address.
No buttons i want to use in this process like from or To but just want to send email Refresh the link.
i have tried below code but it doesn't work.
here is my controller:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Net.Mail;
using automatic_email.Models;
using Comfirm.AlphaMail;

namespace automatic_email.Controllers
{
    public class emailController : Controller
    {
        // GET: email
        public ActionResult Page_load()
        {
            //MailMessage mailMessage = new System.Net.Mail.MailMessage();
            //mailMessage.To.Add("my email");
            //mailMessage.CC.Add("someone email");
            //mailMessage.Subject = "Some subject";
            //mailMessage.Body = "Some text";

            //using (var smtpClient = new SmtpClient())
            //{
            //    smtpClient.Send(mailMessage);
            //}
            GMailer.GmailUsername = "my email";
            GMailer.GmailPassword = "my password";

            GMailer mailer = new GMailer();
            mailer.ToEmail = "my email";
            mailer.Subject = "Verify your email id";
            mailer.Body = "Thanks for Registering your account.<br> please verify your email id by clicking the link <br> <a href='youraccount.com/verifycode=12323232'>verify</a>";
            mailer.IsHtml = true;
            try
            {
                mailer.Send();
            }
            catch (Exception ex)
            {
                ViewBag.data = ex.ToString();
            }
            return View();
        }
    }
    
}


What I have tried:

I have Tried different methods but was unsuccessful to do it.
Posted
Updated 29-Jun-16 6:02am
v5
Comments
Dave Kreskowiak 29-Jun-16 8:34am    
Your question doesn't make any sense.

You want to send an email on every page load, even refreshes, or you do NOT want to send an email on every page load?
Jaffar Hayat 29-Jun-16 9:37am    
yes you are right,
i want send email on every page load.
thanks for correction.
jgakenhe 29-Jun-16 9:27am    
You need to remove your password and email address from this posting, unless you want to give access to everyone in the world.

Also, if you want to use it for a link, you'd do best with the link calling a method, rather than on page load.
Jaffar Hayat 29-Jun-16 9:42am    
the password i have used is wrong one, no need to worry.
and how i can resolve my problem with link calling method Please elaborate.
Richard Deeming 29-Jun-16 10:18am    
You seem to be confusing WebForms and MVC. The Page_Load(object sender, EventArgs e) is a WebForms artefact, and has no place in an MVC controller.

1 solution

First, remove your email address from the code snippets. Spammers have bots that comb through sites, including this one, looking for email addresses. If you love getting spam, fine, leave it in there. If not, remove it.

Next, MVC doesn't have any concept of a Page_Load event. I think you really need to pickup a book on MVC and work through it.

You could put this code inside the Index method of a controller and it would do what you're asking. Sending an email doesn't change no matter which framework you use.
 
Share this answer
 
Comments
Jaffar Hayat 29-Jun-16 12:07pm    
Thanks.
but after running the application an exception occur on smtpClient.Send(mailMessage);
Dave Kreskowiak 29-Jun-16 12:12pm    
Don't you think it would be a good idea to tell us what the exception message is? I mean it's only the most important piece of information telling you where the problem might be...

Nevermind. Now that you've down voted the answer I gave for no reason at all other than your code ran into another problem that nobody could foresee, I don't care.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900