使用SMTP设置Ruby gem'Mail'

时间:2022-10-21 18:10:47

So I am testing out the Ruby gem 'Mail'. Below is my code. According to the sysadmin, there is no authentication necessary to send out by SMTP and the server is allowed to send by SMTP. If that makes sense.

所以我正在测试Ruby gem'Mail'。以下是我的代码。根据sysadmin,没有必要通过SMTP发送身份验证,并且允许服务器通过SMTP发送。如果这是有道理的。

I have used the 'net/smtp' which worked fine but I can't seem to get this 'Mail' gem to work for me. It's not giving me any errors so I'm not sure what the issue is. I am a complete ruby noob at this.

我使用了'net / smtp',它运行良好,但我似乎无法让这个'Mail'宝石为我工作。它没有给我任何错误,所以我不确定是什么问题。我是一个完整的红宝石菜鸟。

#testing out mailer
require 'mail'
Mail.defaults do
    delivery_method :smtp,  {
    :address => '10.18.34.18',
    :port => '25',
    :user_name => 'anonymous',
    :password => 'anonymous',
    :authentication => 'plain',
    :enable_starttls_auto => true}
end

mail = Mail.new do
    from 'server@company.com'
    to 'cvu@company.com'
    subject 'This is a test email'
    body File.read('test.txt')
end

mail.to_s

2 个解决方案

#1


1  

Try calling mail.deliver!, calling to_s just returns you a string representation of the object.

尝试调用mail.deliver !,调用to_s只返回对象的字符串表示。

Alternatively you can call deliver with a block instead of calling new, e.g.

或者,您可以使用块调用传递,而不是调用new,例如

mail = Mail.deliver do
    from 'server@company.com'
    to 'cvu@company.com'
    subject 'This is a test email'
    body File.read('test.txt')
end

#2


0  

In addition to what struthersneil stated, I had to remove the authentication like so:

除了struthersneil所说的,我还必须删除身份验证,如下所示:

require 'mail'
Mail.defaults do
    delivery_method :smtp,  {
    :address => '10.18.34.18',
    :port => '25',
    :enable_starttls_auto => true}
end

#1


1  

Try calling mail.deliver!, calling to_s just returns you a string representation of the object.

尝试调用mail.deliver !,调用to_s只返回对象的字符串表示。

Alternatively you can call deliver with a block instead of calling new, e.g.

或者,您可以使用块调用传递,而不是调用new,例如

mail = Mail.deliver do
    from 'server@company.com'
    to 'cvu@company.com'
    subject 'This is a test email'
    body File.read('test.txt')
end

#2


0  

In addition to what struthersneil stated, I had to remove the authentication like so:

除了struthersneil所说的,我还必须删除身份验证,如下所示:

require 'mail'
Mail.defaults do
    delivery_method :smtp,  {
    :address => '10.18.34.18',
    :port => '25',
    :enable_starttls_auto => true}
end