Geoffrey Emery
Tech Goodness

Send Email From Asp.Net

April 28, 2009 08:39 by gemery

I just finished a client app with a web form to send email. Being that this used to be old hat i found that i had lost all my sample code for this. So here it is for reference.

The process of sending mail is the same for Windows apps and asp.net websites as the same .Net classes are used. The process can be slightly shortened by specifying default SMTP settings in the web.config or app.config file. Here, I’m showing the full version of the code and it does not rely on any configuration settings. The code also specifies unicode encoding for the subject and body.

using System.Net.Mail; 
using System.Net;
//Create the mail message 
MailMessage mail = new MailMessage(); 
mail.Subject = "Subject"; 
mail.Body = "Main body goes here";
//the displayed "from" email address 
mail.From = new System.Net.Mail.MailAddress(you@live.com);  
mail.IsBodyHtml = false; 
mail.BodyEncoding = System.Text.Encoding.Unicode; 
mail.SubjectEncoding = System.Text.Encoding.Unicode; 
//Add one or more addresses that will receive the mail 
mail.To.Add("me@live.com"); 
//create the credentials 
NetworkCredential cred = new NetworkCredential( 
"you@live.com", //from email address of the sending account 
"password"); //password of the sending account 
//create the smtp client...these settings are for gmail 
SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 
smtp.UseDefaultCredentials = false; 
smtp.EnableSsl = true;
//credentials (username, pass of sending account) assigned here 
smtp.Credentials = cred;  
smtp.Port = 587; 
//let her rip 
smtp.Send(mail);

Tags: ,
Categories: Tutorials | Code
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

MSDN Mobile Phone Demo Slides And Code and Pictures

February 27, 2009 07:53 by gemery

Pictures from the event...

 

 

The code

 

Hope All enjoyed Please feelk free to ping me if you have any questions at all


Tags: , , , ,
Categories: Code | Microsoft
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Virtual Earth Tutorial 2: Adding A Specific Map To A Web Page and Deep Dive into the Load Map Function

April 23, 2008 17:02 by gemery

 

 < Previous Tutorial Adding a basic Map 

LIVE DEMO | SOURCE

We are going to start off with the same code base of the last the Tutorial but this time we want to add a specific map to the page instead of the random map. There are actually several ways to do this but for his situation we are going to do it using a lat and longitude. The first thing we need to do is  get the latitude and longitude for the place that we want to have the map centered on. There are several way to do this but for now i just typed in "lat long for los angeles" and it returned this to me.

34.0° N - 118.2° W

So we are going to be taking this Lat long and use it to modify  the existing LoadMap() function. As you can see below we are going to new up a VELatLong object and pass in the lattitude and longitude to its constructor.

map.LoadMap(new VELatLong(34.0, -118.2)); 

Add in this to the sample from tutorial 1 and you get this map.

image

As you can see the map is centered on Los Angeles.This is really cool but it gets even better. This is where we do the deep dive into what the rest of the variables that  you can pass do. Below you will see a example of the function with all the possible variables that you can pass in.

LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch, tileBuffer, mapOptions)

 

Lets talk about what each one of these variables does

VELatLong - A VeLatLong Class object that represents the center of the map..Like LA
zoom - A zoom level to display. Valid values range from 1 through 19. Optional. Default is 4. 1 being the highest(from Space)
style -

A VEMapStyle Enumeration value specifying the map style. Optional. Default is VEMapStyle.Road. 'h' -hybid, 'r' - road, 'o' - obilique , 'a'  -a ariel

Fixed - A Boolean value that specifies whether the map view is displayed as a fixed map that the user cannot change or move it around. Default is false.
mode - A VEMapMode Enumeration value that specifies whether to load the map in 2D or 3D mode. Optional. Default is VEMapMode.Mode2D or Mode3D. Note 3d requires a plug in
showSwitch - A Boolean value that specifies whether to show the map mode switch on the dashboard control this means change from 2d to 3d. Optional. Default is true (the switch is displayed).
TileBuffer - How much tile buffer to use when loading map. Default is 0 (do not load an extra boundary of tiles). This parameter is ignored in 3D mode. Bigger buffer here gives the user faster reaction when they start to drag the map around
mapOptions - A VEMapOptions Class that specifies other map options to set.

 

Now that we have that out of the way we can go ahead and play with all the values.Check out this demo i have set up for you. Of course the all source included. It works best in firefox right now due to a bug in ie. I  am working on a fix.

LIVE DEMO | SOURCE

image


Zip And Unizp Files in VB.net and C#

April 15, 2008 18:00 by gemery

This is some code I used to zip and unzip some files for a project I just finished.  This code utilizes the j# langauge library. You might need to add a refrence to vsjlib.  If you running a 64 bit server you may need to install the j# 64 bit redistibutable

vb

Imports java.util.zip
Imports java.io
    Public Class Ziplib
 
 
 
        Public Shared Function Uncompress(ByVal strZipFileName As String, ByVal strLocation As String) As Boolean
            Try
                Dim oFileInputStream As New java.io.FileInputStream(strZipFileName)
                Dim oZipInputStream As New java.util.zip.ZipInputStream(oFileInputStream)
                Dim bTrue As Boolean = True
                Dim sbBuf(1024) As SByte
                While 1 = 1
                    Dim oZipEntry As ZipEntry = oZipInputStream.getNextEntry()
                    If oZipEntry Is Nothing Then Exit While
                    If oZipEntry.isDirectory Then
                        If Not My.Computer.FileSystem.DirectoryExists(strLocation & oZipEntry.getName) Then
                            My.Computer.FileSystem.CreateDirectory(strLocation & oZipEntry.getName)
                        End If
                    Else
                        Dim oFileOutputStream As New java.io.FileOutputStream(strLocation.Replace("\", "/") & oZipEntry.getName())
                        While 1 = 1
                            Dim iLen As Integer = oZipInputStream.read(sbBuf)
                            If iLen < 0 Then Exit While
                            oFileOutputStream.write(sbBuf, 0, iLen)
                        End While
                        oFileOutputStream.close()
                    End If
                End While
                oZipInputStream.close()
                oFileInputStream.close()
            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try
 
        End Function
 
 
 
        Public Shared Function Compress(ByVal strFileNames() As String, ByVal strZipFileName As String) As Boolean
            Try
                Compress = False
                Dim oFileOutputStream As New java.io.FileOutputStream(strZipFileName)
                Dim oZipOutputStream As New java.util.zip.ZipOutputStream(oFileOutputStream)
                For Each strFileName As String In strFileNames
                    Dim oFileInputStream As New java.io.FileInputStream(strFileName)
                    Dim oZipEntry As New java.util.zip.ZipEntry(strFileName.Substring(3).Replace("\", "/"))
                    oZipOutputStream.putNextEntry(oZipEntry)
                    Dim sbBuf(1024) As SByte
                    While 1 = 1
                        Dim iLen As Integer = oFileInputStream.read(sbBuf)
                        If iLen < 0 Then Exit While
                        oZipOutputStream.write(sbBuf, 0, iLen)
                    End While
                    oZipOutputStream.closeEntry()
                    oFileInputStream.close()
                Next
                oZipOutputStream.close()
                oFileOutputStream.close()
                Return True
            Catch ex As Exception
 
                Throw New Exception(ex.Message)
 
            End Try
 
        End Function
 
    End Class

c#

 

 
using java.util.zip; 
using java.io; 
public class Ziplib 
{ 
    
    
    
    public static bool Uncompress(string strZipFileName, string strLocation) 
    { 
        try { 
            java.io.FileInputStream oFileInputStream = new java.io.FileInputStream(strZipFileName); 
            java.util.zip.ZipInputStream oZipInputStream = new java.util.zip.ZipInputStream(oFileInputStream); 
            bool bTrue = true; 
            sbyte[] sbBuf = new sbyte[1025]; 
            while (1 == 1) { 
                ZipEntry oZipEntry = oZipInputStream.getNextEntry(); 
                if (oZipEntry == null) 
                    break; // TODO: might not be correct. Was : Exit While 
 
                if (oZipEntry.isDirectory) { 
                    if (!My.Computer.FileSystem.DirectoryExists(strLocation + oZipEntry.getName)) { 
                        My.Computer.FileSystem.CreateDirectory(strLocation + oZipEntry.getName); 
                    } 
                } 
                else { 
                    java.io.FileOutputStream oFileOutputStream = new java.io.FileOutputStream(strLocation.Replace("\\", "/") + oZipEntry.getName()); 
                    while (1 == 1) { 
                        int iLen = oZipInputStream.read(sbBuf); 
                        if (iLen < 0) 
                            break; // TODO: might not be correct. Was : Exit While 
 
                        oFileOutputStream.write(sbBuf, 0, iLen); 
                    } 
                    oFileOutputStream.close(); 
                } 
            } 
            oZipInputStream.close(); 
            oFileInputStream.close(); 
        } 
        catch (Exception ex) { 
            throw new Exception(ex.Message); 
        } 
        
    } 
    
    
    
    public static bool Compress(string[] strFileNames, string strZipFileName) 
    { 
        bool functionReturnValue = false; 
        try { 
            functionReturnValue = false; 
            java.io.FileOutputStream oFileOutputStream = new java.io.FileOutputStream(strZipFileName); 
            java.util.zip.ZipOutputStream oZipOutputStream = new java.util.zip.ZipOutputStream(oFileOutputStream); 
            foreach (string strFileName in strFileNames) { 
                java.io.FileInputStream oFileInputStream = new java.io.FileInputStream(strFileName); 
                java.util.zip.ZipEntry oZipEntry = new java.util.zip.ZipEntry(strFileName.Substring(3).Replace("\\", "/")); 
                oZipOutputStream.putNextEntry(oZipEntry); 
                sbyte[] sbBuf = new sbyte[1025]; 
                while (1 == 1) { 
                    int iLen = oFileInputStream.read(sbBuf); 
                    if (iLen < 0) 
                        break; // TODO: might not be correct. Was : Exit While 
 
                    oZipOutputStream.write(sbBuf, 0, iLen); 
                } 
                oZipOutputStream.closeEntry(); 
                oFileInputStream.close(); 
            } 
            oZipOutputStream.close(); 
            oFileOutputStream.close(); 
            return true; 
        } 
        catch (Exception ex) { 
            
            throw new Exception(ex.Message); 
            
        } 
        return functionReturnValue; 
        
    } 
    
}
 
Technorati Tags: ,,,

Tags: , ,
Categories: Code
Actions: E-mail | Permalink | Comments (11) | Comment RSSRSS comment feed

Code From Code Camp

March 26, 2008 22:37 by gemery

This is super late and not fully documented as i would like but i have been getting several requests from people for this so i wanted to get this code out. Sorry for the delay a update to this will be coming soon.

Thanks for your patience. Remember this isn't a release version.

Download The Code


Tags:
Categories: Code | Examples
Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed