user3258603
user3258603

Reputation: 169

Send data file via email using C#

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.

Upvotes: 0

Views: 2274

Answers (4)

Muthulakshmi S
Muthulakshmi S

Reputation: 32

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);
    }

Upvotes: 0

user3497034
user3497034

Reputation:

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);  
  }

Upvotes: 1

Parimal Raj
Parimal Raj

Reputation: 20565

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

Upvotes: 2

Wim Ombelets
Wim Ombelets

Reputation: 5265

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.

Upvotes: 0

Related Questions