Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to send emails from a VB application 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
without showing the browser, I want to send the emails From VB application. if u have any ideas about this pls send me.
 
Nick,<br><br>That's just *gotta* be a faq... <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
There wans't an FAQ about this.<br>There now is one which details how to send mail using either the outlook object library or using CDO 1.21<br><br>It is under the E-mail category and is called <br><br>How to send Mail From VB With or Without Using Outlook<br><br><br>Hope this will help <br><br>
 
This is as simple as it gets my friend.<br><br>Private Sub Command1_Click()<br>Call SendMailMessage<br>End Sub<br><br>Public Sub SendMailMessage()<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim olApp As Object, olItem As Object<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Set olApp = CreateObject(&quot;Outlook.Application&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;Set olItem = olApp.CreateItem(olMailItem)<br>&nbsp;&nbsp;&nbsp;&nbsp;olItem.Recipients.Add &quot;<A HREF="mailto:admin@jamaicanhomes.com">admin@jamaicanhomes.com</A>&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;olItem.Subject = &quot;Some Subject&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;olItem.Body = &quot;This is the actual message&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;olItem.DeleteAfterSubmit = True<br>&nbsp;&nbsp;&nbsp;&nbsp;olItem.OriginatorDeliveryReportRequested = False<br>&nbsp;&nbsp;&nbsp;&nbsp;olItem.ReadReceiptRequested = False<br>&nbsp;&nbsp;&nbsp;&nbsp;olItem.Send<br>End Sub<br><br>
 
With this approach, how do you select the account you want to send it from if you have multiple accounts setup?
 
<br>For those without Exchange (still on cc:Mail), is there a way to accomplish this?<br>
 
You can do this with Microsoft Winsock Control 6.0

Usage:
SendEmail EmailServer, FromName, FromEmailAddress, ToName, ToEmailAddress, EmailSubject, EmailBodyOfMessage

General Declarations[tt]
__________________________________________________________
Dim Response As String, Reply As Integer, DateNow As String
Dim first As String, Second As String, Third As String
Dim Fourth As String, Fifth As String, Sixth As String
Dim Seventh As String, Eighth As String
Dim Start As Single, Tmr As Single
__________________________________________________________

Sub SendEmail(MailServerName As String, FromName As String, FromEmailAddress As String, ToName As String, ToEmailAddress As String, EmailSubject As String, EmailBodyOfMessage As String)

Winsock1.LocalPort = 0 'Must set local port to 0 (Zero) or you can only send 1 e-mail pre program start

If Winsock1.State = sckClosed Then 'Check to see if socet is closed
DateNow = Format(Date, &quot;Ddd&quot;) & &quot;, &quot; & Format(Date, &quot;dd Mmm YYYY&quot;) & &quot; &quot; & Format(Time, &quot;hh:mm:ss&quot;) & &quot;&quot; & &quot; -0600&quot;
first = &quot;mail from:&quot; + Chr(32) + FromEmailAddress + vbCrLf ' Get who's sending E-Mail address
Second = &quot;rcpt to:&quot; + Chr(32) + ToEmailAddress + vbCrLf ' Get who mail is going to
Third = &quot;Date:&quot; + Chr(32) + DateNow + vbCrLf ' Date when being sent
Fourth = &quot;From:&quot; + Chr(32) + Chr(34) + FromName + Chr(34) + &quot;<&quot; + FromEmailAddress + &quot;>&quot; + vbCrLf ' Who's Sending
Fifth = &quot;To:&quot; + Chr(32) + Chr(34) + ToNametxt + Chr(34) + &quot;<&quot; + ToEmailAddress + &quot;>&quot; + vbCrLf ' Who it going to
Sixth = &quot;Subject:&quot; + Chr(32) + EmailSubject + vbCrLf ' Subject of E-Mail
Seventh = EmailBodyOfMessage + vbCrLf ' E-mail message body
Ninth = &quot;X-Mailer: Pump's sendmail&quot; + vbCrLf ' What program sent the e-mail, customize this
Eighth = Fourth + Third + Ninth + Fifth + Sixth ' Combine for proper SMTP sending

Winsock1.Protocol = sckTCPProtocol ' Set protocol for sending
Winsock1.RemoteHost = MailServerName ' Set the server address
Winsock1.RemotePort = 25 ' Set the SMTP Port
Winsock1.Connect ' Start connection

WaitFor (&quot;220&quot;)

StatusTxt.Caption = &quot;Connecting....&quot;
StatusTxt.Refresh

Winsock1.SendData (&quot;HELO yourcomputersname&quot; + vbCrLf)

WaitFor (&quot;250&quot;)

StatusTxt.Caption = &quot;Connected&quot;
StatusTxt.Refresh

Winsock1.SendData (first)

StatusTxt.Caption = &quot;Sending Message&quot;
StatusTxt.Refresh

WaitFor (&quot;250&quot;)

Winsock1.SendData (Second)

WaitFor (&quot;250&quot;)

Winsock1.SendData (&quot;data&quot; + vbCrLf)

WaitFor (&quot;354&quot;)


Winsock1.SendData (Eighth + vbCrLf)
Winsock1.SendData (Seventh + vbCrLf)
Winsock1.SendData (&quot;.&quot; + vbCrLf)

WaitFor (&quot;250&quot;)

Winsock1.SendData (&quot;quit&quot; + vbCrLf)

StatusTxt.Caption = &quot;Disconnecting&quot;
StatusTxt.Refresh

WaitFor (&quot;221&quot;)

Winsock1.Close
Else
MsgBox (Str(Winsock1.State))
End If

End Sub
__________________________________________________________

Sub WaitFor(ResponseCode As String)
Start = Timer ' Time event so won't get stuck in loop
While Len(Response) = 0
Tmr = Start - Timer
DoEvents ' Let System keep checking for incoming response **IMPORTANT**
If Tmr > 50 Then ' Time in seconds to wait
MsgBox &quot;SMTP service error, timed out while waiting for response&quot;, 64, MsgTitle
Exit Sub
End If
Wend
While Left(Response, 3) <> ResponseCode
DoEvents
If Tmr > 50 Then
MsgBox &quot;SMTP service error, impromper response code. Code should have been: &quot; + ResponseCode + &quot; Code recieved: &quot; + Response, 64, MsgTitle
Exit Sub
End If
Wend
Response = &quot;&quot; ' Sent response code to blank **IMPORTANT**
End Sub
__________________________________________________________

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Winsock1.GetData Response ' Check for incoming response *IMPORTANT*

End Sub
__________________________________________________________
[/tt]

Don't forget to put a label or textbox called StatusTxt
 

Well, I'll try to write it next week.

I have a lot of work for now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top