
一、简介
new 方法有两个参数:
server name 默认为 localhost
port number 默认为 25
start 方法有以下参数:
server - SMTP 服务器 IP, 默认为 localhost
port - 端口号,默认为 25
domain - 邮件发送者域名,默认为 ENV["HOSTNAME"]
account - 用户名,默认为 nil
password - 用户密码,默认为nil
authtype - 验证类型,默认为 cram_md5
SMTP 对象实例化方法调用了 sendmail, 参数如下:
source - 一个字符串或数组或每个迭代器在任一时间中返回的任何东西。
sender -一个字符串,出现在 email 的表单字段。
recipients - 一个字符串或字符串数组,表示收件人的地址。
二、示例
- 简单邮件
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’ end
- HTML邮件
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
- 带附件邮件
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
- 多个附件
require ’net/smtp’
filename = "/tmp/test.txt"
# 读取文件并编码为base64格式
filecontent = File.read(filename)
encodedcontent = [filecontent].pack("m") # base64
filename2 = "/tmp/test2.txt"
# 读取文件并编码为base64格式
filecontent2 = File.read(filename2)
encodedcontent2 = [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}
# 定义附件部分2
part4 =<<EOF
Content-Type: multipart/mixed; name=\"#{filename2}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{filename2}"
#{encodedcontent2}
--#{marker}--
EOF
mailtext = part1 + part2 + part3 + part4
# 发送邮件
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