Send Email by C#

pauledwardian
pauledwardian used Ask the Experts™
on
I need a code for Winform application that would send email to the user within the company exchange email address. The users in company are setup with exchange in outlook. Please help if it is possible for you.

Thank you,
PE
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hello, This is the example of mail sending in winform applications.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net.Mail;

namespace YourProjectName
{
    public partial class feedback : Form
    {
        public feedback()
        {
            InitializeComponent();
        }

        private void feedback_Load(object sender, EventArgs e)
        {
           
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string sTo = "abc@abc.com";
            string sFrom = txtEmail.Text;
            string sSubject = txtName.Text;
            string sBody = txtHow.Text;

            try
            {
                MailMessage mail = new MailMessage(sFrom, sTo, sSubject, sBody);
                SmtpClient client = new SmtpClient("192.168.1.1"); //Your smtpclient
                client.Send(mail);
                MessageBox.Show("Mail Successfully Sent");
                txtName.Text = "";
                txtEmail.Text = "";
                txtContactNo.Text = "";
                txtHow.Text = "";
            }
            catch (Exception)
            {
                MessageBox.Show("Mail Sending Fail");
            }
        }
    }
}

You also require to add this into app.config file of your project. The host name will be in your outlook account.

<system.net>
            <mailSettings>
                  <smtp>
                        <network host="mx.abc.com" port="25" userName="abc@abc.com" password=""/>
                  </smtp>
            </mailSettings>
            
      </system.net>

Author

Commented:
Can the username and password gets generated either by taking the username and password from the windows login (that user uses to login to his computer) or by prompting for a username and password for the user to input his credentials.
Basically I need to have my application install on each Users computer and then have the program to automatically send that particular user an email address like a reminder.
Respected Sir,

Can you give me some time for this solution, I will give you a answer as early as possible. Thank you for reply me.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

nishant joshiTechnology Development Consultant
Commented:
good code by @samir


add only one line,

client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

Author

Commented:
Sure, take your time. :)
Thanks  nishantcomp2512 for your suggestion by the way.

Author

Commented:
Hello Samir,

Thanks for helping me on this question. I was just checking to see if you could find the solution.

PE
Nitesh LuharukaDirector

Commented:
@paul, yes you can always take the username and password from winform application instead of app.config file.

Author

Commented:
Thanks guys for the help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial