Thursday, July 31, 2008

Sending an SMTP Message

Well, I make frequent mention of this inability to find easy code to send an SMTP message, but ironically I have not yet posted any source code on the matter. Here she is.

First, put the following in your declarations area:

Imports System.Net.Mail

Second, declare the appropriate objects.

This declares the message itself
Dim msg As New System.Net.Mail.MailMessage
‘This declares the SMTP-related items object
Dim smtp As New System.Net.Mail.SmtpClient
‘Declare the From Address
Dim addrFrom As New System.Net.Mail.MailAddress("Insert your from address here")
‘Declare the email address for the recipient – you can add more than one recipient – just keep declaring
Dim addrTo As New System.Net.Mail.MailAddress(“Insert recipient address here”)

Third, the code.

‘Assign objects to MailMessage
msg.From = addrFrom
msg.To.Add(addrTo)

‘If you want an attachment – use this line. Repeat as necessary.
msg.Attachments.Add(New Attachment(“Insert File and Path”))

‘Subject to Email
msg.Subject = “Subject”

‘Body Contents
msg.Body = “Body”

‘Server Settings, Etc (NOTE: I use My.Settings. Replace with your contents as needed)
smtp.Host = My.Settings.Server
smtp.Port = My.Settings.Port
smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
smtp.Credentials = New System.Net.NetworkCredential(My.Settings.Username, My.Settings.Password)

‘And, send the message
smtp.Send(msg)

My standard disclaimer is that you should have Try/Catch statements in case you receive errors attempting to send.

On a side note, I did point to an Exchange server that had an SMTP connector installed and it worked the first time without a hitch.

Saturday, July 26, 2008

New Blog Title

So soon? Yes - within a week I am changing the title. I chose not to restrict this blog to solving the void of simple programming advice to VB.NET as the problem seems to be just a programming thing - regardless of the language. I have already come across other issues dealing with ASP & ASP.NET- so its best to nip the nomenclature issue in the bud. Hence, as I journey down roads driven by client needs and have to cross new bridges, I will share my solutions to frustrations as I find them.

Thursday, July 24, 2008

'The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified.'

"The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again"

This problem occurs in Visual Studio 2005 Express (VB). Of course, if you do any searching on the error, you will find that many programmers experiencing this problem didn't change anything. Neither did I. I followed the suggestion of rebuilding project. Didn't work. Tried commenting out specific problem. Didn't help. Commented out problem line (property irrelevant and set via user settings from My. Settings) and restarted VS. Designer opened fine.

I don't know why - but it seems many others on the forums don't either. At least give the easy trick a try.

Download File via FTP in VB.NET

This is another shocking lack of .NET - easy FTP. While version 2.0 of .NET does in fact have native FTP handling capabilities (WinHTTP), it is for a lack of a better word, a pain - especially when you are trying something simple. If you are building a commercially distributed, mission-critical, or enterprise class application, you probably will find it valuable to go through the extra steps to use native functionality. However, if FTP is a relatively quick need, this has my recommendation.

I searched high and low for an easy way to do this. There are plenty of complex methods, controls available for sale, and outdated methods to accomplish a simple FTP download. The simplest way to do so (and have it working by lunchtime) is to use the edtFTPnet control available at http://www.enterprisedt.com/products/edtftpnet/overview.html. It is free for commercial use under a GPL license. My code sample as below:

First, add the control to your toolbox.
Second, drag the control onto the applicable form.
Third, your code must specify connection characteristics. Sample below:

NOTE: I am referencing My.Settings where the data is stored in the application - you can easily use direct references or assign variables to control the data. Also, I have a setting in My.Settings for ACTIVE or PASSIVE file transfer mode.

FTPConnection1 is the control name.

You can also set these settings directly in the control properties.

'load FTP settings
FtpConnection1.ServerAddress = My.Settings.FTPServer
FtpConnection1.UserName = My.Settings.FTPUsername
FtpConnection1.Password = My.Settings.FTPPassword
FtpConnection1.ServerPort = CInt(My.Settings.FTPPort)
FtpConnection1.ServerDirectory = My.Settings.FTPDirectory
If My.Settings.FTPConnectionMode = "1" Then
FtpConnection1.ConnectMode = EnterpriseDT.Net.Ftp.FTPConnectMode.ACTIVE
Else
FtpConnection1.ConnectMode = EnterpriseDT.Net.Ftp.FTPConnectMode.PASV
End If

Fourth, the connection and file download code.


FtpConnection1.Connect()
FtpConnection1.ChangeWorkingDirectory("Insert FTP path here")
FtpConnection1.DownloadFile("Insert Where file is going on PC", "Insert File Name on FTP Server")
FtpConnection1.Close()

That's it.

Sunday, July 20, 2008

Why the Blog

Blogging is supposed to be a soap box, right? Lately, I have encountered a shockingly awful lack of simple and direct advice for any level of VB.NET programmer wishing to embark on a programming task that they have never done. You can be the world's top clustered server farm engineer yet have never attempted to email a simple message from .NET. Even with skills and knowledge of that level, you won't figure out how to do it on your own and it would be mightily helpful if direct, concise examples existed with extremely simplifed code.

In the last few months, I have lived the misery of this conundrum. I have been developing a medium-scale application that merely touches on various wide-ranging features. I try to find simple advice and answers on say, sending a very simple SMTP email message, and all I can find are examples on how to write your own version of Outlook with mountains of code to boot. All I needed was to send a message......

Necessity is the mother of invention, so I am committing these findings to writing - hopefully to spare someone else the same fruitless search.