Navneet Sharma's Article for mailing from windows application
Introduction
This article can be used to send emails from windows forms while developing from C# applications in .Net 3.0 environment and I have used the assemblies and method to be added to the code for the smooth functionality.This code has been inherited and implemented by me after a survey on the web and contains the items useful for email over web as well as windows applications.
Background
This article can be used to send emails from windows forms while developing from C# applications in .Net 3.0 environment
Using the code
Copy this code to send emails from windows forms while developing from C# application in .Net 3.0 environment and I have used the assemblies and method to be added to the code for the smooth functionality. Also add the server name in the "SMTPServer" which is used to send emails which can be your localhost also which can be denoted by "." and the method named sendMail() can be called anywhere in your code(where you want) for e.g. Button1_Click event.
Copy the below code and paste it into your application.
using System.Net;
using System.Net.Mail;
private void sendMail()
{
string strMailTo, strMailFrom, strCC,strBCC, strSubject, strBody;
try
{
strMailTo = <a href="mailto:np.mca1982@gmail.com;strMailFrom">np.mca1982@gmail.com;</a>
strMailFrom = <a href="mailto:yourmailid@abc.com">yourmailid@abc.com</a>;
strSubject = <a href="mailto:abc@abc.com;strReportName">"Demo Mail"</a>;
strBody = "Mail Body to be sent goes here.";
strCC = <a href="mailto:np.mca1982@gmail.com;strBCC">np.mca1982@gmail.com</a>;
strBCC = <a href="mailto:np.mca1982@gmail.com;MailMessage">np.mca1982@gmail.com</a>;
MailMessage mm = new MailMessage();
mm.To.Add(strMailTo);
mm.From = new MailAddress(strMailFrom);
mm.CC.Add(strCC);
mm.Bcc.Add(strBCC);
mm.Subject = strSubject;
mm.Body = strBody;
mm.Priority = MailPriority.Normal;
mm.BodyEncoding = Encoding.UTF8;
mm.IsBodyHtml = false;
SmtpClient smtp=new SmtpClient("SMTPServer");
smtp.Send(mm);
}
catch (Exception ex)
{
}
}
Language Used : C#
Platforms : Win XP Professional with SP 2, .Net 3.0
Points of Interest
Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?
History
This code has been implemented here for the first time in this post and if you want some modifications in it then mail me or suggest me the required improvements and I will do the required changes.
Please vote this article if this was useful to you.