简体   繁体   中英

c# send uploaded file through email daily using asp.net web application

I just want to know, how to make a web-application which takes input of Excel files from user and send an email daily if that excel file has any new data available.

Should I use web service (with installation of that service in local computer) ? and yes then how to add Upload User-Interface. or Should I use only a web application and Should I add web-service in that application ?

Pls give way to come out of this problem.

Thankyou.

Depends on the nature of the application you're building:

  1. If you want the user to access this over internet, create it as an ASP.NET application (UI) and a Windows Service / Windows Task Scheduler on the Server to send out scheduled emails.

  2. If you want the user to run the application from his/her local machine/server, create a windows forms application (UI) and a Windows Service / Windows Task Scheduler on the local machine to send out scheduled emails.

No need of installations to the user computer. You need a simple Web Application and a web server, of course. There are many tutorials about getting started with web applications here are some links:

  1. ASP.NET WebForms - obsolete, yet powerful web app paradigm. However, I do recommend the next option, especially if you don't have any previous experience with desktop applications.

  2. ASP.NET MVC - this is the modern way of building web applications.

Once you made your mind about what technology to use, you gonna need so called SMTP server; usually the ISP provides one for you and you gonna need to add a configuration to the web.config, such as:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network host="*SMTP server IP address*" port="25" />
        </smtp>
    </mailSettings>
</system.net>

You can take a look at this SO post for details on how to accomplish this in ASP.NET MVC.

If you really want to it as background job on a Asp.Net WebApp you should look into:


Quartz.Net

Create a job to send e-mail

public class SendMailJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        ...Do your stuff;
    }
}

Then configure your job to execute daily

// define the job and tie it to our SendMailJob class
IJobDetail job = JobBuilder.Create<SendMailJob>()
    .WithIdentity("job1", "group1")
    .Build();

// Trigger the job to run now, and then repeat every 24 hours
ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("trigger1", "group1")
    .StartNow()
    .WithSimpleSchedule(x => x
        .WithIntervalInHours(24)
        .RepeatForever())
    .Build();

HangFire

RecurringJob.AddOrUpdate(
    () => YourSendMailMethod("email@email.com"),
    Cron.Daily);

IHostedService (only in Asp.Net Core)

public class SendMailHostedService : IHostedService, IDisposable
{
    private readonly ILogger<SendMailHostedService> _logger;
    private Timer _timer;

    public SendMailHostedService(ILogger<SendMailHostedService> logger)
    {
        _logger = logger;
    }

    public Task StartAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation("Hosted Service running.");

        _timer = new Timer(DoWork, null, TimeSpan.Zero, 
            TimeSpan.FromSeconds(5));

        return Task.CompletedTask;
    }

    private void DoWork(object state)
    {
        //...Your stuff here

        _logger.LogInformation(
            "Timed Hosted Service is working. Count: {Count}", executionCount);
    }

    public Task StopAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation("Timed Hosted Service is stopping.");

        _timer?.Change(Timeout.Infinite, 0);

        return Task.CompletedTask;
    }

    public void Dispose()
    {
        _timer?.Dispose();
    }
}

In your startup.cs class. add this at configure method.

services.AddHostedService<SendMailHostedService>();

If do not need to host it as a backgroud job on your WebApp, then you can create a Windows Service that runs every day on the time you need.

See this question: Windows service scheduling to run daily once a day at 6:00 AM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM