suntsh 2009-06-13 16:50 采纳率: 0%
浏览 395
已采纳

如何用ruby发送中文邮件?

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编码的,
到收件箱里面看到的是乱码,

  • 写回答

4条回答 默认 最新

  • Hooopo 2009-06-13 19:40
    关注

    [quote] warning: regexp match /.../n against to UTF-8 string[/quote]
    只是warning没问题的,你看看邮件收到没有

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格