How to send an email from one Gmail account to another one using a batch file or script?

前端 未结 5 1835
北海茫月
北海茫月 2020-12-18 17:25

I need a tiny Windows script to send a 1 line email to Gmail accounts. I have tried many utilities that claim to do this such as BLAT, but none work. The script will be exec

相关标签:
5条回答
  • 2020-12-18 18:00

    Have a look at this script on perlmonks which details IMAP access on a GMail account. The post covers everything you need to login into a GMail account through Perl.

    Alternatively you could try the Mail::Webmail::Gmail module in CPAN. From the looks of it the module lets you skip most of the intricate details concerning connecting and authenticating with the mail server leaving you with something as simple as -

    my $gmail = Mail::Webmail::Gmail->new( username => 'username', password => 'password', );
    $gmail->send_message( to => 'user@domain.com', subject => 'Test Message', msgbody => 'This is a test.' );
    

    There's also Email::Send::Gmail in case you need to 'only' send emails from a Gmail account.

    0 讨论(0)
  • 2020-12-18 18:02

    In Linux (I'm not sure what environment you're on) you can use mail:

    some_command| mail foo@gmail.com bar@gmail.com -s "subject"
    
    0 讨论(0)
  • 2020-12-18 18:06

    Blat lets you send e-mails directly from batch files:

    blat.exe - -f from@example.com -to to@gmail.com -s Subject -body "Text body" ^
      -server smtp.example.com:25 -u username -pw password
    

    But it seems that Blat doesn't support SSL, so to make it work with the Gmail you need an additional tool called Stunnel (see here and here).

    Anyway, you should be able to send an e-mail via GMail from VBScript using the Collaboration Data Objects (CDO) COM API:

    Const schema   = "http://schemas.microsoft.com/cdo/configuration/"
    Const cdoBasic = 1
    Const cdoSendUsingPort = 2
    Dim oMsg, oConf
    
    ' E-mail properties
    Set oMsg      = CreateObject("CDO.Message")
    oMsg.From     = "from@gmail.com"  ' or "Sender Name <from@gmail.com>"
    oMsg.To       = "to@gmail.com"    ' or "Recipient Name <to@gmail.com>"
    oMsg.Subject  = "Subject"
    oMsg.TextBody = "Text body"
    
    ' GMail SMTP server configuration and authentication info
    Set oConf = oMsg.Configuration
    oConf.Fields(schema & "smtpserver")       = "smtp.gmail.com"
    oConf.Fields(schema & "smtpserverport")   = 465
    oConf.Fields(schema & "sendusing")        = cdoSendUsingPort
    oConf.Fields(schema & "smtpauthenticate") = cdoBasic
    oConf.Fields(schema & "smtpusessl")       = True
    oConf.Fields(schema & "sendusername")     = "from@gmail.com"
    oConf.Fields(schema & "sendpassword")     = "sender_password"
    oConf.Fields.Update
    
    oMsg.Send
    

    Edit: Added the lacking sendusing parameter so it should work fine now.

    See here for more CDO examples.

    0 讨论(0)
  • 2020-12-18 18:15
    #!c:/Python/python.exe -u
    import libgmail
    ga = libgmail.GmailAccount("username@gmail.com", "password")
    ga.login()
    msg=libgmail.GmailComposedMessage("friend@gmail.com", "SubjectHere", "BodyHere")
    ga.sendMessage(msg)
    

    That should run on Windows using python. Make sure you change the shebang at the top to point to your python installation. Other than that, just enter your name and password, along with the email you want to send.

    0 讨论(0)
  • 2020-12-18 18:21

    If you need just to send to GMail using some SMTP, use MIME::Lite Perl module in SMTP mode - I use it to send notifications to my GMail account.

    0 讨论(0)
提交回复
热议问题