How to send email in Asp.Net/C#.Net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rayapatilavakumar
    New Member
    • Jan 2007
    • 6

    How to send email in Asp.Net/C#.Net

    Hi all, i need code for sending a mail to different mail ids like a yahoo id ,gmail id etc.whenever i click a button it needs to send a mail to that particular id specified in To: field

    My page is like
    To: txtto

    Subject:txtsubj ect

    Body





    send-button
  • enreil
    New Member
    • Jan 2007
    • 86

    #2
    Try exploring System.Web.Mail . That makes it pretty simple to generate emails on the fly.

    Originally posted by rayapatilavakum ar
    Hi all, i need code for sending a mail to different mail ids like a yahoo id ,gmail id etc.whenever i click a button it needs to send a mail to that particular id specified in To: field

    My page is like
    To: txtto

    Subject:txtsubj ect

    Body





    send-button

    Comment

    • rayapatilavakumar
      New Member
      • Jan 2007
      • 6

      #3
      can u give me code ?

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by rayapatilavakum ar
        can u give me code ?
        No, but this can...

        Comment

        • tbarto
          New Member
          • Feb 2007
          • 19

          #5
          Originally posted by rayapatilavakum ar
          can u give me code ?
          Hi, You can try to do that in the following way:
          Code:
              public class Emailer
              {
                  #region Variables
          
                  private string _smtpServer;
                  private string _userName;
                  private string _password;
                  private ArrayList _recipients;
          
                  #endregion
          
                  #region Constructors
          
                  public Emailer()
                  {
          
                  }
          
                  #endregion
          
                  #region Properties
          
                  public string SmtpServer
                  {
                      set
                      {
                          _smtpServer = value;
                      }
                  }
          
                  public string UserName
                  {
                      set
                      {
                          _userName = value;
                      }
                  }
          
                  public string Password
                  {
                      set
                      {
                          _password = value;
                      }
                  }
          
                  public ArrayList Receipients
                  {
                      set
                      {
                          _recipients = value;
                      }
                  }
          
                  #endregion
          
                  #region Methods
          
                  public void SendMessage(string subject, string content)
                  {
                      string domain = "";
          
                      if (_smtpServer.Substring(0, 5).ToLower().Equals("smtp."))
                          domain = _smtpServer.Substring(5, _smtpServer.Length - 5);
                      else
                          domain = _smtpServer;
          
                      string recipient = _userName.ToLower();
                      MailMessage _mail = new MailMessage();
          
          
                      try
                      {
          
                          System.Net.Mail.MailAddress _from = new System.Net.Mail.MailAddress(recipient + "@" + domain, recipient);
                          System.Net.Mail.MailAddressCollection addresses = new System.Net.Mail.MailAddressCollection();
          
                          _mail.From = _from;
                          _mail.Subject = subject;
                          _mail.Body = content;
                          _mail.Priority = System.Net.Mail.MailPriority.High;
          
          
                      }
                      catch (Exception)
                      {
                         Do something...
                      }
          
                      try
                      {
          
                          foreach (string adress in _recipients)
                          {
                              MailAddress addr = new MailAddress(adress);
          
                              if (!_mail.To.Contains(addr))
                                  _mail.To.Add(addr);
                          }
                      }
                      catch (Exception)
                      {
                         Do something
                      }
          
          
          
                      try
                      {
                          System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(_smtpServer);
                          smtp.Credentials = new System.Net.NetworkCredential(_userName, _password);
          
                          smtp.Send(_mail);
                      }
                      catch (Exception)
                      {
                          do something
                      }
          
                      _mail.Dispose();
          
                  }
          
                  #endregion
          
              }

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            First, you need to put CODE tags around your code, it is very annoying to read your post with out them. Second, it is better to let others work through their problems with guidance instead of giving them the entire solution. Your solution will likely not meet their needs, and further more they should be familiar with how to send an email using the asp.net classes. I wish I could edit your post so that it was easier to read, but I can't. If you still can, you should!

            Comment

            • tbarto
              New Member
              • Feb 2007
              • 19

              #7
              Originally posted by RedSon
              First, you need to put CODE tags around your code, it is very annoying to read your post with out them. Second, it is better to let others work through their problems with guidance instead of giving them the entire solution. Your solution will likely not meet their needs, and further more they should be familiar with how to send an email using the asp.net classes. I wish I could edit your post so that it was easier to read, but I can't. If you still can, you should!
              Hi

              Since I am a new at this forum, please forgive me all the mistakes. Thank You for Your advices. I will follow them in the future.
              Sorry, I am not able to edit my previous post anymore.

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                No problem, just remember the CODE tags, it is a real pet peeve of mine!

                Comment

                • GNoter
                  New Member
                  • Feb 2007
                  • 9

                  #9
                  Code:
                  Protected Sub TestCode(ByVal sTemp as string)
                    If sTemp.Length = 0 then 'no data in variable.
                     
                    End if
                  End Sub
                  (just testing the CODE tags; above code is merely test code.

                  Comment

                  • AricC
                    Recognized Expert Top Contributor
                    • Oct 2006
                    • 1885

                    #10
                    Originally posted by RedSon
                    First, you need to put CODE tags around your code, it is very annoying to read your post with out them.
                    I edited and added code tags.

                    Comment

                    • enreil
                      New Member
                      • Jan 2007
                      • 86

                      #11
                      Well said, RedSon, I agree.

                      Originally posted by RedSon
                      First, you need to put CODE tags around your code, it is very annoying to read your post with out them. Second, it is better to let others work through their problems with guidance instead of giving them the entire solution. Your solution will likely not meet their needs, and further more they should be familiar with how to send an email using the asp.net classes. I wish I could edit your post so that it was easier to read, but I can't. If you still can, you should!

                      Comment

                      Working...