WebHosting

Thursday, March 15, 2012

C# code for sending mail automatically through application code.

You can generally find this kind of programming requirements in automated mail response system like websites for online booking for booking confirmation mail. This is very basic piece of code. For enterprise level requirement you can use MS SQL server 2008 which has functionality for the same available.


These namespaces are to added to the code.
using System.Net.Mail;
using System.Net;
The following lines of code shows how the mail message is generated and finally send using SMTP.
MailMessage mM = new MailMessage();
//Mail Address
mM.From = new MailAddress("sender@hotmail.com ");
//receiver email id
mM.To.Add("receiver@hotmail.com ");
//subject of the email
mM.Subject = “subject string”;
//attachment
mM.Attachments.Add(new Attachment(@"local path"));
//body of the email
mM.Body = “email body”;
mM.IsBodyHtml = true;
//SMTP client
SmtpClient sC = new SmtpClient("smtp.live.com");
//port number for Hot mail
sC.Port = 25;
//credentials to login in to hotmail account
sC.Credentials = new NetworkCredential("sender@hotmail.com", "password");
//enabled SSL
sC.EnableSsl = true;
//Send an email
sC.Send(mM);

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

เค•िเคธเค•े เคนिเคธ्เคธे เคฎें เคฎें เคฏे เคงเคฐเคคी

เคšिเคก़िเคฏों เค•ा เค˜เคฐ เคœเคฒ เคฐเคนा เคนै  เค‡ंเคธाเคจ เค–ुเคถ เคนै เค•ि เคฏे เค†เค— เคฆूเคฐ เคนै เค…เคญी เค•เคญी เคคो เค†เคเค—ी เค˜เคฐ เคคเค•  เคคेเคฐा เคญी เค˜เคฐ เคœเคฒेเค—ा เค•เคญी เคฌेเคœुเคฌाเคจ เคนै เค•ुเค› เคฌोเคฒเคคे เคจเคนीं เคนै เคฆिเคฒ เคฎें เคฆुเค– ...