Send data file via email using C# - 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.

I write a simple guide to do what u want.
Look at the SmtpClient class and MailMessage class.
You need to dump the data to a file or u can attach the sqlite file itself, as an attachment for the email.
then you can use a SMTP server to send emails, take a look at this question : Sending email in .NET through Gmail

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

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.

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

Related

GMail API / OAUTH2 - How To Send Emails With Traditional C# SMTP Method Being Deprecated?

As per notice from Google's Less Secure App Deprecation Notice, on May 30, 2022 the ability access Google accounts through 'less secure apps' is being deprecated. The C# code below is how I have been sending emails from my own personal GMail account. I only send emails from my app. I do not do anything else (read emails, create drafts, etc...) from this account, only sending. Basically, it is the equivalent of sending "no-reply" emails.
// C# code
using (SmtpClient smtpClient = new SmtpClient(SmtpDomain, SmtpPortNumber))
{
smtpClient.DeliveryMethod = SmtpDeliveryMethod;
smtpClient.UseDefaultCredentials = UseDefaultCredentials;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(SmtpUsername, SmtpPassword);
MailMessage mailMessage = new MailMessage { /* ... */ };
smtpClient.Send(mailMessage);
}
I've started looking at Google's API and OAuth documentation but I feel like I am going down a dark rabbit hole here since I am not wanting to access other users data, just only send emails from my own account. I assume from research that OAuth2 is the preferred way to do things when at all possible but I can't find a clear cut of example of "if you are doing it this way now", "then start doing this way". Can someone point me to the absolute starting point of the path I need to take to update my code so that I can continue sending emails from my own personal account/app?
Go to your google account and create an apps password once this is created just use that instead of the actual password for your gmail account.
Security
// C# code
using (SmtpClient smtpClient = new SmtpClient(SmtpDomain, SmtpPortNumber))
{
smtpClient.DeliveryMethod = SmtpDeliveryMethod;
smtpClient.UseDefaultCredentials = UseDefaultCredentials;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(SmtpUsername, appsPasswrod);
MailMessage mailMessage = new MailMessage { /* ... */ };
smtpClient.Send(mailMessage);
}

Sending an SMTP email through GMail with reply support

I am creating a gmail SMTP client like so:
client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential(email, password);
client.Timeout = 20000;
And sending an email like so:
MailMessage msg = new MailMessage();
msg.From = new MailAddress(myemail-no-reply#gmail.com);
msg.To.Add(to);
msg.Subject = subject;
msg.Body = body;
client.Send(msg);
I wish that if someone replies to my email it'll go to the admins email address, not to my application, and also ideally for them not to even know about the existence of the application's email address.
I could have my application forward emails received to the admin, but that's an ugly solution.
I understood from other threads that there's no way to send an email with a "reply to" field.
What I'm wondering is what if I have the admin's email as a "Send mail as" registered account on the application's GMail account.
So through the GMail interface I can send an email as if I'm the admin, can I do this from my c# application?
I know that I can set the from field of the msg sent to the admin's email, but I'm wondering if this is the correct approach, if gmail supports this, and if the email doesn't have more likelihood of ending up in the spam folder.
I have given authority to "Send mail as" in the application e-mail as the admin, and simply editing the "from" field to be sent as the admin's email address appears to be working flawlessly.
So I guess this is one of those cases that the simplest solution is the correct one.
Edit: It appears that editing the "from" field doesn't allow you to send from a different address, but what does work is setting up the default email to send emails from as a different email in the GMail "send mail as" options.

avast VS my Programm

I have problem with my program. I make a simple mail-messenger, and in this code :
var mail = new MailMessage();
var smtpServer = new SmtpClient("smtp.mail.ru");
smtpServer.Port = 25;
smtpServer.Credentials = new
NetworkCredential("#mail.ru", "pass");
smtpServer.EnableSsl = true;
mail.Body = text;
mail.From = new MailAddress("#mail.ru");
mail.To.Add("#mail.ru");
smtpServer.Send(mail); // in this moment
Avast find the idp generic in moment of mail send. The other antiviruses (eset32, Kaspersky,cureit) do not see problems and danger in code. I also was try to send mail with mailkit.dll. how can I fix it?
Your best bet is probably using the standard SMTP client-to-server email submission port: 587.
smtpServer.Port = 587;
If you, for whatever reason, can't use the standard email submission port, use the Avast GUI to make exceptions. Doing it through the antivirus directly should allow your program to run on your machine, and that's all you want, right? ;)
You can try:
Excluding your build directory from live scans
Authorizing your process to use port 25
Avast help pages for further reading

How to send message to hotmail using asp.net without user password?

I have a web page, In CONTACT US tab I have a forms user can only enter their Name, Email Id, Subject and message. Once they click the ok button I want to get those message to my hotmail account.
I tried some code. But it doesn't work.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(txtUserEmail.text, txtPassword);
smtp.EnableSsl = true;
MailMessage msg = new MailMessage();
msg.Subject = "Demo";
msg.Body = "Hi there..";
string toAddress = "xxx#hotmail.com"; // Add Recepient address
msg.To.Add(toAddress);
string fromAddress = "\"no reply \" <from#gmail.com>";
msg.From = new MailAddress(fromAddress);
msg.IsBodyHtml = true;
try
{
smtp.Send(msg);
}
catch
{
throw;
}
This code I tried. But it's having a password. I want user to send email without password to my xxx#hotmail.com
Here is my form design
That depends on your host. Usually web hosts give you a local SMTP server, then you can use it to send any mail you want, just need to know the configuration settings and use them with the SmtpClient.
If your host doesn't offer smtp (very strange unless you're selfhosting the page) you can:
1-Install a local SMTP server (if you manage the server), this is the preferred solution.
2-Use an external service like google to send the mails, but then you need to create an account on the service and use these credentials, and have in account that Google has a lot of restrictions sending emails (limit per second, marking mails as spam, etc etc).
Preferred way to do this is to not have the mail be sent from the users e-mail, but rather have a dummy e-mail that sends the mails and contains the data the user entered. Not the best solution probably, but it doesn't require user credentials.

SMTP mail only sends internally

I've got a c# function that sends emails from the win app to me via SMTP from a gmail account.
It works, but only if the FROM parameter is internal.
When I add an external address, it runs without errors, but the email never arrives in my mailbox.
I've noticed several posts on this site with similar issues, but the difference with most seems that everyone else gets some kind of error message.
MailMessage mail = new MailMessage();
using (SmtpClient SmtpServer = new SmtpClient(smtp.gmail.com))
{
mail.From = new MailAddress("you#myGoogleDomain.com"); //This works
//mail.From = new MailAddress("me#somewhere.com"); //This does not work
mail.To.Add("recipient#myGoogleDomain.com");
mail.Subject = "Mail Subject";
mail.Body = mailBody;
mail.ReplyTo = new MailAddress("me#myGoogleDomain.com");
mail.CC.Add(new MailAddress("me#myGoogleDomain.com"));
mail.IsBodyHtml = true;
SmtpServer.Port = 587; //I've also tried 465
//SmtpServer.UseDefaultCredentials = false; //This does not make a difference.
SmtpServer.Credentials = new System.Net.NetworkCredential("myaccount#myGoogleDomain.com", "Pass123");
SmtpServer.EnableSsl = true; //I've also tried false
//Add attachments
...
//Send the mail
SmtpServer.Send(mail);
}
MORE INFORMATION: I notice that the emails sits in the myaccount#myGoogleDomain.com 'Sent' box. Just no mails coming through to recipient#myGoogleDomain.com.
You are sending email using Google smtp server on behalf of your account in Google. You can't use Google smtp with non-google account to From. Even if you are sending regular email in Gmail or other email you can't modify From. Same behavior in this code you posted.
The problem was not really with the From param, but with the ReplyTo. Since both params are set from the same variable (and not strings), it did not work when it was set to outside my domain.
Removing the ReplyTo settings solved the problem.

Categories

Resources