karastojko / mailio

mailio is a cross platform C++ library for MIME format and SMTP, POP3 and IMAP protocols. It is based on standard C++ 17 and Boost library.
Other
374 stars 98 forks source link

Segmentation fault due to mailio::smtps conn(smtp_server, smtp_server_port); #120

Closed UnknownObject000 closed 1 year ago

UnknownObject000 commented 1 year ago

I wrote an SMTP function like this:

    try
    {
        // create mail message
        mailio::message msg;
        //std::unique_ptr<std::ifstream[]> ifs = nullptr;
        msg.from(mailio::mail_address(smtp_sender, smtp_account));
        for (const auto& ele : data.Header.Recivers)
            msg.add_recipient(mailio::mail_address(ele.DisplayName, ele.Address));
        for (const auto& ele : data.Header.CCs)
            msg.add_cc_recipient(mailio::mail_address(ele.DisplayName, ele.Address));
        for (const auto& ele : data.Header.BCCs)
            msg.add_bcc_recipient(mailio::mail_address(ele.DisplayName, ele.Address));
        //msg.subject(AtoUTF8(data.Header.Subject));
        msg.subject(data.Header.Subject);
        msg.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
        msg.content_type(mailio::mime::media_type_t::TEXT, (data.Content.Text.IsHTML ? "html" : "plain"), "gb2312");
        msg.content(data.Content.Text.Text);
        mailio::smtps conn(smtp_server, smtp_server_port);
        conn.authenticate(smtp_account, smtp_password, mailio::smtps::auth_method_t::LOGIN);
        conn.submit(msg);
        smtp_last_error = "No Error";
        }
    catch (mailio::smtp_error& exc)
    {
        smtp_last_error = "MIOSE - ";
        smtp_last_error += exc.what();
        return false;
    }
    catch (mailio::dialog_error& exc)
    {
        smtp_last_error = "MIODE - ";
        smtp_last_error += exc.what();
        return false;
    }

It works fine on Windows 11 x64/Visual Studio 2022, but when I run it on Ubuntu LTS 20.04/g++ 9.4.0, it causes Segmentation fault. GDB Info:

Thread 3 "heartbeat_serve" hit Breakpoint 1, SMTPSender::SendMail (this=0x555555693ec0 <reporter>, root=...) at SMTPSender.cpp:182
182                     mailio::smtps conn(smtp_server, smtp_server_port);
(gdb) c
Continuing.

Thread 3 "heartbeat_serve" received signal SIGSEGV, Segmentation fault.
0x00005555556110e8 in boost::asio::detail::reactive_socket_service_base::close(boost::asio::detail::reactive_socket_service_base::base_implementation_type&, boost::system::error_code&) ()

My compile command: g++ -o heartbeat_server SMTPSender.cpp -pthread -lcrypto -ljsoncpp -lmailio -std=c++17

karastojko commented 1 year ago

Here is an example which is similar to yours:

mailio::message msg;
msg.from(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.add_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.add_cc_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.add_bcc_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.subject("issue120");
msg.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
msg.content_type(mailio::mime::media_type_t::TEXT, "plain", "gb2312");
msg.content("issue120");
smtps conn("smtp.mailio.dev", 465);
conn.authenticate("adresa@mailio.dev", "***", smtps::auth_method_t::LOGIN);
conn.submit(msg);

I do not have any segmentation faults on Ubuntu/Gcc. Can you confirm that this simplified example works in your case? Also take care that you are using the correct port number which requires SSL immediately, not the START TLS port.

UnknownObject000 commented 1 year ago

Here is an example which is similar to yours:

mailio::message msg;
msg.from(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.add_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.add_cc_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.add_bcc_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.subject("issue120");
msg.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
msg.content_type(mailio::mime::media_type_t::TEXT, "plain", "gb2312");
msg.content("issue120");
smtps conn("smtp.mailio.dev", 465);
conn.authenticate("adresa@mailio.dev", "***", smtps::auth_method_t::LOGIN);
conn.submit(msg);

I do not have any segmentation faults on Ubuntu/Gcc. Can you confirm that this simplified example works in your case? Also take care that you are using the correct port number which requires SSL immediately, not the START TLS port.

This example runs fine. I'm using a 163.com mailbox and the same 465 port, it works fine on windows.

karastojko commented 1 year ago

Did you try the port 587 with the start tls option?

UnknownObject000 commented 1 year ago

Did you try the port 587 with the start tls option?

It doesn't work. I have re-tested the example, it still crashs.

UnknownObject000 commented 1 year ago

Here is an example which is similar to yours:

mailio::message msg;
msg.from(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.add_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.add_cc_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.add_bcc_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
msg.subject("issue120");
msg.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
msg.content_type(mailio::mime::media_type_t::TEXT, "plain", "gb2312");
msg.content("issue120");
smtps conn("smtp.mailio.dev", 465);
conn.authenticate("adresa@mailio.dev", "***", smtps::auth_method_t::LOGIN);
conn.submit(msg);

I do not have any segmentation faults on Ubuntu/Gcc. Can you confirm that this simplified example works in your case? Also take care that you are using the correct port number which requires SSL immediately, not the START TLS port.

This example runs fine. I'm using a 163.com mailbox and the same 465 port, it works fine on windows.

This test result is invalidate. I have tested the wrong program by mistake, sorry.

karastojko commented 1 year ago

So, my simplified example crashes too?

UnknownObject000 commented 1 year ago

So, my simplified.example crashes too?

The example reports this error:

terminate called after throwing an instance of 'mailio::dialog_error'
  what():  Server connecting failed.
Aborted

I changed it to my server setup, it crashed with the same Segmentation fault error.

UnknownObject000 commented 1 year ago

Another whield thing is when I created a new program like this

#include <iostream>
#include <mailio/smtp.hpp>
#include <mailio/message.hpp>

int main()
{
    mailio::message msg;
    msg.from(mailio::mail_address("mailio", "xxxxxx@163.com"));
    msg.add_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
    msg.add_cc_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
    msg.add_bcc_recipient(mailio::mail_address("mailio", "adresa@mailio.dev"));
    msg.subject("issue120");
    msg.content_transfer_encoding(mailio::mime::content_transfer_encoding_t::QUOTED_PRINTABLE);
    msg.content_type(mailio::mime::media_type_t::TEXT, "plain", "gb2312");
    msg.content("issue120");
    mailio::smtps conn("smtp.163.com", 465);    
    conn.authenticate("xxxxxx@163.com", "xxxxxx", mailio::smtps::auth_method_t::LOGIN);
    conn.submit(msg);
    return 0;
}

it doesn't crash.

karastojko commented 1 year ago

So, when you use the example with your credentials it works, but after your code changes it does not work?

UnknownObject000 commented 1 year ago

So, when you use the example with your credentials it works, but after your code changes it does not work?

Yes.

karastojko commented 1 year ago

But then it's a problem with your changes, right?