nsc*_*hoe 5 email haskell smtp
我一直在努力在Haskell程序中发送电子邮件一段时间,试图使用HaskellMime库或类似的东西,但失败了.
我最近安装了HaskellNet并尝试使用Haskellnet.SMTP模块.我尝试使用'sendMail'命令发送电子邮件并获取"用户错误(sendMail错误)".我想这是因为我使用的SMTP服务器需要身份验证.
我看了一下'sendMail'源代码并最终编写了这个简单的主要内容:http://hpaste.org/47841
我检查了每个'sendCommand'命令,在AUTH命令之后我从SMTP服务器获得了"Auth success" ,以及来自其他命令的250个代码,正如'sendMail'源代码中所期望的那样.
问题是我的邮箱里没有邮件,所以我做错了什么?我唯一能想到的是,邮件是在SMTP传出列表中排队的地方,我需要刷新SMTP服务器,但这不是'sendMail'代码的一部分,所以我想...任何帮助都会可以极大的赞赏,因为我从来没有想到会是这么难发送电子邮件:/
PS我用我的手机上完全相同的设置与此SMTP服务器,同样的"smtp.sfr.fr",相同的ID(整个发送电子邮件地址),相同的密码; 它的工作原理:我可以通过手机发送邮件.
在此先感谢您的帮助.
虽然我无法评论您对 HaskellNet 的使用,但我使用SMTPClient取得了巨大成功,您可以使用cabal install SMTPClient
.
我提供了该包的示例,让您了解如何使用该库:
import Network.SMTP.ClientSession
import Network.SMTP.Client
import Network.Socket
import System.Time
import System.IO
import Data.Bits
import Data.IORef
myDomain = "example.com"
smtpHost = "hubert.blacksapphire.com" -- <-- Your SMTP server here
-- This will send the author an email. I don't mind!
main = do
now <- getClockTime
nowCT <- toCalendarTime now
let message = Message [
From [NameAddr (Just "Mr. Nobody") "nobody@example.com"],
To [NameAddr (Just "Stephen Blackheath") "unprintable.distances.stephen@blacksapphire.com"],
Subject "I'm using SMTPClient!",
Date nowCT
]
("Dear Sir,\n"++
"It has come to my attention that this is an email.\n"++
"Yours sincerely,\n"++
"Mr. Nobody\n")
addrs <- getAddrInfo Nothing (Just smtpHost) Nothing
let SockAddrInet _ hostAddr = addrAddress (addrs !! 0)
sockAddr = SockAddrInet (fromIntegral 25) hostAddr
putStrLn $ "connecting to "++show sockAddr
sentRef <- newIORef []
sendSMTP' (hPutStrLn stderr) (Just sentRef) myDomain
sockAddr [message]
statuses <- readIORef sentRef
-- If no exception was caught, statuses is guaranteed to be
-- the same length as the list of input messages, therefore head won't fail here.
case head statuses of
Nothing -> putStrLn "Message successfully sent"
Just status -> putStrLn $ "Message send failed with status "++show status
Run Code Online (Sandbox Code Playgroud)