# How to send the email by Ruby script?

**URL:** <https://www.ruby-forum.com/t/how-to-send-the-email-by-ruby-script/169595>\
**Category:** Ruby\
**Created:** [June 5, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/how-to-send-the-email-by-ruby-script/169595 "2009-06-05T18:51:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chenpassion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@chenpassion](https://www.ruby-forum.com/u/chenpassion)\
**Post date:** [June 5, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/how-to-send-the-email-by-ruby-script/169595/1 "2009-06-05T18:51:31Z")

</div>

Hi, Guys,  
I can read my [sina.com](http://sina.com) email by the following code:  
require ‘net/pop’  
Net::POP3.foreach(‘_[pop3.sina.com.cn](http://pop3.sina.com.cn)_’, 110,  
‘removed\_email\_address@domain.invalid’, ‘my\_password’ ) do |email|  
hdr = email.header  
if hdr =~ /\d+/  
language = hdr.to\_s  
else  
language = “INVALID”  
end  
puts language  
end

I would like to send email by _[smtp.sina.com.cn](http://smtp.sina.com.cn)_, there is code on the  
net:

Net::SMTP.start(‘[smtp.sina.com.cn](http://smtp.sina.com.cn)’,25) do |smtp|  
smtp.send\_message “hello”, ‘removed\_email\_address@domain.invalid’, [‘removed\_email\_address@domain.invalid’]  
end  
But I get error message:  
_e:/ruby/lib/ruby/1.8/net/smtp.rb:680:in `check\_response’: 530  
Authentication required (Net::SMTPUnknownError)_

So, how can make it I can send the email by the ruby code?

Any suggestion would be quite appreciated.

Thanks.  
Wesley C…

---

<div class="post-metadata">

**Author:** ![chenpassion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@chenpassion](https://www.ruby-forum.com/u/chenpassion)\
**Post date:** [June 5, 2009, 6:55pm UTC](https://www.ruby-forum.com/t/how-to-send-the-email-by-ruby-script/169595/2 "2009-06-05T18:55:03Z")

</div>

Wesley C. wrote:

> end  
> Authentication required (Net::SMTPUnknownError)\*
> 
> So, how can make it I can send the email by the ruby code?
> 
> Any suggestion would be quite appreciated.

I have a detailed example here:  
[http://notepad.onghu.com/2007/3/26/sending-email-with-ruby](http://notepad.onghu.com/2007/3/26/sending-email-with-ruby)  
not sure if it helps.

Cheers,  
Mohit.  
6/6/2009 | 12:54 AM.

---

<div class="post-metadata">

**Author:** ![chenpassion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@chenpassion](https://www.ruby-forum.com/u/chenpassion)\
**Post date:** [June 5, 2009, 7:10pm UTC](https://www.ruby-forum.com/t/how-to-send-the-email-by-ruby-script/169595/3 "2009-06-05T19:10:53Z")

</div>

Hi, Mohit,  
Thank you very much, the code works great!

Thanks.  
Wesley C…

---

<div class="post-metadata">

**Author:** ![chenpassion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@chenpassion](https://www.ruby-forum.com/u/chenpassion)\
**Post date:** [June 5, 2009, 7:34pm UTC](https://www.ruby-forum.com/t/how-to-send-the-email-by-ruby-script/169595/4 "2009-06-05T19:34:26Z")

</div>

But do you know how to send the email with an attachment?

Thanks.  
Wesley C…

---

<div class="post-metadata">

**Author:** ![chenpassion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@chenpassion](https://www.ruby-forum.com/u/chenpassion)\
**Post date:** [June 5, 2009, 8:10pm UTC](https://www.ruby-forum.com/t/how-to-send-the-email-by-ruby-script/169595/5 "2009-06-05T20:10:18Z")

</div>

Wesley C. wrote:

> But do you know how to send the email with an attachment?
> 
> Thanks.  
> Wesley C…

I haven’t needed to, but a Google search should find you quite a few  
hits.

Cheers,  
Mohit.  
6/6/2009 | 2:07 AM.
