Code to send email

Hi, below is the code to send the email.

First of all make changes in web config file in "app settings"
i.e.


<add key="SMTPServer" value="mail.test.com"/>
<add key="MailUserName" value="admin@test.com"/>
<add key="MailPassword" value="password"/>

Then use namespace on source code file.

i.e.
using System.Net.Mail;

Now write down the below code on button click (on which you want to send email.)with required perameter. i.e.


SmtpClient objSMTP = new SmtpClient(ConfigurationManager.AppSettings ["SMTPServer"].ToString());

objSMTP.Credentials = new System.Net.NetworkCredential(
ConfigurationManager.AppSettings["MailUserName"].ToString(),

ConfigurationManager.AppSettings["MailPassword"].ToString());

MailMessage objMailMessage = new MailMessage();

objMailMessage.From = new MailAddress(ConfigurationManager.AppSettings["MailUserName"].ToString());

objMailMessage.Subject = "Test mail";

objMailMessage.To.Add(new MailAddress(EmailAddress));

objMailMessage.Body = "Hi, this is a text mail.

objMailMessage.IsBodyHtml = true;

objSMTP.Send(objMailMessage);

lblMsg.Visible = true;

lblMsg.Text = "Mail has been sent.";


Regards
Vivek Sharma


Comments

Author: Mohan03 Sep 2009 Member Level: Gold   Points : 1

HI,

Is Your code is working from any mail id to anyone?
or Anyone id shoule be a abc@test.com

Author: Vivek Sharma03 Sep 2009 Member Level: Gold   Points : 1

Sorry sir, but I am not getting you exactly??
Could you explain please??

Regards
Vivek Sharma

Author: Mohan03 Sep 2009 Member Level: Gold   Points : 1

Means you are using mail.test.com server for sending mail

Is your code allow

from address=abc@gmail.com
to address=def@gmail.com


or it allow only

from address=abc@test.com
to address=def@test.com

Author: Punit25 Sep 2009 Member Level: Silver   Points : 0

Thank you vivek, this code was helpful for me.

Regards,
Punit

Author: Vivek Sharma25 Sep 2009 Member Level: Gold   Points : 0

Hi Punit, Thanks you very much.

Regards
Vivek Sharma

Author: Vivek Sharma25 Sep 2009 Member Level: Gold   Points : 1

Hi Mohan, You can pass any (required parameter in it) I mean in
Tomail and From mail .

Regards
vivek Sharma



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: