0

is it possible to send via email the file or data of an application made using c#? i have a program which will have its data stored in sqlite database at appdata. so i need to back it up regularly (or everyday) in case of accidental deletion of the data without manually sending it through internet.

If it is possible, can you help me with it? like posting here links or tuts on how to.. answers are very much appreciated.

The program create the file database.sqlite at the AppData/MyProgram folder and i want to send that file.

user3258603
  • 149
  • 2
  • 17

4 Answers4

2

I write a simple guide to do what u want.

  1. Look at the SmtpClient class and MailMessage class.
  2. You need to dump the data to a file or u can attach the sqlite file itself, as an attachment for the email.
  3. then you can use a SMTP server to send emails, take a look at this question : Sending email in .NET through Gmail
Community
  • 1
  • 1
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
1

You can send email with your file attached as attachment using .Net mail class. Below is code that send email with attachment.

var smtp = new System.Net.Mail.SmtpClient();
{
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(sFromEmail);
    string sFrom = mail.From.ToString();

    mail.Subject = sSubject;
    mail.Body = sBody;
    mail.IsBodyHtml = true;

    Attachment sMailAttachment;
    sMailAttachment = new Attachment("Your file file");
    mail.Attachments.Add(sMailAttachment);

    smtp.Host = "SMTPP HOST"
    smtp.Port = "PORT" 

    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    smtp.Credentials = new NetworkCredential(sSMTPUserName, sSMTPPassword);

    smtp.Timeout = 30000;

    smtp.Send(mail);  
  }
0

There are a bunch of articles (and StackOverflow posts) on how to send email using C#, just do an internet search on "send email c#" this post can get you started.

The only thing you really need to make sure of, is that you have an smtp server (outgoing mail server) that you have permission to send through. Usually, that means the ourgoing mail server of your company of internet service provider, but that would need checking as it tends to differ for everyone.

Also: be aware that most (if not all) "regular" smtp servers will have a cap on the amount of emails per minute or hour you can send, so don't overdo it.

Community
  • 1
  • 1
Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
  • you mean it is needed to have an smtp server setup or is it possible to use app like mandrill ? – user3258603 Apr 18 '14 at 05:48
  • everyone who's connected to the internet, is so through an ISP. That ISP will offer internet access and usually email as well (yourname@yourisp.bla) you can either try to use that ISP's smtp server to send mail or use e.g. gmail, as stated in the other answer here – Wim Ombelets Apr 18 '14 at 05:52
0
public void mail()
    {
        MailMessage sendmsg = new MailMessage("frommail", "tomail", "subject", "body"); 
        SmtpClient client = new SmtpClient("smtp.gmail.com");

        client.Port = Convert.ToInt16("587");
        client.Credentials = new System.Net.NetworkCredential("frommail", "password");
        Attachment sMailAttachment;
        sMailAttachment = new Attachment("your file");
        sendmsg.Attachments.Add(sMailAttachment);
        client.EnableSsl = true;
        client.Send(sendmsg);
    }