【问题标题】:CSmtp won't send an e-mail attachment. But without the attachment it works fine. (c++)CSmtp 不会发送电子邮件附件。但没有附件它工作正常。 (c++)
【发布时间】:2014-03-15 16:44:50
【问题描述】:

我在使用 CSmtp class 发送附件时遇到问题。
这是该链接上的代码:

int SendMail()
{
  bool bError = false;

  try
  {
    CSmtp mail;

#define test_gmail_tls

#if defined(test_gmail_tls)
    mail.SetSMTPServer("smtp.gmail.com",587);
    mail.SetSecurityType(USE_TLS);
#elif defined(test_gmail_ssl)
    mail.SetSMTPServer("smtp.gmail.com",465);
    mail.SetSecurityType(USE_SSL);
#elif defined(test_hotmail_TLS)
    mail.SetSMTPServer("smtp.live.com",25);
    mail.SetSecurityType(USE_TLS);
#elif defined(test_aol_tls)
    mail.SetSMTPServer("smtp.aol.com",587);
    mail.SetSecurityType(USE_TLS);
#elif defined(test_yahoo_ssl)
    mail.SetSMTPServer("plus.smtp.mail.yahoo.com",465);
    mail.SetSecurityType(USE_SSL);
#endif

    mail.SetLogin("email@email.com");
    mail.SetPassword("password");
    mail.SetSenderName("");
    mail.SetSenderMail("email@email.com");
    mail.SetReplyTo("");
    mail.SetSubject("Subject");
    mail.AddRecipient("email@email.com");
    mail.SetXPriority(XPRIORITY_NORMAL);
    mail.SetXMailer("The Bat! (v3.02) Professional");
    mail.AddMsgLine("Hello,");
    mail.AddMsgLine("you have been successfully registered!");
    mail.AddMsgLine(" ");
    mail.AddMsgLine("Username: ");
    mail.AddMsgLine("Password: ");
    mail.AddMsgLine(" ");
    mail.AddMsgLine("See ya!");

    mail.AddAttachment("C:\\Users\\Jenda\\AppData\\Roaming\\text.dat");
    mail.Send();
}
catch(ECSmtp e)
{
    std::cout << "Error: " << e.GetErrorText().c_str() << ".\n";
    bError = true;
}
if(!bError)
    std::cout << "Registration E-Mail was sent on given address.\n";
return 0;
}

当我评论附件行时,它成功发送了电子邮件。 但是当我尝试发送该附件时,它似乎只是停在那里并且什么都不做 - 它不会返回任何错误或任何东西。它什么都不做(它正在响应——根据任务管理器,你知道的)。

此外,还有一个次要问题:您看到附件路径 (C:\Users\Jenda\AppData\Roaming\text.dat) 了吗?该程序如何获取有关用户(名称)的信息,以及我如何将其添加到路径中以便它在每台计算机上运行。 C:\Users\ WINDOWSUSERNAME \...

就是这样,感谢您的所有回复和想法。

附:我正在使用 Windows7 32 位和 Visual c++ Express 2010。

【问题讨论】:

    标签: c++ email smtp email-attachments visual-c++-2010-express


    【解决方案1】:

    对于第一个问题,我相信您指的是this 代码。

    可能的问题:

    一)

    在 CSmtp.cpp 中:

    hFile = fopen(FileName.c_str(), "rb");
    

    应该是(你也应该考虑 fopen_s):

    hFile = fopen(Attachments[FileId].c_str(), "rb");
    

    B)

    在头文件 CSmtp.h 中有一行指定邮件的最大大小。您的附件可能大于 5MB。将其更改为 25MB:

    #define MSG_SIZE_IN_MB 5  // the maximum size of the 
                              // message with all attachments
    

    C)

    代码中有很多 Windows/Linux 特定的部分。 一个这样的例子是:

    pos = Attachments[FileId].find_last_of("\\");
    

    因此,如果您在 Windows 中,附件的路径需要包含“\\”而不是“/”。更好的方法是从系统中分离出来。简而言之,看看您是否正确定义了路径(例如:“c:\\test3.txt”)。

    D)

    我强烈建议您在 main.cpp 的末尾添加该行(以便能够看到系统消息):

    Sleep(4000);
    

    对于第二个问题,您可以执行以下操作(另请参阅here):

    #include <cassert>
    #include <fstream>
    #include <string>
    #include <Windows.h>
    
    std::string getPath(void){
        //Get local dir
        TCHAR szBuf[MAX_PATH] = { 0 };
        ::GetEnvironmentVariable("USERPROFILE", szBuf, MAX_PATH);
        std::string path = szBuf;
        path += "\\AppData\\Roaming\\text.dat";
        return path;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      相关资源
      最近更新 更多