RSS

how to send email in asp.net with C#

26 Oct
  1. System;
  2. using System.Configuration;
  3. using System.Web;
  4. using System.Web.Security;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Web.UI.HtmlControls;
  8. using System.Net.Mail;
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. #region "Send email"
  12. protected void btnSendmail_Click(object sender, EventArgs e)
  13. {
  14. // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
  15. // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
  16. SmtpClient smtpClient = new SmtpClient();
  17. MailMessage message = new MailMessage();
  18. try
  19. {
  20. MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
  21. // You can specify the host name or ipaddress of your server
  22. // Default in IIS will be localhost
  23. smtpClient.Host = "localhost";
  24. //Default port will be 25
  25. smtpClient.Port = 25;
  26. //From address will be given as a MailAddress Object
  27. message.From = fromAddress;
  28. // To address collection of MailAddress
  29. message.To.Add("john_begginer@yahoo.co.in");
  30. message.Subject = "Feedback";
  31. // CC and BCC optional
  32. // MailAddressCollection class is used to send the email to various users
  33. // You can specify Address as new MailAddress("admin1@yoursite.com")
  34. // message.CC.Add("");
  35. // message.CC.Add("");
  36. // You can specify Address directly as string
  37. //message.Bcc.Add(new MailAddress(""));
  38. //message.Bcc.Add(new MailAddress(""));
  39. //Body can be Html or text format
  40. //Specify true if it is html message
  41. message.IsBodyHtml = false;
  42. // Message body content
  43. message.Body = txtMessage.Text;
  44. // Send SMTP mail
  45. smtpClient.Send(message);
  46. lblStatus.Text = "Email successfully sent.";
  47. }
  48. catch (Exception ex)
  49. {
  50. lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
  51. }
  52. }
  53. #endregion
  54. #region "Reset"
  55. protected void btnReset_Click(object sender, EventArgs e)
  56. {
  57. txtName.Text = "";
  58. txtMessage.Text = "";
  59. txtEmail.Text = "";
  60. }
  61. #endregion
  62. }
You need to set Relay IP-Address. (Relay restrictions). Steps: 1. Open IIS applet from the control panel 2. Go to0 Default SMTP Virtual Server 3. Select tab Access + Click on Relay. 4. Add IP of localhost 127.0.0.1 you need to check mail in C:\Inetpub\mailroot\Queue ........... the configuraion for this plz see this link http://www.emailarchitect.net/webapp/smtpcom/developers/smtpservice.asp
Advertisements
 
Leave a comment

Posted by on October 26, 2010 in ASP Dot Net C#

 

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: