我查看了所有的SMTP ruby-docs,无法弄清楚我出错的地方:
def send(username, password, data, toAddress, fromAddress)
smtp = Net::SMTP.new('my.smtp.host', 25)
smtp.start('thisisunimportant', username, password, "plain") do |sender|
sender.send_message(data, fromAddress, toAddress)
end
end
send(user, pass, rcpt, "Hey!")
Run Code Online (Sandbox Code Playgroud)
出现意外的错误:
/usr/lib/ruby/1.9.1/net/smtp.rb:725:in authenticate': wrong number of arguments (3 for 4) (ArgumentError)
from /usr/lib/ruby/1.9.1/net/smtp.rb:566:indo_start'来自/usr/lib/ruby/1.9.1/net/smtp.rb:531:in start'
from gmx_pop.rb:24:insend'来自gmx_pop.rb: 30:在''
我试过几次踢我的电脑,但问题仍然存在.
这是Net :: SMTP #start call的描述:
http://ruby-doc.org/stdlib-1.9.1/libdoc/net/smtp/rdoc/Net/SMTP.html#method-i-start
该页面提到您可以直接执行SMTP.start来完成所有操作.
看起来你错过了port参数.尝试端口587进行安全身份验证,如果不起作用,请执行端口25.(查看下面提到的教程)
你的电话应该是这样的:
message_body = <<END_OF_EMAIL
From: Your Name <your.name@gmail.com>
To: Other Email <other.email@somewhere.com>
Subject: text message
This is a test message.
END_OF_EMAIL
server = 'smtp.gmail.com'
mail_from_domain = 'gmail.com'
port = 587 # or 25 - double check with your provider
username = 'your.name@gmail.com'
password = 'your_password'
smtp = Net::SMTP.new(server, port)
smtp.enable_starttls_auto
smtp.start(server,username,password, :plain)
smtp.send_message(message_body, fromAddress, toAddress) # see note below!
Run Code Online (Sandbox Code Playgroud)
重要:
检查还:
教程:http://www.java-samples.com/showtutorial.php? tutorialid = 121
Net :: SMTP下的源代码 ~/.rvm/src/ruby-1.9.1*/lib/net/smtp.rb
另一种从Ruby发送电子邮件的方法:
您可以使用Rails中的ActionMailer gem从Ruby发送电子邮件(不带Rails).起初这看起来有些过分,但它更容易,因为您不必使用To:,From:,Subject:,Date:,Message-Id:Headers格式化邮件正文.
# usage:
# include Email
#
# TEXT EMAIL :
# send_text_email( 'sender@somewhere.com', 'sender@somewhere.com,receiver@other.com', 'test subject', 'some body text' )
# HTML EMAIL :
# send_html_email( 'sender@somewhere.com', 'sender@somewhere.com,receiver@other.com', 'test subject', '<html><body><h1>some title</h1>some body text</body></html>' )
require 'action_mailer'
# ActionMailer::Base.sendmail_settings = {
# :address => "Localhost",
# :port => 25,
# :domain => "yourdomain.com"
# }
ActionMailer::Base.smtp_settings = { # if you're using GMail
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => "your-username@gmail.com"
:password => "your-password"
:authentication => "plain",
:enable_starttls_auto => true
}
class SimpleMailer < ActionMailer::Base
def simple_email(the_sender, the_recepients, the_subject, the_body , contenttype = nil)
from the_sender
recipients the_recepients
subject the_subject
content_type contenttype == 'html' ? 'text/html' : 'text/plain'
body the_body
end
end
# see http://guides.rails.info/action_mailer_basics.html
# for explanation of dynamic ActionMailer deliver_* methods.. paragraph 2.2
module Email
# call this with a message body formatted as plain text
#
def send_text_email( sender, recepients, subject, body)
SimpleMailer.deliver_simple_email( sender , recepients , subject , body)
end
# call this with an HTML formatted message body
#
def send_html_email( sender, recepients, subject, body)
SimpleMailer.deliver_simple_email( sender , recepients , subject , body, 'html')
end
endsubject , body, 'html')
end
end
Run Code Online (Sandbox Code Playgroud)
例如,如果您要使用Gmail的SMTP服务器通过Gmail帐户发送电子邮件,则上述代码有效.其他SMTP服务器可能需要其他值:port,:authentication和:enable_starttls_auto,具体取决于SMTP服务器设置
| 归档时间: |
|
| 查看次数: |
3965 次 |
| 最近记录: |