Sending hardcoded email with attachment - c#

I am trying to write a code for sending hard coded email with attachment i-e I don't want to use the buttons and text fields. I want when the program runs it should automatically go to location in my drive and attach some files and email it to the email address which I have already told that program while coding.
The normal code with buttons and text fields does not work. See below the normal code
MailMessage mail = new MailMessage(from.Text, to.Text, subject.Text, body.Text);
mail.Attachments.Add(new Attachment(attachment1.Text));
SmtpClient client = new SmtpClient(smtp.Text);
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);
client.EnableSsl = true;
client.Send(mail);
MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);
I have tried replacing from.Text, to.Text, subject.Text, body.Text and attachment1.Text with a string as
string from="abc#gmail.com";
string attachment1=#"c:\image1.jpg";
They give me errors.

Remove the .Text after each variable, as strings don't have a Text property.
Like this:
MailMessage mail = new MailMessage(from, to, subject, body);

Related

How do you schedule an email in C# using Gmail?

So, this is my email function, that sends an email using Gmail's SMTP:
public void Email(string subject, string body)
{
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(email, emailname);
message.To.Add(new MailAddress(targetemail, targetname));
message.Subject = subject;
smtp.EnableSsl = true;
message.IsBodyHtml = true;
message.Attachments.Add(new Attachment(folder + #"\" + cbPrezentacja.SelectedItem.ToString() + "." + extension));
message.Body = body;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(email, password);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
It works however, how would I schedule the email for a specific time? I don't mean making a Windows schedule/timer on the host (that requires leaving it on) or whatever, I want to do it via Gmail.
https://i.stack.imgur.com/eNG9D.png < This is how it works using Gmail. (sorry for the weird tint)
Is this even possible using System.Net.Mail or do I need a special Gmail API for that (if so, how?)? This is a private application for 1 computer, so it doesn't need to be super secure.
Thanks!
You can't.
There's nothing in SMTP to schedule it, when you do smtp.Send(message); the message is sent that exact moment.
Gmail has an API, but (as far I can see) it doesn't offer this functionality, so right now the only way to do it would be exactly what you say you don't want: your app should somehow wait till the desired time and send it.

Get a list of files in a folder and list them in an e-mail body

I'm new to C# and am trying to get a list of files from a directory and then send them in an e-mail. I can do both things individually, but just can't seem to work out. Here is my basic code to get a list of files:
foreach (string str in Directory.GetFiles(path))
{
Message.Print(str);
}
For my e-mail code, I have this:
SmtpClient smtpClient = new SmtpClient(server, Port);
smtpClient.Credentials = new System.Net.NetworkCredential(username, password);
smtpClient.EnableSsl = ssl;
MailAddress fromAddress = new MailAddress(sender);
MailMessage message = new MailMessage();
message.From = fromAddress;
message.Subject = "Test e-mail";
message.IsBodyHtml = false;
message.Body = "List directory content here";
message.To.Add(reciever);
smtpClient.Send(message);
No matter what I try, I just can't seem to work out how to list the directory contents in the e-mail body. Can anyone assist?
Directory.GetFiles(path) is an array, you can use string.Join to get an string out of that instead of your current foreach loop, then you just use the resulting string for message.Body.
message.Body = sting.Join(",", Directory.GetFiles(path))
This is the initial step to get it working, validations need to be done in order to make this production ready. Check Directory.GetFiles exceptions to get an idea of all the stuff that can go wrong with this code.

Sending an email from c# windows application

I have a windows c# application which is running on server, After it has been run everyday through task scheduler at certain time it generates a log file and now my issue can i send a email to my office id after it has finished running the application where there is no from address and needs to attach the log file with the email.
private void button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage("eg#king.co.uk", "user#hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = #"100.100.0.1";- fake host
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
}
Many issues in your code and question.
Your question says "how do I send an email to my office id". It's not clear what that means. Do you mean you have a company email address such as shruti#mycompany.com and you want to send an email to it?
There must be a from address. The address doesn't have to exist. For example, you would have a from address of donotreply#mycompany.com.
Your question includes the code for sending an email via Gmail's servers: client.Host = "smtp.google.com";. This makes everyone that is reading your question think you're trying to send an email via Google, which has specific requirements. If that's not the case, then update your question to be explicit. For example, if you're using a company hosted email server, you can use smtp.mycompany.com.
So if the question is really about how to attach a text file to your email, here's how you'd do that:
private void button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage("donotreply#yourcompany.com", "user#hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "smtp.mycompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
mail.Attachments.Add(new Attachment("log.txt"));
client.Send(mail);
}
Notice I removed the UseDefaultCredentials=false because you didn't specify alternate credentials.
If you know the format or name of the daily log message, you can use the following code after you create your MailMessage:
MailMessage mail = new MailMessage("you#yourcompany.com", "user#hotmail.com");
mail.Attachments.Add(new Attachment("filepath.log"));

Save and send a mail using System.Net.Mail

I'm trying to send and save the send email using C# code. But i can't get this done. I can either save the mail, or send it. But i can't get both done.
This is what i have:
public ActionResult Index()
{
MailMessage message = new MailMessage();
message.From = new MailAddress("test#mail.com");
message.To.Add(new MailAddress("mymail#gmail.com"));
message.Subject = "Test Subject";
message.Body = "This is a test message";
message.IsBodyHtml = true;
// Setup SMTP settings
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential basicCredential = new NetworkCredential("mymail#gmail.com", "******");
smtp.UseDefaultCredentials = false;
smtp.Credentials = basicCredential;
smtp.Send(message);
// save
smtp.EnableSsl = false;
smtp.PickupDirectoryLocation = #"C:\Temp";
smtp.Send(message);
return View();
}
So first i try to send the email. That works. Then i'm trying to save the email to my HDD. But it never gets saved. It does work when i don't send out the email and try to save it to my HDD right away. But i need to do both.
Anyone any idea how i can get this done? I just simply need to log the send messages.
Mail messages in the pickup directory are automatically sent by a local SMTP server (if present), such as IIS. (SmtpClient.PickupDirectoryLocation)
If you want to save to file system, you need to set the DeliveryMethod to SmtpDeliveryMethod.SpecifiedPickupDirectory:
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = #"C:\Temp";
client.Send(message);
See How to save MailMessage object to disk as *.eml or *.msg file
You have to change the property DeliveryMethod to SmtpDeliveryMethod.SpecifiedPickupDirectorynot to not send the email.
Just changing the PickupDirectoryLocation will not work, because the property is not used when DeliveryMethod is set to Network (which is the default value).
See MSDN.

gmail Conversation via smtp

how can i send an email as a part of a gmail-conversation via smtp?
Taking the same subject doesnt work...
tell me if you need more infos...
thanks in advance!
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("#googlemail.com");
mail.To.Add("#.com");
mail.Subject = "(Somee.com notification) New order confirmation";
mail.Body = "(Somee.com notification) New order confirmation";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("", "");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
You'll need to use the following:
mail.Headers.Add("In-Reply-To", <messageid>);
The message id you should be able to get from the previous email's headers. Just look for "Message-Id".
This answer gives a few more headers you may want to add to try help threading in other clients. It seems maybe gmail is now using these, too.

Categories