
How to send cc:Mail from VB or MS Access
I've been deluged with requests for this code:
Here's code to send cc:mail from Microsoft Access. "ToAddr" is the
cc:mail address of the
recipient. "Subject" is the subject.
Dim chan, i
Dim ToAddr As String
Dim Subject As String
' Set ToAddr, Subject field!
On Error Resume Next
' Initiate the conversation
chan = DDEInitiate("wmail", "SendMail")
If Err Then
MsgBox "cc:Mail isn't running - please start it!"
Exit Sub
End If
On Error GoTo 0
If IsNull(ToAddr) Then
MsgBox "Can't send email - address field is blank!"
Exit Sub
End If
If Len(ToAddr) = 0 Then
MsgBox "Can't send email - address field is blank!"
Exit Sub
End If
i = SysCmd(SYSCMD_INITMETER, "Sending email to '" & ToAddr & "'", 100)
DDEExecute chan, "NewMessage"
DDEExecute chan, "TO " & ToAddr
DDEExecute chan, "Subject Re: " & Left$(Subject, 70)
DDEExecute chan, "Receipt 0"
' The text of the message starts here:
DDEExecute chan, "Text This is your email text here." + Chr$(10)
' Here's how to send a blank line...
DDEExecute chan, "Text " + Chr$(10)
i = SysCmd(SYSCMD_UPDATEMETER, 90)
DDEExecute chan, "Send"
i = SysCmd(SYSCMD_UPDATEMETER, 100)
DDETerminate chan
i = SysCmd(SYSCMD_REMOVEMETER)
Here's how to do the same thing in Visual Basic. To use it, just "Call
SendMail(user, subject,
message)". Don't forget to put a non-visible Text1 textbox on the form
somewhere.
Sub DDEExecute (chan, cmd)
Text1.LinkExecute cmd
End Sub
Function DDEInitiate (app, topic)
Const AUTOMATIC = 1
Const MANUAL = 2
Const NONE = 0
Text1.LinkMode = NONE
Text1.LinkTopic = app + "|" + topic
Text1.LinkItem = "Text1"
Text1.LinkMode = MANUAL
DDEInitiate = ""
End Function
Sub DDETerminate (chan)
Const NONE = 0
Text1.LinkMode = NONE
End Sub
Sub SendMail (user As String, subj As String, msg As String)
Dim chan
On Error Resume Next
chan = DDEInitiate("wmail", "SendMail")
If Err <> 0 Then
On Error GoTo 0
chan = Shell("s:\mail\ccmail\wmail.exe", 2)
chan = DDEInitiate("wmail", "SendMail")
While Err <> 0
DoEvents
chan = DDEInitiate("wmail", "SendMail")
Wend
End If
Call DDEExecute(chan, "NewMessage")
Call DDEExecute(chan, "Subject " & subj)
Call DDEExecute(chan, "Receipt 0")
Call DDEExecute(chan, "Priority Urgent")
Call DDEExecute(chan, "Text " + msg + Chr$(10))
Call DDEExecute(chan, "Text " + Chr$(10))
Call DDEExecute(chan, "TO " & user)
Call DDEExecute(chan, "Send")
Call DDETerminate(chan)
End Sub