HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
SMTP (Simple Mail Transfer Protocol) that is Simple Mail Transfer Protocol, which is a set of rules for transmitting messages from source to destination, which it has to control the way the letters of transit.
Ruby provides a Net :: SMTP to send mail, and provides two methods and new start:
new method has two parameters:
start method has the following parameters:
SMTP object instantiation method calls sendmail, the following parameters:
The following provides a simple Ruby script to send mail:
require 'net/smtp' message = <<MESSAGE_END From: Private Person <me@fromdomain.com> To: A Test User <test@todomain.com> Subject: SMTP e-mail test This is a test e-mail message. MESSAGE_END Net::SMTP.start('localhost') do |smtp| smtp.send_message message, 'me@fromdomain.com', 'test@todomain.com' endIn the above example, you have set up a basic e-mail message, pay attention to the correct title format. An e-mail to be From, To, and Subject, between text and header information requires a blank line.
Use Net :: SMTP to connect to the SMTP server on the local machine to send messages using send_message process, process parameters for the sender of the message with the recipient mail.
If you are not running on the machine's SMTP server, you can use the Net :: SMTP communication with remote SMTP server. If you use webmail services (such as Hotmail or Yahoo Mail), your email provider will provide details of the sending mail server for you:
Net::SMTP.start('mail.your-domain.com')
The above code will connect host mail.your-domain.com, port number 25 of the mail server, if you need to fill in the user name and password, the code is as follows:
Net::SMTP.start('mail.your-domain.com', 25, 'localhost', 'username', 'password', :plain)
The above examples use a user name and password to connect to the specified host mail.your-domain.com, port number 25 of the mail server.
Net :: SMTP also provides support for sending messages in HTML format.
When sending e-mail you can set the MIME version, document type, character set to send messages in HTML format.
The following examples are used to send messages in HTML format:
require 'net/smtp' message = <<MESSAGE_END From: Private Person <me@fromdomain.com> To: A Test User <test@todomain.com> MIME-Version: 1.0 Content-type: text/html Subject: SMTP e-mail test This is an e-mail message to be sent in HTML format <b>This is HTML message.</b> <h1>This is headline.</h1> MESSAGE_END Net::SMTP.start('localhost') do |smtp| smtp.send_message message, 'me@fromdomain.com', 'test@todomain.com' end
If you need to send a mixed content of e-mail, you need to set the Content-type is multipart / mixed. So you can add an attachment in the message content.
Accessory before the transfer requires the use pack ( "m") function converts its contents into base64 format.
The following example will send attachments as /tmp/test.txt e-mail:
require 'net/smtp' filename = "/tmp/test.txt" # 读取文件并编码为base64格式 filecontent = File.read(filename) encodedcontent = [filecontent].pack("m") # base64 marker = "AUNIQUEMARKER" body =<<EOF This is a test email to send an attachement. EOF # 定义主要的头部信息 part1 =<<EOF From: Private Person <me@fromdomain.net> To: A Test User <test@todmain.com> Subject: Sending Attachement MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=#{marker} --#{marker} EOF # 定义消息动作 part2 =<<EOF Content-Type: text/plain Content-Transfer-Encoding:8bit #{body} --#{marker} EOF # 定义附件部分 part3 =<<EOF Content-Type: multipart/mixed; name=\"#{filename}\" Content-Transfer-Encoding:base64 Content-Disposition: attachment; filename="#{filename}" #{encodedcontent} --#{marker}-- EOF mailtext = part1 + part2 + part3 # 发送邮件 begin Net::SMTP.start('localhost') do |smtp| smtp.sendmail(mailtext, 'me@fromdomain.net', ['test@todmain.com']) end rescue Exception => e print "Exception occured: " + e end
Note: You can specify the address of a plurality of transmission, but requires the use of commas.