Five Ways to Send Mail

  • Published on
    05-Apr-2018

  • View
    214

  • Download
    0

Embed Size (px)

Transcript

  • 7/31/2019 Five Ways to Send Mail

    1/6

    '**********************************************************'1)Withcdo-fromMsdn'HOWTO:SendMailfromVisualBasicUsingCDO1.xLibrary'**********************************************************OptionExplicit

    PrivateSubCommand1_Click()DimobjSessionAsObjectDimobjMessageAsObjectDimobjRecipientAsObject

    'CreatetheSessionObjectSetobjSession=CreateObject("mapi.session")

    'Logonusingthesessionobject'Specifyavalidprofilenameifyouwantto'AvoidthelogondialogboxobjSession.LogonprofileName:="MSExchangeSettings"

    'AddanewmessageobjecttotheOutBoxSetobjMessage=objSession.Outbox.Messages.Add

    'SetthepropertiesofthemessageobjectobjMessage.subject="Thisisatest."

    objMessage.Text="Thisisthemessagetext."

    'AddarecipientobjecttotheobjMessage.RecipientscollectionSetobjRecipient=objMessage.Recipients.Add

    'SetthepropertiesoftherecipientobjectobjRecipient.Name="JohnDoe"'

  • 7/31/2019 Five Ways to Send Mail

    2/6

    rseomail.Body="Thisistry"rseomail.Importance=0rseomail.Send'toknowifsended,youcantrywiththefollowing'butifyoumispelledtheaddresstosendto,'youwillnotbeinformedIfErr.Number0ThenMsgBox"Anerroroccurred"&vbCrLf&Err.DescriptionErr.ClearElseMsgBox"OK.Giveserverstimetosendandreceivethemessage!"EndIfEndSub

    PrivateSubCommand2_Click()OnErrorResumeNextDimcdoObjectSetcdoObject=CreateObject("CDONTS.NewMail")'ifnotisobject(CDoObject)thenmsgbox"CDoObject=nothing"'foreachpropertyinCDoObject.properties'msgboxproperty.name&"="&property.value'next

    cdoObject.From="Unkleberry@Finn.it"'cdoObject.To="Tom@Sawyer.it"cdoObject.To="King.Kong@Studios.it"cdoObject.Subject="You'reover!"cdoObject.Body="Justkidding..."cdoObject.Send'CDoObject.Send"Domald@Duck.it","Winnie.ThePoh@Disney.it","Hello","world"

    IfErr.Number0ThenMsgBoxErr.Number&"."&Err.DescriptionErr.ClearElseMsgBox"OK"'thismeanseverithingwentonok,'butyouwillnotseethepostimmediately:'giveservertimestosendandtherecipients'toreceive...EndIfEndSub

    '************************************************************'3)Winsockexample

    'codefrom:heinlein@execpc.com'*************************************************************'youMUSTputtheWinsock1controlonyourform

    DimResponseAsString,ReplyAsInteger,DateNowAsStringDimfirstAsString,SecondAsString,ThirdAsStringDimFourthAsString,FifthAsString,SixthAsStringDimSeventhAsString,EighthAsString

  • 7/31/2019 Five Ways to Send Mail

    3/6

    DimStartAsSingle,TmrAsSingle

    SubSendEmail(MailServerNameAsString,FromNameAsString,FromEmailAddressAsString,ToNameAsString,ToEmailAddressAsString,EmailSubjectAsString,EmailBodyOfMessageAsString)Winsock1.LocalPort=0'Mustsetlocalportto0(Zero)oryoucanonlysend1e-mailpreprogramstartIfWinsock1.State=sckClosedThen'ChecktoseeifsocetisclosedDateNow=Format(Date,"Ddd")&","&Format(Date,"ddMmmYYYY")&""&Format(Time,"hh:mm:ss")&""&"-0600"first="mailfrom:"+Chr(32)+FromEmailAddress+vbCrLf'Getwho'ssendingE-MailaddressSecond="rcptto:"+Chr(32)+ToEmailAddress+vbCrLf'GetwhomailisgoingtoThird="Date:"+Chr(32)+DateNow+vbCrLf'DatewhenbeingsentFourth="From:"+Chr(32)+FromName+vbCrLf'Who'sSendingFifth="To:"+Chr(32)+ToNametxt+vbCrLf'WhoitgoingtoSixth="Subject:"+Chr(32)+EmailSubject+vbCrLf'SubjectofE-MailSeventh=EmailBodyOfMessage+vbCrLf'E-mailmessagebodyNinth="mousemailer"+vbCrLf'Whatprogramsentthee-mail,customizeth

    isEighth=Fourth+Third+Ninth+Fifth+Sixth'CombineforproperSMTPsending

    Winsock1.Protocol=sckTCPProtocol'SetprotocolforsendingWinsock1.RemoteHost=MailServerName'SettheserveraddressWinsock1.RemotePort=25'SettheSMTPPortWinsock1.Connect'StartconnectionWaitFor("220")StatusTxt.Caption="Connecting...."StatusTxt.Refresh

    Winsock1.SendData("HELOworldcomputers.com"+vbCrLf)

    WaitFor("250")

    StatusTxt.Caption="Connected"StatusTxt.Refresh

    Winsock1.SendData(first)

    StatusTxt.Caption="SendingMessage"StatusTxt.Refresh

    WaitFor("250")

    Winsock1.SendData(Second)

    WaitFor("250")

    Winsock1.SendData("data"+vbCrLf)WaitFor("354")

  • 7/31/2019 Five Ways to Send Mail

    4/6

    Winsock1.SendData(Eighth+vbCrLf)Winsock1.SendData(Seventh+vbCrLf)Winsock1.SendData("."+vbCrLf)

    WaitFor("250")

    Winsock1.SendData("quit"+vbCrLf)StatusTxt.Caption="Disconnecting"StatusTxt.Refresh

    WaitFor("221")

    Winsock1.CloseElseMsgBox(Str(Winsock1.State))EndIfEndSubSubWaitFor(ResponseCodeAsString)Start=Timer'Timeeventsowon'tgetstuckinloopWhileLen(Response)=0Tmr=Start-Timer

    DoEvents'LetSystemkeepcheckingforincomingresponse**IMPORTANT**IfTmr>50Then'TimeinsecondstowaitMsgBox"SMTPserviceerror,timedoutwhilewaitingforresponse",64,MsgTitleExitSubEndIfWendWhileLeft(Response,3)ResponseCodeDoEventsIfTmr>50ThenMsgBox"SMTPserviceerror,impromperresponsecode.Codeshouldhavebeen:"+ResponseCode+"Coderecieved:"+Response,64,MsgTitleExitSub

    EndIfWendResponse=""'Sentresponsecodetoblank**IMPORTANT**EndSub

    PrivateSubCommand1_Click()SendEmailtxtEmailServer.Text,txtFromName.Text,txtFromEmailAddress.Text,txtToEmailAddress.Text,txtToEmailAddress.Text,txtEmailSubject.Text,txtEmailBodyOfMessage.Text'MsgBox("MailSent")StatusTxt.Caption="MailSent"StatusTxt.Refresh

    BeepCloseEndSub

    PrivateSubCommand2_Click()EndEndSub

  • 7/31/2019 Five Ways to Send Mail

    5/6

    PrivateSubWinsock1_DataArrival(ByValbytesTotalAsLong)

    Winsock1.GetDataResponse'Checkforincomingresponse*IMPORTANT*

    EndSub

    '************************************************************'4)Outlookexample'codefrom:me'*************************************************************'needareferencetoMIcrosoftOutlook(highetsnumber)ObjectlibraryDimobjOutlookAsOutlook.ApplicationDimobjOutlookMsgAsOutlook.MailItemDimtheNamespaceAsOutlook.NameSpaceDimmyRecipientAsOutlook.RecipientSetobjOutlook=NewOutlook.ApplicationSettheNamespace=objOutlook.GetNamespace("MAPI")theNamespace.Logon'hereyoucouldaddyourprofileSetobjOutlookMsg=objOutlook.CreateItem(olMailItem)'*****puttheemailaddressyouneed

    objOutlookMsg.To="Xyz@yahoo.IT"objOutlookMsg.Subject="hifromme"objOutlookMsg.Body="NIcetosendmessageswithOutLook2000!"objOutlookMsg.Importance=olImportanceHighSetmyRecipient=objOutlookMsg.Recipients.Add("SMTP:"&objOutlookMsg.To)myRecipient.ResolveobjOutlookMsg.Attachments.Add"c:\theattach.txt",,,"theattach.txt"objOutlookMsg.SendSetmyRecipient=NothingSetobjOutlookMsg=Nothing

    'theNamespace.SyncObjects.Item(1).Start'theNamespace.SyncObjects.Item(1).StoptheNamespace.LogoffSettheNamespace=NothingobjOutlook.QuitSetobjOutlook=Nothing

    '************************************************************************'5)Mapi:examplefrom:IouriBoutchkine'************************************************************************Youdon'tneedMicrosoftExchangeserveronyourlocalmachine.OneoftheMAPIenabledprogram(likeEudora)mustbeonyourPC

    'makesurethatonthedefaulte-mailuseofMAPIserverisallowed

    OptionExplicit'2controlsMicrosoftMAPIControl6.0->MAPISessionandMAPIMessages

    PrivateSubCommand1_Click()DimmsgAsString

    Screen.MousePointer=vbHourglass

  • 7/31/2019 Five Ways to Send Mail

    6/6

    msg="Thisisatestemail"

    WithMe.MAPILogOn.SignOn'usecurrentuser

    DoWhile.MAPILogOn.SessionID=0DoEvents'needtowaituntilthenewsessioniscreatedLoop

    CallSendToEmail("theaddress@hotmail.com",msg)

    .MAPILogOn.SignOffEndWith

    Screen.MousePointer=vbNormal

    EndSub

    PrivateSubSendToEmail(ByValEmailAsString,ByValmsgAsString)WithMAPIMessages1'createanewmessageandaddressit.SessionID=MAPILogOn.SessionID.Compose.RecipDisplayName=Email

    .AddressResolveUI=True.RecipAddress="smtp:"&Email

    .MsgSubject="VBGENERATEDE-MAIL"

    .SendFalseEndWithEndSub