我需要刷新SMTP服务器吗?如果是的话,该怎么办?

时间:2011-06-15 12:15:45

标签: 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'代码的一部分,所以我想知道...... 任何帮助都会被非常赞赏,因为我从未想过发送电子邮件会如此困难:/
附:我使用手机上完全相同的设置发送电子邮件与此SMTP服务器,相同的“smtp.sfr.fr”,相同的ID(整个地址),相同的密码;它的工作原理:我可以通过手机发送邮件 在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

虽然我无法评论您对HaskellNet的使用,但我使用SMTPClient取得了巨大的成功,您可以使用cabal install SMTPClient从hackage中获取。

我已经包含了该软件包的示例,以便您了解使用该库的内容:

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