【SMTP】Linux下C++实现smtp邮件发送

LeonTown 2011-08-16 09:22:57
用linux下C++写的smtp的邮件发送程序,
但是运行时提示连接不上。
代码如下:


#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <string>

using std::string;

int Connect(const string& host)
{
int sock = 0;

struct hostent * remoteHost;
if ((remoteHost = gethostbyname(host.c_str())) == 0)
{
printf("get host failed:%s\n", host.c_str());
return 0;
}

struct sockaddr_in pin;
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_port = htons(25);
pin.sin_addr.s_addr = ((struct in_addr *)(remoteHost->h_addr))->s_addr;

printf("remote host ip:%s, %u\n", remoteHost->h_addr, pin.sin_addr.s_addr);

if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("create socket failed\n");
return 0;
}

if (connect(sock, (const sockaddr*)&pin, sizeof(pin)) == -1)
{
printf("connect failed\n"); // 这里提示连接失败!
return 0;
}

return sock;
}

int main(int argc, const char** argv)
{
int sock = Connect("mail.google.com");
if (sock == 0)
{
return 1;
}
printf("Connect succeed:%d\n", sock);

string fromAddr = "xxx@gmail.com";
string msg;
msg = "mail from:<";
msg.append(fromAddr);
msg.append(">\n");

string toAddr = "xxx@gmail.com";
msg.append("rcpt to:<");
msg.append(toAddr);
msg.append(">\n");

string subject = "ttttt";
string content = "aaaaa";
msg.append("data\n");
msg.append("subject:");
msg.append(subject);
msg.append("\n\n");
msg.append(content);
msg.append("\n.\n");

if (send(sock, msg.c_str(), msg.length(), 0) == -1)
{
printf("send fetching request failed\n");
return 1;
}

char recvBuf[1024];
int l = recv(sock, recvBuf, 1023, 0);
printf("recv len:%d\n", l);
printf("%s\n", recvBuf);

return 0;
}


请教,为什么那个connect连接失败呢?
...全文
703 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
LeonTown 2011-09-20
  • 打赏
  • 举报
回复
经确认是硬件设置的问题。
上述脚本代码没有问题。
结贴
xiehui3651 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 nickowen 的回复:]

楼上

HTTPS是HTTP+SSL
和SMTP协议没点关系
[/Quote]
我的错。
Defonds 2011-08-17
  • 打赏
  • 举报
回复
base on tcp
nickowen 2011-08-17
  • 打赏
  • 举报
回复
楼上

HTTPS是HTTP+SSL
和SMTP协议没点关系
xiehui3651 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 xiehui3651 的回复:]
引用 20 楼 fallening 的回复:
gmail 的连接是加密的,这样做成么?


加密指的是https??如果是的话没有问题
因为https链接的建立也是在tcp连接之上的。
[/Quote]

更准确的说法应该是在http链接之上的
xiehui3651 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 fallening 的回复:]
gmail 的连接是加密的,这样做成么?
[/Quote]

加密指的是https??如果是的话没有问题
因为https链接的建立也是在tcp连接之上的。
luciferisnotsatan 2011-08-16
  • 打赏
  • 举报
回复
失败,报什么错呢?
LeonTown 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 rendao0563 的回复:]

不是有libsmtp?
[/Quote]
怎么用啊?
rendao0563 2011-08-16
  • 打赏
  • 举报
回复
不是有libsmtp?
DylanHxxxx 2011-08-16
  • 打赏
  • 举报
回复
mark 晚上看
LeonTown 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liufang421 的回复:]

smtp.gmail.com
[/Quote]
改成那个之后,还是不行,
还是那里,连接失败。。。
liufang421 2011-08-16
  • 打赏
  • 举报
回复
smtp.gmail.com
LeonTown 2011-08-16
  • 打赏
  • 举报
回复
大侠们,能帮忙看一下吗?
luciferisnotsatan 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 fallening 的回复:]

gmail 的连接是加密的,这样做成么?
[/Quote]
用socket connect上是没问题的。
rendao0563 2011-08-16
  • 打赏
  • 举报
回复


有封弄好的. 直接可以用的类. 好几个文件不好贴.
fallening 2011-08-16
  • 打赏
  • 举报
回复
gmail 的连接是加密的,这样做成么?
LeonTown 2011-08-16
  • 打赏
  • 举报
回复
非常感谢!
我咨询一下硬件。。。
luciferisnotsatan 2011-08-16
  • 打赏
  • 举报
回复
110是time out,telnet也提示超时。
是不是你的网络有问题呀
至善者善之敌 2011-08-16
  • 打赏
  • 举报
回复
我的是WINDOWS下的,没LINUX环境
既然如上面所说,连PIN都PIN不通,看是不是硬件上面有限制
LeonTown 2011-08-16
  • 打赏
  • 举报
回复
您这个是windows下的,
调调linux下的那个吧。。。

[Quote=引用 14 楼 babilife 的回复:]

C/C++ code


int Connect(const string& host)
{
SOCKET sock;// = 0;
struct hostent * remoteHost;


WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
WSAStartup(wVe……
[/Quote]
加载更多回复(8)

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧