简体   繁体   English

如何使用C ++发送电子邮件

[英]How to send an email with C++

I'm making a program that supports email functionality within a web browser. 我正在制作一个支持Web浏览器中的电子邮件功能的程序。 I want it to be able to email people and I'm trying to find out how I can accomplish this using C++. 我希望它能够向人们发送电子邮件,并且我正在尝试找出如何使用C ++完成此任务。 Could someone please help? 有人可以帮忙吗?

You will need to install an SMTP server (supposing you are on windows), or use sendmail if on linux. 您将需要安装SMTP服务器(假设您在Windows上),或者在Linux上使用sendmail。 If you do not wish to install an SMTP server, you can use an external one as specified in some examples . 如果您不想安装SMTP服务器,则可以使用某些示例中指定的外部服务器。

I've tried sending email with CURL and with libesmtp . 我尝试使用CURLlibesmtp发送电子邮件。
Both are good, although I like CURL more because it can work asynchronously. 两者都很好,尽管我更喜欢CURL,因为它可以异步工作。
Also, I have a header-only curl wrapper . 另外,我有一个仅标头的curl包装器 Sending email with it is as simple as: 发送电子邮件很简单:

long rc = Curl().send ("Subject: subject\r\n\r\n" "text\r\n") .smtp ("from", "to") .go().status();
if (rc != 250) std::cerr << "Error sending email: " << rc << std::endl;

如果您使用的是Windows,那么我使用Dundas Ultimate TCP / IP的效果很好。

Sockets are your best bet. 套接字是您最好的选择。 Your best resource for sockets in C and C++ is: http://beej.us/guide/bgnet/ 使用C和C ++ 编写套接字的最佳资源是: http : //beej.us/guide/bgnet/

For an implementation try here: http://www.codeguru.com/forum/showthread.php?t=300530 对于实现,请尝试在这里: http : //www.codeguru.com/forum/showthread.php?t=300530

All you have to do is use a library that lets you use an SMTP server (Simple mail transfer protocol). 您所要做的就是使用一个使您可以使用SMTP服务器(简单邮件传输协议)的库。 What platform are you developing this for? 您正在为此平台开发?

If all else fails, you could always write the mail to a file and try using system(3) to invoke mail(1) . 如果所有其他方法均失败,则可以始终将邮件写入文件,然后尝试使用system(3)调用mail(1)

$ mail -s 'OMG!' $ mail -s'OMG!' mybff@example.com < mymailfile mybff@example.com <mymailfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM