add multiple selected dates of calendar in mail body wpf c# - 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);

Related

filesystemwatcher event is not triggering when excel file data are changed

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.

Send mail from Exchange Server

I want to send mail from exchange server without password. I use the following code snippet :
MailMessage msg = new MailMessage();
msg.From = new MailAddress("kavitham#test.com");
msg.To.Add(new MailAddress("kavitham#sample.com"));
msg.Subject = "Reg : Mail test";
msg.IsBodyHtml = true;
msg.Body = "<html><body>" + strMsg + "</body></html>";
SmtpClient client = new SmtpClient("IP of Server", 25);
client.Host = "IP of Server";
client.Send(msg);
But the mail is not sent. Is there any other settings for Exchange server need to be done ?
add this
smtpClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
MailMessage msg = new MailMessage("noreply#test.com", "manager#test.com", "Notification For Lead / Admin ",
"Emp Name :" + EmpName);
msg.Body = "Emp Name : " + EmpName + "<br /><br >" + Environment.NewLine + timsheetMail;
msg.IsBodyHtml = true;
msg.CC.Add(toMail); msg.CC.Add(admin);
SmtpClient SMTPServer = new SmtpClient("mail.myserver.com", 25);
SMTPServer.Timeout = 300000;
SMTPServer.EnableSsl = false;
SMTPServer.Credentials = new System.Net.NetworkCredential("noreply#test.com", "passw0rd");
SMTPServer.Send(mailObj);

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

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.........

How to format text in email when using smtp

I'm using the following method to send an email. I want to be able to format the email with bold text.
Ex.
From: name
Email: email address
Message: message
How would I do this?
protected void SendMail()
{
var fromAddress = "myemail#gmail.com";
var toAddress = "myotheremail#gmail.com";
const string fromPassword = "mypassword";
string subject = "New Email from Website";
string body = "From: " + name.Text + "\n";
body += "Email: " + email.Text + "\n";
body += "Message: \n" + message.Text + "\n";
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(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);
}
Set isBodyHtml to true, the following code describes it,
To send a bold text you can use "<b> My bold text </b> ".
To send an italicized text you can use "<i> Italic text </i> ".
To send an underlined text you can use "<u> underlined text </u>".
You can copy and use the following method. By using this method, it will be very easy to send emails.
public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
{
try
{
MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 25,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("your email address", "your password")
};
using (MailMessage message = new MailMessage(FromAddr, ToAddr)
{
Subject = Subject,
Body = Body,
IsBodyHtml = IsBodyHTML,
BodyEncoding = System.Text.Encoding.UTF8,
})
{
smtp.Send(message);
}
return true;
}
catch
{
return false;
}
}
When you call this method call it like this
SendEmail("Here address to" , "Here to name" , "Here from", "here from name", "here subject" , here Body, " Here whether HTML or Plain" )
You need only few minor changes.
say IsBodyHtml is true
replace all \n with <br/>
and here's the final the code
protected void SendMail()
{
var fromAddress = "myemail#gmail.com";
var toAddress = "myotheremail#gmail.com";
const string fromPassword = "mypassword";
string subject = "New Email from Website";
string body = "From: " + name.Text + "<br/>";
body += "Email: " + email.Text + "<br/>";
body += "Message: <br/>" + message.Text + "<br/>";
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(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body,IsBodyHtml:true);
}
Hope that helps.

Categories

Resources