Join Webhostforasp.net
dailtcode logo

Service not available, closing transmission channel. The server response was: 4.3.2 Service not available, closing transmission channel

clock October 15, 2008 22:20 by author mderaeve

Trying to look up this error only gave 2 results and did not provide the simple solution for this problem I found.

Thsi code worked fine before we changed to a secure Exchange 2007 server.

MailBody.Insert(0, "Dear, <br><br>");
 
            string[] mails = toString.Split(';');
  
            MailAddress from = new MailAddress(sharepoint@dailycode.net);
 
            MailMessage Message = new MailMessage();
 
            foreach (string mailAdress in mails)
            {
                Message.To.Add(mailAdress);
            }
            Message.From = from;
            Message.Subject = "Document needs revising!";
            Message.Body = MailBody.ToString();
            Message.IsBodyHtml = true;
             
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = System.Configuration.ConfigurationSettings.AppSettings["SmtpServer"];
            smtpClient.Send(Message);

 

Then we changed and I got the beautifull error message: "Service not available, closing transmission channel. The server response was: 4.3.2 Service not available, closing transmission channel". So what did the trick was adding:

            smtpClient.UseDefaultCredentials = true;


You can also pass credentials if the program will run under a local user.  This can be done like this:

      smtpClient.Credentials = GetMailCredentials();
 
private static System.Net.NetworkCredential GetMailCredentials()
        {
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential();
            cred.UserName = ConfigurationSettings.AppSettings["User"];
            cred.Password = ConfigurationSettings.AppSettings["Pwd"];
            cred.Domain = ConfigurationSettings.AppSettings["Dom"];
            return cred;
        }

 

Reading this makes it seem very logic, secure server disables anonymous login.

 


Print multiple pages with PrintDocument class

clock October 1, 2008 03:56 by author mderaeve

When you just want to print a single page, it very easy and all you need is a couple of lines of code:

Declare the instance:

private PrintDocument printDoc = new PrintDocument();
 

Add the eventhandler to the printdocument object:

printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

Then you can start printing your document in the event handler:

int yPosition = e.MarginBounds.Bottom - 10;
int xPosition = e.MarginBounds.Right - 150;
string toPrint = string.Format("Printed on {0:dd-MMM-yyyy}", DateTime.Now);
e.Graphics.DrawString(toPrint, _normalFont, new SolidBrush(Color.Black), xPosition, yPosition);
 

But when you start writing more and more lines on the page, you will notice that it will only print one page. In order to print several pages, we need to set the HasMorePages flag. What you have to understand is that setting this flag will fire a new print page event after this event is handled.

In this example I have to print a dataset with data from a database. In my loop I have to check: whether the height of the current line is not greater then the page bottom bound. If so, I will have to remember which line I'm in, set the hasmorepages flag and exit the loop. Here is the code:

private int countline = 0;
... 
private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
{
...
for (int i = countLine; i < ds.Tables[0].Rows.Count;i++)
{
	DataRow dt = ds.Tables[0].Rows[i];
	x = 20;
	e.Graphics.DrawString(dt["Study_number"].ToString(), _normalFont, Brushes.Black, x, y);
	x = x + 130;
        e.Graphics.DrawString(dt["tube"].ToString(), _normalFont, Brushes.Black, x, y);
        x = x + 130;
        e.Graphics.DrawString(dt["total"].ToString(), _normalFont, Brushes.Black, x, y);
        x = x + 130;
        y = y + 20;
        if (y + 20 > e.MarginBounds.Bottom)
        {
        	countLine = i+1;
                e.HasMorePages = true;
                break;
	}
}
 

The countline variable is declare in the class and keeps track of the record I'm handling. When a page is full, the event is ended. When a new printpage event is fired the loop starts where the last page stopped. The printdocument is a performant way to pint documents. I fell its perfect for printing data tables in a simple and fast way. If you start adding style to it, you'll better look for open source or 3rd party software, because you can loose to much time.



How to count string in string

clock August 15, 2008 08:35 by author mderaeve

I was looking for a c# function to count the number of occurrences of a string in a string. But I couldn't find any by default.

So I wrote my own little function. Migth come in handy!

In this scenario I had a string that contains line feeds, I had to know how many to correctly calculate the length of the string. Here is the function that does the work:

public static int Count(string src, string find)
        {
            int ret = 0;
            int len = find.Length;
            for (int i=0; i < src.Length-len;i++)
            {
                if (src.Substring(i,len) == find)
                {
                    ++ret;
                }
            }
            return ret;
        }

So I was looking for "\r\n" in a string:

int countNewLines = Count(day.Remark,"\r\n");
 

It's Simple and easy, any person could find this, but I like to blog it so next time I won't have to look for it again.

This improved function by feedback of Tom is more performant:

private int countOccurences(string text, string toFind)
{
  return (toFind.Length > 0) ? 
  (text.Length - text.Replace(toFind, string.Empty).Length) / toFind.Length : 0;
}
 

I'll explain what happens: it returns the length of the text minus the lenght of the text without the string that you are looking for divided by the length of the search string.

Eg. text = aaaabgaabg to find = bg. string lenght = 10 - string lenght without bg = 6 makes 4. We divide 4 by the length of the search string bg = 2 makes 2 occurences: (10 - 6) /2=2



Short if structure

clock August 6, 2008 20:19 by author mderaeve

It keeps slippin my mind, so here an example, making it easy to find:

string test = i<0?"yes":"no";
 

If i < 0 then it returns string "yes" else "no".

Syntax:  Res = Coparison ? returnIfTrue : returnIfFalse ;



Creation or modify date of a file

clock July 15, 2008 13:49 by author mderaeve

Very easy in .Net:

if (File.Exists(fileName))

{           

            FileInfo info = new FileInfo(fileName);

            lblLastGenerated.Text = info.LastWriteTime.ToLongDateString() + " " + info.LastWriteTime.ToShortTimeString();

}

Msdn link to the fileinfo class: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx



Search


Categories





Locations of visitors to this page

About

Mark Deraeve

Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

This site was created by Jetro Wils and Deraeve Mark powered by BlogEngine

© Copyright 2008

Sign in