require 'net/smtp'
#Senders and Recipients
from_name = 'myname'
from_mail = 'myname@126.com'
to_name = 'yourname'
to_mail = 'yourname@163.com'
#Servers and Authentication
smtp_host = 'smtp.126.com'
smtp_port = 25
smtp_domain = '126.com'
smtp_user = 'myname@126.com'
smtp_pwd = 'mypwd'
#The subject and the message
t = Time.now
subj = 'Sending Email with Ruby'
msg_body = "我的测试Check out the instructions on how to send mail using Ruby.\n"
#The date/time should look something like: Thu, 03 Jan 2006 12:33:22 -0700
msg_date = t.strftime("%a, %d %b %Y %H:%M:%S +0800")
#Compose the message for the email
msg = <
Date: #{msg_date}
From: #{from_name}
To: #{to_name} <#{to_mail}>
Subject: #{subj}
#{msg_body}
END_OF_MESSAGE
Net::SMTP.start(smtp_host, smtp_port, smtp_domain, smtp_user, smtp_pwd, :plain) do |smtp|
smtp.send_message msg, smtp_user, to_mail
end
发送英文邮件正常,不能发送带中文的邮件
msg_body = "我的测试Check out the instructions on how to send mail using Ruby.\n" 这个信件内容不能有中文
提示错误
sendemail:19: invalid multibyte char (US-ASCII)
sendemail:19: invalid multibyte char (US-ASCII)
那位高手能帮忙解决一下
[b]问题补充:[/b]
我把文件转换成utf-8,还是提示有错误
/usr/local/lib/ruby/1.9.1/net/protocol.rb:291: warning: regexp match /.../n against to UTF-8 string
[b]问题补充:[/b]
转成utf-8的编码,邮件可以收到,但是国内的大部分邮箱都是gbk编码的,
到收件箱里面看到的是乱码,