我的脚本为用户列表发送电子邮件。我需要多线程,因为 1 个地址中的错误会使所有应用程序崩溃。这是我的实现:
require 'thread'
<...>
lock = Mutex.new
lock.synchronize {
@model.certs.each{ |user|
@threads << Thread.new(user) { |data|
to = "#{data['name']} #{data['surname']} <#{data['email']}>"
subject = '<Subject>'
body = "<Body>"
view = View.new()
view.to = to
view.body = body
view.subject = subject
view.attachment = ''
view.sendMessage()
@model.sended(data['email'])
}
}
}
@threads.each { |t|
begin
t.join
rescue => err
$log.fatal(err)
end
}
view.sendMessage
包括:
require 'mail'
require 'net/smtp'
<...>
smtp = Net::SMTP.start(@mailserver)
mail = Mail.new()
mail.from = @from
mail.to = @to
mail.subject = @subject
mail.body = @body
if !@attachment.empty?
mail.add_file @attachment
end
mail.delivery_method :smtp_connection, {
:connection => smtp
}
mail.deliver
有时它会出现以下错误:
uninitialized constant Mail::Field::FromField (NameError)
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/field.rb:189:in `new_field'
或者
uninitialized constant Mail::CommonAddress::AddressList (NameError)
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/fields/common/common_address.rb:9:in `parse'
我不知道,如何解决。互斥量添加没有结果。