Popular Posts

Feb 23, 2011

Sending Mail (C#)


Take a windows form. Take a textbox from toolbox and change the name of the textbox. i change it to emailToTextBox. Another textbox for subject and chance to subjectFromTextBox.Another RichTextBox for body of the mail. bodyRichTextBox.Now take a button and write the code behind the button
[ smtp.Host = ""; //Like "smtp.gmail.com" ]


        private void sendMessageButton_Click(object sender, EventArgs e)
        {
            string to = emailToTextBox.Text;
            string subject = subjectFromTextBox.Text;
            string mailBody = bodyrRichTextBox.Text;

            if (to == "" && subject == "" && mailBody == "")
            {

            }
            else
            {
                MailMessage msg = new MailMessage();
                msg.To.Add(to);
                msg.From = new MailAddress(to);
                msg.Subject = subject;
                msg.Body = mailBody;

                msg.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "";  
                smtp.Credentials = new System.Net.NetworkCredential("x@y.com", "passxyz");
                smtp.EnableSsl = true;
                try
                {
                    smtp.Send(msg);
                    MessageBox.Show("Message Has Been Sent.");
                }
                catch
                {
                    MessageBox.Show("Message can't send...");
                }
            }

No comments:

Post a Comment