Last time I used jmail.net to write a Web-based email sending applet, which is suitable for real-time email sending, such as obtaining and retrieving passwords. However, in actual use, there is also a need to send scheduled emails. For example, I want to add an "email reminder" function to the system, users can include the things to be reminded, send an email automatically at the specified time, or list their colleagues on their birthdays today, and send e-mails in the name of the Human Resources Department.
SQL Server 2005 has an easy-to-use "database mail" function. It used to use database mail and then use scheduled tasks for processing, however, considering the impact on the performance of the database, some netizens thought that the main business of the database was to store data and should not send all emails to it, therefore, we decided to write a small program to process these things.
Although this kind of mini-program may be very simple for everyone, I would like to share it with you, so that you can make reference for anyone who needs it in the future or who is new to VB.net, I would like to share the compilation steps. If you think there are better solutions, I also hope to make common progress together.
Development Environment: Windows 2003/VS 2005/SQL Server 2005. The language used is VB.net 2005.
The first thing to consider is the function. To send mails at a scheduled time, you need to use the scheduled function. Then, you need to read the emails to be sent from the database, and finally send the emails through the mail sending function. At the beginning of the design, I wanted to make it a general version. All projects are not fixed and can be set through parameters. At least the following content is required: (1) database server information, this includes the server address, user name, password, and database name. You still want to specify the name of the data table that stores the email. Later, you should be able to unify the data table and leave it unspecified. (2) information about the email server, including the SMTP server address, port number, user name, password, password verification method, sender address, and sender name; (3) Functions of the program itself, this includes the scan interval and log file storage path.
Secondly, I started interface design. To be honest, my coding level is very low, but the interface design level is lower. Considering that this software is running on the server, I hope to design the interface a little less, finally, we decided to use the tabcontrol Control to distribute multiple contents on different card pages. Let's take a look at the specific content.
The Interface Design lags behind and formal code design begins.
First, I want to save parameters. Currently, there are four common methods to save parameters: INI file storage, registry storage, XML file storage, and database storage. Because the program itself is very simple, I decided to save the INI file in the current directory of the program. To read and write INI files, you can use the Windows API function. The Code is as follows:
'Read ini API function private declare function getprivateprofilestring lib "Kernel32" alias "alias" (byval lpapplicationname as string, byval lpkeyname as string, byval lpdefault as string, byval lpreturnedstring as string, byval nsize as int32, byval lpfilename as string) as int32 'write ini API function private declare function writeprivateprofilestring lib "Kernel32" alias "alias" (byval lpapplicationname as string, byval lpkeyname as string, byval lpstring as string, byval lpfilename as string) as int32 'read the INI File Content public function getini (byval section as string, byval appname as string, byval lpdefault as string, byval filename as string) as string dim STR as string = lset (STR, 256) getprivateprofilestring (section, appname, lpdefault, STR, Len (STR), filename) return Microsoft. visualBasic. left (STR, instr (STR, CHR (0)-1) end function 'write INI File Operation Public Function writeini (byval section as string, byval appname as string, byval lpdefault as string, byval filename as string) As long writeini = writeprivateprofilestring (section, appname, lpdefault, filename) end Function
Note that in VB.net, the final data type of the API function should be int32, while in VB, it is long.
Method: Read the content of the INI file during form loading, and fill the parameter content in the corresponding parameter box.
Private sub form1_load (byval sender as system. object, byval e as system. eventargs) handles mybase. load dim INIFILE as string 'ini file name is sendmail. ini inifile = application. startuppath + "\ Sendmail. ini "if not file. exists (INIFILE) Then msgbox ("Sendmail. the INI file does not exist. Check whether "+ vbcrlf + INIFILE, msgboxstyle. okonly, "file not found") end if txt_dbserver.text = getini ("Database", "server", "", INIFILE) txt_smtpserver.text = getini ("SMTP", "server ", "", INIFILE) dim savelog as integer = getini ("Sendmail", "savelog", "", INIFILE) if (savelog = 1) then chk_logfile.checked = true elseif (savelog = 0) Then chk_logfile.checked = false end if 'other projects are similar and end sub is not listed one by one
I will write it here today. In the next article, I will write about how to use encrypter. decryptdes as the parameter to encrypt and decrypt sensitive information and design the database structure.