filesystemwatcher event is not triggering when excel file data are changed - c#

I have made a web service which uses object FileSystemWatcher. Now When I try to open a file, service stopped automatically otherwise filecreated method is working properly. Here is my code,
protected override void OnStart(string[] args)
{
string dirname = #"C:\Users\Admin\Documents\Visual Studio 2017\WebSites\Project1\client";
watch = new System.IO.FileSystemWatcher(dirname, "*.*");
watch.Created += new FileSystemEventHandler(this.FileCreated);
watch.Changed += new FileSystemEventHandler(this.FileChanged);
watch.Deleted += new FileSystemEventHandler(this.FileDeleted);
watch.Renamed += new RenamedEventHandler(this.FileRenamed);
watch.IncludeSubdirectories = true;
watch.EnableRaisingEvents = true;}
and filechanged method is this -
public void FileChanged(object sender, FileSystemEventArgs e1)
{
MailMessage message = new MailMessage("***#gmail.com", "***#gmail.com");
Attachment attachfile = new Attachment(e1.FullPath);
message.Subject = "Data Changed " + e1.Name.ToString();
message.IsBodyHtml = true;
message.Body = "The Following File is Changed " + Path.GetFileName(e1.FullPath.ToString()) + " in " + " at " + DateTime.Now.ToLongTimeString() + " by " + System.Environment.UserName.ToString();
message.Attachments.Add(attachfile);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "email#gmail.com";
credentials.Password = "password";
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(message);
foreach (Attachment attachment in message.Attachments)
{
attachment.Dispose();
}
}
So, Please help me out, how to keep running web service even when I open a file and change its data.

Related

i cant remove a path in my sending email form [winform]

my email format looks like this or my design:
my cross is not working even if i press the cross and send an email it still sends an attached file
still i tried everything Eg i used if else conditions and if you can help me fix this it would mean a lot and thanks in advance for looking at my question
String path = "";
public Form3()
{
InitializeComponent();
}
private void label9_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//to add attached file
path = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
label10.Text = path;
pictureBox1.ImageLocation = path;
}
}
private void label12_Click(object sender, EventArgs e)
{
//cross to remove the attached file
String path = "";
label10.Text = "'Attached File Location'";
pictureBox1.ImageLocation = path;
}
private void label11_Click(object sender, EventArgs e)
{
try
{
if (path != "")
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(textBox2.Text);
message.To.Add(new MailAddress(textBox3.Text));
message.Subject = textBox1.Text;
message.Body = richTextBox1.Text;
//attached path sendds here
message.Attachments.Add(new Attachment(path));
smtp.Port = 587;
smtp.Host = "smtp." + textBox5.Text.ToLower() + ".com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential(textBox2.Text, textBox4.Text);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
MessageBox.Show("Success, Email has been successfully sent to "+textBox3.Text);
}
else if(path == "")
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(textBox2.Text);
message.To.Add(new MailAddress(textBox3.Text));
message.Subject = textBox1.Text;
message.Body = richTextBox1.Text;
smtp.Port = 587;
smtp.Host = "smtp." + textBox5.Text.ToLower() + ".com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential(textBox2.Text, textBox4.Text);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
MessageBox.Show("Success, Email has been successfully sent to " +textBox3.Text);
}
You are using a local path variable
private void label12_Click(object sender, EventArgs e)
{
//cross to remove the attached file
String path = "";
use global path instead
private void label12_Click(object sender, EventArgs e)
{
//cross to remove the attached file
path = "";

add multiple selected dates of calendar in mail body wpf c#

In MyCalendar_SelectedDatesChanged() event i am showing all multiple selected dates in list box. I need to pick all these dates and add in email body line by line.
sending mail code :
login = new NetworkCredential("wapsatest#gmail.com", "wapsatest123456");
client = new SmtpClient("smtp.gmail.com");
client.Port = Convert.ToInt32(587);
client.EnableSsl = true;
client.Credentials = login;
msg = new MailMessage { From = new MailAddress("wapsatest" + "smtp.gmail.com".Replace("smtp.", "#"), "nWorks Employee", Encoding.UTF8) };
msg.To.Add(new MailAddress("saurabh.pawar#nworks.co"));
msg.Subject = "Requested for leave by "+comboboxEmployee.Text;
msg.Body = "///////////////List of dates coming from list box name selecteddates";
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userstate = "sending.......";
client.SendAsync(msg, userstate);
how can i add required body using html format in c# wpf...?
Try below code that may help you.
login = new NetworkCredential("wapsatest#gmail.com", "wapsatest123456");
client = new SmtpClient("smtp.gmail.com");
client.Port = Convert.ToInt32(587);
client.EnableSsl = true;
client.Credentials = login;
msg = new MailMessage { From = new MailAddress("wapsatest" + "smtp.gmail.com".Replace("smtp.", "#"), "nWorks Employee", Encoding.UTF8) };
msg.To.Add(new MailAddress("saurabh.pawar#nworks.co"));
msg.Subject = "Requested for leave by "+comboboxEmployee.Text;
//-------------------Code for your Email body---------------------
string strBody = string.Empty;
int i = 1;
strBody += "///////////////List of dates coming from list box name selecteddates";
strBody += Environment.NewLine;
foreach (var item in lstDate) // here "lstDate" is name of your list where you store all date.
{
//strBody += item.ToShortDateString() + Environment.NewLine;
strBody += "Some text before date".
strBody += i + ". " + item.ToShortDateString() + " (" + item.DayOfWeek + ")";
strBody += "Some text after date".
strBody += "<br/>";
i++;
}
msg.Body = strBody;
//----------------------------- Over -----------------------------
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userstate = "sending.......";
client.SendAsync(msg, userstate);

Unable to send the mailmessage in asp.net

The following code unable to send an emails to customers and it not throwing any exception. The code it not send any email or exception but executed.I am completely new about the asp.net. Some one can help me how to resolve the problem.
Code:
try
{
String userName = "ramesh";
String passWord = "123456";
String sendr = "ramesh#gmail.com";
String recer = "customer#yahoo.com";
String subject = "Comformation ";
String body = "Dear Customer";
MailMessage msgMail = new MailMessage(sendr, recer, subject, body);
int PortNumber = 25;
SmtpClient smtp = new SmtpClient("smtp.test.com", PortNumber);
msgMail.IsBodyHtml = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential(userName, passWord);
smtp.Send(msgMail);
MsgLP.Text = "Emailed to Customer..";
LogInLink.Visible = true;
}
catch (Exception ex){
AuditLog.LogError("ErrorE-mail " + ex.Message);
}
You have to set smtp.EnableSsl=true and use port number 587. Your final code will be this:
try
{
String userName = "ramesh";
String passWord = "123456";
String sendr = "ramesh#gmail.com";
String recer = "customer#yahoo.com";
String subject = "Comformation ";
String body = "Dear Customer";
MailMessage msgMail = new MailMessage(sendr, recer, subject, body);
int PortNumber = 587; //change port number to 587
SmtpClient smtp = new SmtpClient("smtp.gmail.com", PortNumber); //change from test to gmail
smtp.EnableSsl = true; //set EnableSsl to true
msgMail.IsBodyHtml = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential(userName, passWord);
smtp.Send(msgMail);
MsgLP.Text = "Emailed to Customer..";
LogInLink.Visible = true;
}
catch (Exception ex){
AuditLog.LogError("ErrorE-mail " + ex.Message);
}
I tested this code with my credentials and it works fine.
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
mm.From = new MailAddress("email#gmail.com");
mm.To.Add("email#gmail.com");
System.Net.Mail.Attachment attachment;
string strFileName;
strFileName = "Uploadfile/" + "200814062455PM_Admin_Screenshot (10).JPEG";
attachment = new System.Net.Mail.Attachment(Server.MapPath(strFileName));
mm.Attachments.Add(attachment);
mm.Body = ("<html><head><body><table><tr><td>Hi</td></tr></table></body></html><br/>"); ;
mm.IsBodyHtml = true;
mm.Subject = "Candidate " + Name + " for your Requirement " + Jobtt + " ";
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("email#gmail.com", "password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
object userstate = mm;
client.Send(mm);

Using SMTP in Visual Studio 2012

I am having problems with sending an email using SMTP, it says "waiting for local host" then eventually times out.
My assumption is that SMTP is disabled on local host, so I had a look at the directions here: http://msdn.microsoft.com/en-us/library/8b83ac7t%28v=vs.100%29.aspx But none of the steps were applicable to my computer (Windows 8, asp.net 4, VS 2012)
Is this enabled by default and the problem is my code?
Here is my code
protected void SendMail()
{
MailMessage email = new MailMessage();
email.To.Add(new MailAddress("removed"));
email.Subject = "Contact Form";
email.From = new MailAddress(emailTextbox.Text);
email.Body = "From: " + nameTextbox.Text + "<br />";
email.Body = "Message: " + commentTextbox.Text + "<br />";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 465;
smtp.Credentials = new NetworkCredential("removed", "removed");
smtp.EnableSsl = true;
smtp.Send(email);
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SendMail();
messageLabel.Text = "Thanks for your email, we will get back to you shortly.";
}
catch (Exception error) {
messageLabel.Text = "Error " + error;
}
}
Any help is appreciated, thanks!
Please see, if adding following configuration helps:
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

Hyperlinks in mails not showing in gmail, but it does in Outlook

I've got a c# process that send emails to customers with a hyperlink in the mail. The mail is send from a SQL Server stored proc. My c# program just invokes the sp.
The hyperlink works fine in Outlook, but on online gmail it only shows as text. It is not clickable.
My mail text looks something like:
Hi.
This is the hyperlink:<br>
<a href=\"serveraddress\Documents\\123_128635312685687531322.gif\">
Click Here</a><br><br>
What should I do to fix it?
EDIT:
My code:
string email = "xx#gmail.com;
string password = "MyPassword";
var credentials = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email, senderName);
msg.To.Add(new MailAddress(toAddress));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.Send(msg);
EDIT 2:
Compile message being send:
string message = #"Hi. <br>
This is the intro line in the mail message.<br>";
using (DataTable dtLinks = data.ExecuteDataSet(#"SELECT *
FROM LessonFiles
WHERE Course = " + dr["Course"].ToString().DBValue() + #" AND
Lesson = " + dr["NextLesson"].ToString().DBValue()).Tables[0])
{
int i = 0;
foreach (DataRow drLink in dtLinks.Rows)
{
i += 1;
message += "<a href=\"" + drLink["Link"].ToString() + "\">" + drLink["Lesson"].ToString();
message += i == 1 ? "" : " file " + i;
message += "</a>" + "<br>";
}
}
message += "<br>Regards<br><br>";
try to add target="_blank", like this...
message += "<a href=\"" + drLink["Link"].ToString() + "\"target=\"_blank\">" + drLink["Lesson"].ToString();
Seems like something was funny with the hyperlink itself.
Using http://serveraddress/Documents/logoColourBG635315550177822533.jpg seems to work.
The original contained backslashes in the path. The fact that it showed the hyperlink in Outlook led me to believe the address was correct.
Thanks for all your help.
creating mail message object...
var smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(UserName, Password);
smtp.Timeout = 20000;
MailMessage Msg = new MailMessage();
Msg.IsBodyHtml = true;
MailAddress fromMail = new MailAddress(SenderID);
Msg.From = fromMail;
Msg.To.Add(new MailAddress(TosendID));
Msg.Subject = subject;
Msg.Body = body;
In body add ur code.....
Hope This Helps.........

Categories

Resources