In fact, as long as your wayward can, with Telnet can also send e-mail oh. But the cat is not so willful, or use the KISS principle to send e-mail it. This blog post only describes how to send e-mail, but does not involve the receipt of the mail, in the future if there is a chance to open a separate blog introduction.
Ruby sends mail through SMTP 2 kinds of operation means, one is to send directly with NET::SMTP, compare the bottom. If you also want to send an attachment, you need an extra gem:mailfactory, and mailfactory depends on the package mime-types. Even if the mail server connection requires SSL, you also need to require file smtp-tls.rb, and this RB file needs the support of the OpenSSL package! The second method is to directly use the high-level gem package mail, including the ability to add attachments, but the gem also relies on the Mime-types package, this package is designed to describe the mail file format, commonly known as the multi-purpose Internet Mail extension type AH. Each of these methods is described in turn below.
Method One: Use NET::SMTP
The SMTP port number for a normal non-encrypted connection is 25, or 587 or 465 if it is encrypted, depending on the description of the specific mail server. Started with the QQ mail server, but always unstable, then changed to Hotmail, but Hotmail needs SSL connection. The OpenSSL package is required as described above. Trouble began: Rubygems.org with Gem alive, and later will Openssl.gem directly downloaded to the local, with Gem install-l openssl.gem installation, found require or error, need native ext:openssl.so Dynamic Library extension, enter OpenSSL's ext source code directory with make compile prompt error: Ignore OpenSSL broken by Apple, prompted me to use the other OpenSSL library path, I use hair ah! is Mac OS x not supported? However, the system is checked for the installation of OpenSSL:
/private/etc/openssl
/private/etc/openssl/cert.pem
/usr/bin/openssl
/usr/lib/pkgconfig/openssl.pc
/usr/local/cellar/openssl
Dynamic libraries are also present, but not Ruby's c_ext!
[Email protected]: ruby_src$locate libssl.dylib
/applications/xcode6-beta4.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx10.10.sdk/usr /lib/libssl.dylib
/applications/xcode6-beta4.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx10.9.sdk/usr/ Lib/libssl.dylib
/opt/com.bitaxis/lib/libssl.dylib
/opt/local/lib/libssl.dylib
/usr/lib/libssl.dylib
/usr/local/cellar/openssl/1.0.1e/lib/libssl.dylib
God horse situation? Given the TK issue with Ruby (see my other blog post on Ruby using TK under Mac OS x), I suspect that the Ruby version itself does not support native OpenSSL, which I compiled and installed with the ruby-2.1.5 source code that I downloaded in Ruby-lang! Then with RVM downloaded its ruby-2.1.5 version, a try unexpectedly can bird! But then found that the Hotmail.com encrypted connection is still not connected, and then change back to QQ mailbox, with non-encrypted smtp,25 port connection. This time the basic can be sent stably, if the mailbox test of transposition 163 finds more stable, the code is not refactored:
#!/usr/bin/ Ruby#encoding:utf-8require ' net/smtp ' require './smtp-tls.rb ' require ' mailfactory ' #Senders and recipientsfrom_name = ' localhost ' from_mail = ' [email protected] ' to_name = ' ks ' to_mail = ' [email protected] ' #Servers and Authentication#smtp_host = ' smtp.qq.com ' smtp_host = ' smtp.163.com ' smtp_port = #465 587 25#smtp_domain = ' qq.com ' SM Tp_domain = ' Localhost.localdomain ' Smtp_user = "[email protected]" smtp_pwd = "xxxxxxxx" #smtp_user = "[email Protected] "#smtp_pwd = ' xxxxxxxx ' #The subject and the Messaget = TIME.NOWSUBJ = ' 1331 thinkpad Test hopy ' msg_body = ' se nd msg from ruby.\n "#The date/time should look something Like:thu, Jan 2006 12:33:22-0700msg_date = T.strftime ("%a,% D%b%Y%h:%m:%s +0800 ") #Compose the message for the email
#如果使用mailfactory发送则实际用不着msg格式了msg = <<end_of_messagedate: #{msg_date}from: #{from_name} <#{from_mail}> To: #{to_name} <#{to_mail}>subject: #{subj}#{msg_body} end_of_messagemail = MailFactory.newmail.to = To_ Mailmail.from = From_mailmail.subject = Subjmail.text = Msg_bodymail.attach (File.expand_path ("./mail.rb")) #发送附件 #smtp = Net::smtp.new (smtp_host,587) #smtp. Enable_starttls#net::smtp.start (Smtp_host, Smtp_port, Smtp_domain, SMTP_ User, smtp_pwd,:p Lain) do |smtp|net::smtp.start (Smtp_host,smtp_port,smtp_domain, Smtp_user, smtp_pwd,: login) do |SMTP | #smtp. Send_message msg, Smtp_user, To_mail #mail. to = To_mail #puts smtp.methods #smtp. Enable_starttls
Method Two: Use Ruby Gem:mail (not finished)
Ruby used to send Internet mail