• RSS
  • Twitter
  • FaceBook

Exchange Server Forums

Forums | Register | Login | My Profile | Inbox | RSS RSS icon | My Subscription | My Forums | Address Book | Member List | Search | FAQ | Ticket List | Log Out

Using C# to send an email over exchange server

Users viewing this topic: none

Logged in as: Guest
  Printable Version
All Forums >> [Microsoft Exchange 2003] >> Server Security >> Using C# to send an email over exchange server Page: [1]
Login
Message << Older Topic   Newer Topic >>
Using C# to send an email over exchange server - 19.Feb.2009 12:36:50 PM   
Luxx

 

Posts: 3
Joined: 19.Feb.2009
Status: offline
If anyone could shed some light on this subject it would really help me out.  I am able to send an email over my company's SMTP server.. but I want to set it up so I can connect to our exchange server and send an email as me. 

I would eventually like to get it so I can connect to the exchange server.. and not even have to put an email address in the FROM line.. because I am connected under my credentials and it knows who I am.

When I use:

client.Credentials = new System.Net.NetworkCredential(userBox.Text, passBox.Text);

It doesn't matter what username or password I put in.. the email will still send.  (I am sending it to myself) HOW do I get this to connect to the exchange server and verify my credentials?  Any help would be greatly appreciated.
Post #: 1
RE: Using C# to send an email over exchange server - 20.Feb.2009 4:37:46 AM   
anil.chopra

 

Posts: 26
Joined: 20.Feb.2009
Status: offline
Hi Luxx,
 
With the description provided I am not sure if you are writing a utility/sw program to connect to exchange-->authenticate-->and then send an email. If you would share some details then perhaps I can help you with more specific information. Anyways given the details, you may use following code snippet to connect to exchange and authenticate before email is sent. However if you are already authenticated then you may remove the line starting with mymail.credential.
 
*****************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;

public static void sendEmail()
       {

     string fromEmail = "";//This field will contain the FROM email
    string ToEmail = "";//This field will contain destination email            
           string body = ""; //This field will contain the body of the email
           string subject = ""; //This field will contain the subject


            try
           {
               SmtpClient MyMail = new SmtpClient("");//exchange or smtp server goes here.
               MyMail.DeliveryMethod = SmtpDeliveryMethod.Network;
               MyMail.Credentials = new NetworkCredential("username","password"); //this line most likely wont be needed if you are already an authenticated user.
               MyMail.Send(fromEmail,ToEmail, subject, body);
           }
           catch(Exception ex)
           {
              //do something after error if there is one
           }
}

 
*****************************************************************************

Let me know if this works for you.
 
Regards..Anil

_____________________________

Certified Information Systems Auditor
Certified Information Security Manager
ITIL v3 Foundation
Microsoft Certified Systems Engineer
Post #: 2
RE: Using C# to send an email over exchange server - 24.Feb.2009 11:56:05 AM   
Luxx

 

Posts: 3
Joined: 19.Feb.2009
Status: offline
Hi Neko, Anil and others,
Thanks for responding.  What I am essentially trying to do is write a program using C# that will connect to our (my company's) exchange server and send and manage email.  I want to make it so anyone can log on and send an email.  For example I would like to set up the form like:

User Name: username(textbox)
Password: password(textbox)
        Login(button)

Once logged in, have the email form similar to the code you provided but I want to pass in the username and email from the text boxes to the MyMail.Credentials.

I've written this layout for my form already.  The problem that I am having is I can send an email BUT It doesn't matter what username or password I put in (i could put in nothing at all).. it will STILL send the email.  Is this happening because I am logged in my work computer on the exchange server?   Here is my code:

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

namespace EmailTest
{
  public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {

          try
          {

              SmtpClient client = new SmtpClient(sendServer.Text);

              MailMessage message = new MailMessage(sendFrom.Text, sendTo.Text);

              message.Body = contentBox.Text;

              message.Subject = subjectBox.Text;

              client.UseDefaultCredentials = false;
              client.DeliveryMethod = SmtpDeliveryMethod.Network;
              client.Credentials = new System.Net.NetworkCredential(userBox.Text, passBox.Text);

              client.Send(message);
          }

          catch (Exception ex)
          {
              MessageBox.Show("Cannot send message: " + ex.Message);
          }

      }

      private void button2_Click(object sender, EventArgs e)
      {
          sendServer.Clear();
          serverPort.Clear();
          sendTo.Clear();
          sendFrom.Clear();
          userBox.Clear();
          passBox.Clear();
          subjectBox.Clear();
          contentBox.Clear();

      }
  }
}


I also, in this example, am required to supply a FROM line.. I would like it to be: after a user logs in, they don't have to supply a FROM line - because they are logged in.  Is any of this possible?  Am I missing something?


< Message edited by Luxx -- 24.Feb.2009 12:30:42 PM >

(in reply to anil.chopra)
Post #: 3
RE: Using C# to send an email over exchange server - 24.Feb.2009 3:01:39 PM   
Luxx

 

Posts: 3
Joined: 19.Feb.2009
Status: offline
Update:
Okay so I realized that when the emails are sending and I am typing in my CORRECT credentials, the emails are getting sent to my actual inbox (I'm sending from myself, to myself) and my exchange user name is shown in the FROM line.. like: Myfirstname Mylastname

When I use FALSE credentials, for example a wrong password or something, the email gets sent to my Junk Email folder with my email address that I supplied in the FROM line written out like: myemail@myjob.com.

Is there anything I can do to not send out the email if the credentials are incorrect?

(in reply to anil.chopra)
Post #: 4
RE: Using C# to send an email over exchange server - 19.Aug.2012 1:40:11 PM   
chriselviss

 

Posts: 1
Joined: 19.Aug.2012
Status: offline
try use CDO email , check the followinf link it will help u how to send email using CDO.

http://csharp.net-informations.com/communications/csharp-cdo-email.htm

chris.

(in reply to Luxx)
Post #: 5

Page:   [1] << Older Topic    Newer Topic >>
All Forums >> [Microsoft Exchange 2003] >> Server Security >> Using C# to send an email over exchange server Page: [1]
Jump to:

New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Follow TechGenix on Twitter