Use VB6 to write programs for sending emails. There are a variety of email components to choose from, such as MAPIMessages, CDONTS (NTSCollaborationDataObjects) included with IIS4.0, and email software developed by many third-party manufacturers. This article introduces how to use CDONTS to develop a mail program under VB.
1. Confirm that the SMTP server is installed
First, you must confirm that the SMTP server is installed on the IIS server. This is the option that is installed by default when installing OptionPack. If the SMTP server has been installed, there will be a file named cdonts.dll in the System32 directory.
2. The simplest email sending program
The following is the simplest email sending program, which sends an E-mail with the title "Title" and the content "Hello" from [email protected] to [email protected]:
PRivateSubcmdSendMail_Click()
DimobjmailasObject
SetobjMail=CreateObject("CDFONTS.DLL")
ObjMail.Send"[email protected]","[email protected]","Title","Hello"
SetobjMail=nothing
EndSub
3. Send email attachments
If there is an attached file that needs to be sent along with the email, you can use the AttachFile method of CDONTS:
PrivateSubcmdSendMail_Click()
DimobjMailasObject
SetobjMail=CreateObject("CDFONTS.DLL")
WithObjMail
.From="[email protected]"
.To="[email protected]"
.Subject="Title"
.Body="Hello"
.AttachFile"c:/document/sample.doc","sample.doc"
.Send
EndWith
SetobjMail=nothing
EndSub
4.Commonly used properties and methods of CDONTS components
Commonly used properties and methods of CDONTS components are listed below:
property:
Version: Returns the version number of the CDONTS component
MailFormat: Email format, 1-normal text type; 0-MIME type
Cc: Set one or more email addresses for reference
From: The email sender’s address
To: The address of the email recipient
Subject: title
Body: content
BodyFormat: content format, 1-normal text; 0-hypertext
method:
AttachFile: Attach a file
AttachURL: attach an address
Send: send email
5.Send E-mail in hypertext format
You must have seen many emails using hypertext format, which can not only contain pictures, sounds, but also embed animations. If you send such an email to your friend, it will definitely bring him or her an unexpected surprise, because this is something that cannot be done with ordinary email sending programs. The following uses the CDONTS component to send an email in HTML format, which contains a picture named sample.gif. The program is as follows:
PrivateSubcmdSendMail_Click()
DimobjMailasObject
SetobjMail=CreateObject("CDFONTS.DLL")
DimstrHTMLasString
StrHTML="〈html〉〈head〉"
StrHTML=StrHTML "〈title〉Email〈/title〉〈/head〉 using hypertext format
StrHTML=StrHTML "〈body〉〈p〉〈strong〉Example of sending email using CDONTS"
StrHTML=StrHTML "〈imgsrc=sample.gif〉〈/strong〉〈/p〉"
StrHTML=StrHTML "〈p〉The above example has embedded image sample.gif〈/p〉"
StrHTML=StrHTML "〈/body〉〈/html〉"
WithObjMail
.From="[email protected]"
.To="[email protected]"
.Subject="Title"
.Body=strHTML
.AttachURL"c:/mydata/sample.gif","sample.gif"
.BodyFormat=0
.EmailFormat=0
.Send
EndWith
SetobjMail=nothing
EndSub
At this point, I believe you must have discovered a flaw in SMTP email processing. The SMTP server receives your output message, but does not verify the sender's name and address. This is determined by the asynchronous sending mechanism of SMTP, but someone may use it with ulterior motives to pretend to be someone else and send emails! This article strongly recommends that you do not try it easily. Once your ISP knows about your behavior, your account will most likely be deleted immediately. ->