C# Code to send a Mail
Dec02

C# Code to send a Mail

Welcome to CodeSpread! While working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in forum asking for code to send email in .Net. I was not able to answer his query properly as it was something that i wrote long back and because of copy pasting from old code, i had completely forgot how to do that. So Here i attempt to recollect my memories, I know its very simple and if some of you can help me add more value to this article,you are most welcome. First thing first, we require a reference to the mail namespace in our cs file before we can start coding. we will include. using System.Net.Mail; This namespace will provide the necessary classes required to compose and send a mail. To compose a Mail, we have to create an instance of MaiMessage like var mailMessage = new MailMessage(); There are members of this instance which are required to be assigned like ‘From’ Address, ‘To’ Address, subject, message body, any attachments. We have another class MailAddress which is used to create instance for ‘From’ Address and ‘To’ Address . I think if you see the code, then it would be much more clear. mailMessage.From = new MailAddress("from"); mailMessage.To.Add(new MailAddress("to")); mailMessage.Subject = "subject"; mailMessage.Body = "body"; if (!string.IsNullOrEmpty("fileName")) mailMessage.Attachments.Add(new Attachment("fileName")); Text written under quotes(“”) needs to be changes with your real values. Don’t miss that. Now we have composed our message, only thing that remains now is to send it. Lets look, if we have some more class in our namespace to perform the operation. We certainly have a class SmtpClient to server our purpose. var mailSmtpClient = new SmtpClient("SmtpHost", SmtpPort); mailSmtpClient.Send(mailMessage); We have created one instance of SmtpClient and passed the values of Smtphost which is the host-name of your mail server and the SmtpPort which has been configured to send a mail. Finally you just have to call their Send() method and pass your mail message. We have many overloads of all these methods available to facilitate the process but here, we have mentioned only the basic process. So, last step is to write all this code at one place. var mailMessage = new MailMessage(); try { mailMessage.From = new MailAddress("from"); mailMessage.To.Add(new MailAddress("to")); mailMessage.Subject = "subject"; mailMessage.Body = "body"; if (!string.IsNullOrEmpty("fileName")) mailMessage.Attachments.Add(new Attachment("fileName")); var mailSmtpClient = new SmtpClient("SmtpHost", SmtpPort); mailSmtpClient.Send(mailMessage); return true; } catch (Exception ex) { return false; } That’s all from my side. Please provide your...

Read More

know your URL

Guys trust me, I am not kidding!! I have had my experience, telling people about URL, parts of URL, query string, SEO friendly URL, canonical URL. Once again, a simple and innocent question “What is URL?” Internet is a world in itself and each resource can be located by an address which is called as URL:uniform resource locator. Though it is self explanatory but you can read more here. How this works actually? We will take an example and try to explain it in our own usual way. “http://codespread.com/2012/10/28/know-your-url/” http:// – Hyper Text Transfer Protocol A protocol is a standardized means of communication among machines across a network. Equivalent form exists in our world,just look at our daily communication through mails,posts,couriers. Other common protocols can be https,ftp. https is required for a secure communication like transferring money in a guarded truck, ftp is required for file transfer like movers and packers . Lets consider,we post a mail. codespread.com – Domain Name We require an address where this mail should reach so at the front of the envelope we write a unique address. Similarly we have a unique domain name to identify each website. /2012/10/28/know-your-url/ – directories/sub-directory/file-name Though our mail will reach the correct address but still it should reach the concerned person.It should move across the structure to reach the exact recipient  Similarly, our URL should open the exact webpage which might be located inside a directory or sub-directory. Lets see,what is this query string? http://codespread.com/2012/10/29/know-your-url?sort=true The ? operator separates the right side character string in the URL which is called as query string. They appear in a key-value pair like here key is ‘sort’ and its value is ‘true’. Query string is useful, in case, when on reaching a webpage, we require some additional activity to happen. In our case we require sorting. Not all the characters can be used in query string.To show that, I want to include some useful information related to URL encoding from Wikipedia here. In particular, encoding the query string uses the following rules: Letters (A-Z and a-z), numbers (0-9) and the characters ‘.’,’-‘,’~’ and ‘_’ are left as-is SPACE is encoded as ‘+’ or %20 All other characters are encoded as %FF hexadecimal representation with any non-ASCII characters first encoded as UTF-8 (or other specified encoding)  SEO friendly URL SEO is Search Engine Optimization. We all know Google is the search engine that most of the world uses and to make indexing of your website better , SEO friendly URL is one of the options .Few basic rules that needs to be followed to make URL’s search friendly. Keep URLs short Use hyphen to separate words Use lower case letters Avoid parameters/query strings Language based links Canonical URLs Canonical URLs is...

Read More