Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have some c# code that creats a standard email and sends it through an exchange server.

I have 6 textboxes in this mailform: from,to,subject,exchangeserver,user and password.

If the user textbox is preset with the user info (found from mssql server) it won't send the mail.


This won't work:
oServer.User = usertextbox.Text;

This works:
oServer.User = "user1";
Posted
Comments
Zoltán Zörgő 16-Jan-15 15:52pm    
1) What type is oServer?
2) Are you doing this in asp.net web forms and this is an action handler running om server code behind?
2) What means "won't work"? What do you get? Any compile or runtime error?
Member 11380736 16-Jan-15 16:11pm    
1) smtpMail send to an EWS
2) It's in Windows Forms Application.
3) I get this error: The remote server returned an error: (401) Unauthorized.
Zoltán Zörgő 16-Jan-15 16:18pm    
1) SmtpMail is obsolote + I can't imagine EWS service working as SMTP - these two thigs are different
3) And "user1" is passing authorization???? Really?
4) What is the configured authentication model of Exchange?
5) Which version of Exchange do you have?

Please, post some more relavant part of your code.
Member 11380736 16-Jan-15 16:40pm    
My user is not called "user1" but Yes if I manually type in my user account it works.

The exchange server isn't mine so I havn't got that much info about it. Think it's a microsoft exchange 2003 server. It's my employers (public hospital).

Here is what happens when the send button is pressed:

private void button4_Click_1(object sender, EventArgs e)
{

try
{

SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();

// Set sender email address, please change it to yours
oMail.From = from.Text;

// Set recipient email address, please change it to yours
oMail.To = to.Text;

// Set email subject
oMail.Subject = subject.Text;

// Set email body
oMail.TextBody = body.Text;


Attachment oAttachment = oMail.AddAttachment(vedhæft.Text);


// Your Exchange Server address
SmtpServer oServer = new SmtpServer(smtp.Text);

// Set Exchange Web Service EWS - Exchange 2007/2010/2013
oServer.Protocol = ServerProtocol.ExchangeEWS;

// User and password for Exchange authentication

oServer.User = user.Text;
oServer.Password = password.Text;

// By default, Exchange Web Service requires SSL connection
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

oSmtp.SendMail(oServer, oMail);

//Opret forbindelse til sql serveren
globalvariables NewConnection = new globalvariables();
NewConnection.con();

//Opret en sql kommando
SqlCommand command = globalvariables.Con.CreateCommand();

command.CommandText = "UPDATE bestillinger SET bestilt= @bestilt, bestiltbruger = @bruger Where firma = @firma and bestilt is null and udlevering is not null and udlevering BETWEEN @fradato AND @tildato";
command.Parameters.AddWithValue("@firma", firma.Text);
command.Parameters.AddWithValue("@bestilt", DateTime.Now);
command.Parameters.AddWithValue("@bruger", globalvariables.user);
command.Parameters.AddWithValue("@fradato", fradato.Value.Date);
command.Parameters.AddWithValue("@tildato", tildato.Value.Date);

command.ExecuteScalar();

//Luk forbindelsen
globalvariables.Con.Close();

//Kør Form1 sqlcount variable
(System.Windows.Forms.Application.OpenForms["Form1"] as Form1).sqlcount(sender, e);


MessageBox.Show("HA er nu bestilt!", "Mail afsendt", MessageBoxButtons.OK);


//Slet habestilling.csv fil fra PC
if (File.Exists(@"habestilling.csv"))
{
File.Delete(@"habestilling.csv");
}

//Slet vedhæftet sti fra tekstboksen
vedhæft.Text = "";

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}


No enough information.

Since TextBox.Text just returns a string, you just have to type "user1" (without the quotes) into your usertextbox and you get the exact same thing.

The server is telling you that whatever you're doing isn't authorized, given the data that you sent.
 
Share this answer
 
Comments
Member 11380736 16-Jan-15 16:52pm    
But my user info comes from this sql coding:

//Brugers emailid
command.CommandText = "SELECT emailid from bruger WHERE bruger = @bruger";

globalvariables.Con.Open();
command.ExecuteScalar();
user.Text = command.ExecuteScalar().ToString();

//Luk forbindelsen
globalvariables.Con.Close();



//This writes my account in my user textbox, but pressing send button gives the error.
user.Text = command.ExecuteScalar().ToString();
Dave Kreskowiak 16-Jan-15 19:18pm    
OK, so step through the code and make sure the TextBox gets the value you expect BEFORE your code tries to send an email.
I solved it.
The problem was that when the user id was found in the mssql database and writen to the bruger.Text it added some empty spaces. I think it's the .ToString();that did this.

My solution was to write this line:
bruger.Text = bruger.Text.Replace(" ", "");


C#
//Brugers emailid
command.CommandText = "SELECT emailid from bruger WHERE bruger =            @bruger";

globalvariables.Con.Open();
command.ExecuteScalar();
bruger.Text = command.ExecuteScalar().ToString();

//Dette fjerner tomme mellemrum i bruger.Text som ToString har lavet
bruger.Text = bruger.Text.Replace(" ", "");

//Luk forbindelsen
globalvariables.Con.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900