Sending emails with outlook reminder - c#

Introduction
I currently have a C# method that allows me to send emails (see below). The emails are being sent via a Microsoft Exchange server, and the emails are read using an Outlook client. I would like to add additional functionality to this method so that I can include a follow-up reminder for 24 hours after the send time. From what I have read online, I need to use Microsoft.Office.Interop.Outlook, but I haven't been able to figure out how to use it.
Current method
using System.Net.Mail;
public void SendEmail(string host, string un, string pw, string from, string to, string subject, string body)
{
MailMessage email = new MailMessage(from, to);
email.Subject = subject;
email.Body = body;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(un, pw);
client.Host = host;
client.Send(email);
}
What I Have Tried
I believe I need to use the Microsoft.Office.Interop.Outlook.MailItem class to create the email instead of the MailMessage class I have been using. However I am unsure about 2 things:
How to connect to the server and send the email. I tried using the SmtpClient class as I was before, but I can't pass it a MailItem.
I'm not sure if my code to create the reminder is correct. I haven't been able to send the message, so I also haven't been able to see if the reminder is displayed in Outlook correctly. If anyone has experience using reminders, comments on how I am creating the reminder would also be appreciated.
Here is the code I have used to create the email and reminder:
using Microsoft.Office.Interop.Outlook;
public void SendEmail(string host, string un, string pw, string from, string to, string subject, string body)
{
MailItem email = new MailItem();
email.ReminderTime = new DateTime();
email.ReminderTime = DateTime.Now;
email.ReminderTime.AddDays(1);
email.ReminderSet = true;
email.Subject = subject;
email.Body = body;
}

Related

Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email .

The official Gmail API documentation is horrendous. Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email.
It would be a three step process -
Define an HTML template which which describes how your mail should be presented.
Write a small c# code to replace all place holders like your form fields , user name, etc.
private string createEmailBody(string userName, string title, string message)
{
string body = string.Empty;
//using streamreader for reading my htmltemplate
using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName); //replacing the required things
body = body.Replace("{Title}", title);
body = body.Replace("{message}", message);
//// Instead of message add you own parameters.
return body;
}
When form is submitted, call step 2 code first. Then use it's output to set mail body.
Code would be something like -
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
/// This mail from can just be a display only mail I'd
string emailFrom = "no-reply#gmail.com";
string subject = "your subject";
string body = createEmailBody();
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
/// Add more to IDs if you want to send it to multiple people.
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// This is required to keep formatting of your html contemts
/// Add attachments if you want, this is optional
mail.Attachments.Add(new Attachment(yourfilepath));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(your-smtp-account-email, your-smtp-account-password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Refer this link for working example
https://www.c-sharpcorner.com/UploadFile/33b051/sending-mail-with-html-template/
EDIT: For using GMail API
Using GMAIL APIs you will need two nuget packages:
1. Install-Package Google.Apis.Gmail.v1
2. Install-Package AE.Net.Mail
Code is very similar to what we have for normal SMTP mail send. It is explained at: http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/

send an email using SMTP without password in C#

I have a web application using ASP.net and C#,in one step it will need
from the user to
send an email to someone with an attachments.
my problem is when the user will send the email i don't want to put their
password every time the user send.
i want to send an email without the password of the sender.
any way to do that using SMTP ?
and this is a sample of my code "not all".
the code is worked correctly when i put my password , but without it ,it
is not work, i need a way to send emails without put the password but
in the same time using smtp protocol.
private void button1_Click(object sender, EventArgs e)
{
string smtpAddress = "smtp.office365.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "my email";
string password = "******";
string emailTo = "receiver mail";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
// mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
// mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress,portNumber))
{
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
MessageBox.Show("message sent");
}
}
I believe this can be accomplished easily, but with some restrictions.
Have a look at the MSDN article on configuring SMTP in your config file.
If your SMTP server allows it, your email object's from address may not need to be the same as the credentials used to connect to the SMTP server.
So, set the from address of your email object as you already are:
mail.From = new MailAddress(emailFrom);
But, configure your smtp connection one of two ways:
Set your app to run under an account that has permission to access the SMTP server
Include credentials for the SMTP server in your config, like this.
Then, just do something like this:
using (SmtpClient smtp = new SmtpClient())
{
smtp.Send(mail);
}
Let the configuration file handle setting up SMTP for you. This is also great because you don't need to change any of your code if you switch servers.
Just remember to be careful with any sensitive settings in your config file! (AKA, don't check them into a public github repo)

c# Sending emails in threads using 1 connection

I have a program for sending my resume to emails of people who posted jobs if they included their email in their post.
so I send an email with their quote of the job description, date of when it was generated etc,
so each email is unique, But each email uploads the same file (resume.pdf) as Attachment.
right now each time I send an email I need to upload the same file (resume.pdf) // my resume
so this are my questions:
can I send each email and only upload my pdf resume once?
right now I use a smtp client library like this:
GMailSmtp gmail = new GMailSmtp(new NetworkCredential("username", "password"));
so each time I send an email I create a thread that opens a new connection which seems time consuming to me.
I was wondering if there is an API or library to create 1 connection and then send all the emails I want thru a queue or create a new thread just for sending the email.
Yes. If you're using a third-party server like Gmail, you will need to upload your resume with each email. BUT, there are lots of easy ways to do this in the background.
Play with this for a while and if you have specific questions or problems, post your code and your specific issue:
List<string> recipients = new List<string>();
BackgroundWorker emailer = new BackgroundWorker();
public void startSending()
{
emailer.DoWork += sendEmails;
emailer.RunWorkerAsync();
}
private void sendEmails(object sender, DoWorkEventArgs e)
{
string attachmentPath = #"path to your PDF";
string subject = "Give me a JOB!";
string bodyHTML = "html or plain text = up to you";
foreach (string recipient in recipients)
{
sendEmail(recipient, subject,bodyHTML,attachmentPath);
}
}
private void sendEmail(string recipientAddress, string subject, string bodyHTML,string pathToAttachmentFile)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add(recipientAddress);
mail.Subject = subject;
mail.Body = bodyHTML;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(pathToAttachmentFile);
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
Note that BackgroundWorker requires a reference to System.ComponentModel.
You could use separate thread for sending e-mails. For example you can do it as Shannon Holsinger suggested. You could also upload your resume to Dropbox or anywhere else and send the link instead of attaching a file.

Change sender name doesn't work

First of all, I search for an hour how to solve my problem on other posts but the other solutions don't work in my case.
My problem
I need to send a report mail after the execution of my program. To send mails I use System.Net.Mail namespace and particularly SmtpClient class.
The mail is correctly sent but I need to hide the sender mail address.
I tried some different things but none of them seems to work.
What I tried
Firstly I tried to do this :
public static void sendMail(String Titre,String Message)
{
SmtpClient client = new SmtpClient(GestionParametres.getParametre("SMTP"), Int32.Parse(GestionParametres.getParametre("PortSmtp")));
client.Credentials = new System.Net.NetworkCredential(GestionParametres.getParametre("UsernameSmtp"), GestionParametres.getParametre("PasswordSmtp"));
MailAddress from = new MailAddress(GestionParametres.getParametre("ExpediteurMail"),"Rapport interface ****");
MailAddress to = new MailAddress(GestionParametres.getParametre("DestinataireMail"));
MailMessage message = new MailMessage(GestionParametres.getParametre("ExpediteurMail"), GestionParametres.getParametre("DestinataireMail"));
message.From = from;
message.Subject = Titre;
message.Body = Message;
message.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(message);
}
But the sender mail address still appear in mail.
Secondly, I tried this :
public static void sendMail(String Titre,String Message)
{
SmtpClient client = new SmtpClient(GestionParametres.getParametre("SMTP"), Int32.Parse(GestionParametres.getParametre("PortSmtp")));
client.Credentials = new System.Net.NetworkCredential(GestionParametres.getParametre("UsernameSmtp"), GestionParametres.getParametre("PasswordSmtp"));
MailAddress from = new MailAddress(GestionParametres.getParametre("ExpediteurMail"));
MailAddress to = new MailAddress(GestionParametres.getParametre("DestinataireMail"));
MailMessage message = new MailMessage("Rapport interface ****" + GestionParametres.getParametre("ExpediteurMail"), GestionParametres.getParametre("DestinataireMail"), Titre, Message);
client.Send(message);
}
But it doesn't work either...
Now I have no idea how to solve this problem.
Any idea ?
Thank you in advance,
Thomas

Unable to send email through local SMTP server

I am working with sending email through C# in asp.net, with IIS7.
But I couldn't fix the error.
I cannot even send a message to this path
C:\inetpub\mailroot\Queue
This is the simplest code I have used
MailMessage m = new MailMessage();
m.From = TextBox1.Text;
m.To = TextBox2.Text;
m.Subject = TextBox3.Text;
m.Body = TextBox4.Text;
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send(m);
note: that I manually created this path, isn't wrong? or should be created automatically?
Please HELP!
If you manually created the path, then the SMTP Service is not installed on your machine (assuming that you intend to use this and not a third-party SMTP server).
You can verify this by looking in the list of services (I believe it starts with Simple Mail Transport...).
Perhaps it's the SmtpCredentials that's missing? And as the earlier speekers have said that the SmtpServer is it really localhost, meaning that you have a smtpserver on your machine?
I've done a SMTP mail to send som emails from my program that is working, perhaps you can find something in this that can help you?
It looks like this.
public class Email
{
public Email(string recieverAdress)
{
mail = new MailMessage(senderAdress, recieverAdress);
}
private readonly MailMessage mail;
private readonly SmtpClient smtpClient = new SmtpClient("smtp.domain.com", port);
private readonly NetworkCredential credential = new NetworkCredential("username", "password");
public void SendMail(string subject, string textInBody)
{
mail.Subject = DateTime.Now + " " + subject;
mail.Body = textInBody;
smtpClient.Credentials = credential;
smtpClient.Send(mail);
}
}

Categories

Resources