Sending Email From Your Application





How to send Email

Simple Mail Transfer Protocol (SMTP) help us to send email using our application.

The namespace that needs to be imported to send an email :


using System.Net.Mail;

Credentials for authentication are required by the SMTP server. ie,
Username and Password.We need to provide this.

MailMessage classes of using System.Net.Mail for tis purpose. The MailMessage class provides properties and methods for constructing an email message.


Code Snippet

 try
        {
         String FileName = Server.MapPath("./files/Test.txt");
            System.Net.Mail.MailMessage EmailMsg = new MailMessage();
            EmailMsg.From = new MailAddress("FromAddress@domain.com","YourName");
            EmailMsg.To.Add(new MailAddress("ToAddress@domain.com", "SendersName"));
            EmailMsg.Subject = "Test email form";
            EmailMsg.Body = "TestMail<hr>";
            EmailMsg.IsBodyHtml = true;
            EmailMsg.Priority = MailPriority.Normal;

            SmtpClient MailClient = new SmtpClient("ServerName");
            MailClient.Credentials = new System.Net.NetworkCredential("UserName,"Password");
         EmailMsg.Attachments.Add(new System.Net.Mail.Attachment(FileName));
            MailClient.Send(EmailMsg);        }

        catch {
            Response.Write("Failure");

        }


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: