Send Email From C#

Embed Size (px)

Citation preview

  • 8/12/2019 Send Email From C#

    1/5

    MailHelper.csusing System.Net.Mail;public class MailHelper{ /// /// Sends an mail message /// /// Sender address /// ecepient address /// !cc recepient /// c recepient /// Sub#ect of mail message /// !ody of mail message publicstatic $oid SendMailMessage% string from& string to& string bcc& string cc& string sub#ect& string body'

    { // (nstantiate a ne) instance of MailMessage MailMessage mMailMessage = ne) MailMessage %'; // Set t*e sender address of t*e mail message mMailMessage.+rom = ne) Mail,ddress %from'; // Set t*e recepient address of t*e mail message mMailMessage.-o.,dd% ne) Mail,ddress %to''; // *ec if t*e bcc $alue is null or an empty string if %%bcc = null ' 00 %bcc = string .1mpty'' { // Set t*e !cc address of t*e mail message mMailMessage.!cc.,dd% ne) Mail,ddress %bcc''; 2 // *ec if t*e cc $alue is null or an empty $alue if %%cc = null ' 00 %cc = string .1mpty'' { // Set t*e address of t*e mail message mMailMessage. .,dd% ne) Mail,ddress %cc''; 2 // Set t*e sub#ect of t*e mail message

    mMailMessage.Sub#ect = sub#ect; // Set t*e body of t*e mail message mMailMessage.!ody = body; // Set t*e format of t*e mail message body as H-M3 mMailMessage.(s!odyHtml = true ; // Set t*e priority of t*e mail message to normal mMailMessage.4riority = Mail4riority .Normal; // (nstantiate a ne) instance of Smtp lient Smtp lient mSmtp lient = ne) Smtp lient %'; // Send t*e mail message mSmtp lient.Send%mMailMessage'; 22

    //Write in Web.config< configuration > < system.net > < mailSettings > < smtp from =" default1mail9yourdomain.com " > < net)or *ost = " smtp.yourdomain.com " port = " : " userName = " your serName " pass)ord = " your4ass)ord " />

    Send via gmailprivate void send_form(){try{System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(FROM email id ere!" #O email id ere"$o%r S%&'e t ere!"!$o%r #*+# ere!),

    -- roper /%t enti ation 0etails need to &e passed w en sending email from gmail System.Net.Networ12redentialmail/%t enti ation = new System.Net.Networ12redential(yo%r email-login id ere!" password ere!),

    --Smtp Mail server of 3mail is smpt.gmail. om! and its port no. 456 System.Net.Mail.Smtp2lient mail2lient = newSystem.Net.Mail.Smtp2lient(smtp.gmail. om!" 456),

    --*na&le SS7--3mail is SS7 ertify so t at w y we need to set *na&elssl=tr%e

    mail2lient.*na&leSsl = tr%e,mail2lient.8se0efa%lt2redentials = false,mail2lient.2redentials = mail/%t enti ation,mail.9s:ody tml = tr%e,

    -- 9t will send t e emailmail2lient.Send(mail),Response.;rite(*mail Sent!),