登陆账号已经是邮箱全名,端口号25,服务器域名是smtp.mxhichina.com,还是有问题。。。
SMTP登陆阿里云企业免费邮箱端口25或者80,不能使用auth login的验证方式,只能使用auth plain的验证方式,贴段我成功发送邮件的代码,CreatSocket就不贴了
bool CSendMail::PlainSend(SOCKET &sock)
{
memset(send_buff_, 0, sizeof(send_buff_));
sprintf_s(send_buff_, "HELO %s\r\n", mail_info_.mail_server_ip_addr);
send(sock, send_buff_, strlen(send_buff_), 0); // 开始会话
memset(receive_buff_, 0, sizeof(receive_buff_));
recv(sock, receive_buff_, 1024, 0);
if (receive_buff_[0] != '2' || receive_buff_[1] != '5' || receive_buff_[2] != '0') return false;
const char* user = mail_info_.mailbox_user_name;
const char* pass = mail_info_.mailbox_user_pwd;
char outstr[1024] = {};
char instr[1024] = {};
strcpy(instr, user);
strcpy(instr + strlen(user) + 1, user);
strcpy(instr + strlen(user) * 2 + 2, pass);
Char2Base64(outstr, instr, strlen(user) * 2 + 2 + strlen(pass));//账户名和密码必须使用user\0user\0pass形式再encode
memset(send_buff_, 0, sizeof(send_buff_));
sprintf_s(send_buff_, "AUTH PLAIN %s\r\n", outstr);//阿里云企业邮只能用AUTH PLAIN,不能使用AUTH LOGIN,163邮都可以
send(sock, send_buff_, strlen(send_buff_), 0);
memset(receive_buff_, 0, sizeof(receive_buff_));
recv(sock, receive_buff_, 1024, 0);
if (receive_buff_[0] != '2' || receive_buff_[1] != '3' || receive_buff_[2] != '5')return false;// 登录成功
memset(send_buff_, 0, sizeof(send_buff_));
sprintf_s(send_buff_, "MAIL FROM:<%s>\r\n", mail_info_.mailbox_sender);
if (send(sock, send_buff_, strlen(send_buff_), 0) != strlen(send_buff_))return false;
recv(sock, receive_buff_, 1024, 0);
memset(send_buff_, 0, sizeof(send_buff_));
sprintf_s(send_buff_, "RCPT TO:<%s>\r\n", mail_info_.mailbox_receiver);
if (send(sock, send_buff_, strlen(send_buff_), 0) != strlen(send_buff_))return false;
recv(sock, receive_buff_, 1024, 0);
memset(send_buff_, 0, sizeof(send_buff_));
memcpy(send_buff_, "DATA\r\n", strlen("DATA\r\n"));
if (send(sock, send_buff_, strlen(send_buff_), 0) != strlen(send_buff_))return false;
recv(sock, receive_buff_, 1024, 0);
string datastr;//这个from,reply-to,to,timestr对阿里云企业邮箱都是必须的,否则可能收不到邮件
datastr += "From: " + string(mail_info_.mailbox_sender) + "\r\n" + "Reply-To: " + string(mail_info_.mailbox_sender) + "\r\n";
datastr += "To: " + string(mail_info_.mailbox_receiver) + "\r\n";
time_t t=time(NULL);char timestr[128];
strftime(timestr, 127, "Date: %d %b %y %H:%M:%S", localtime(&t));
datastr += string(timestr) + "\r\n";
datastr += "Subject: " + string(mail_info_.mail_title) + "\r\n\r\n";
datastr += string(mail_info_.mail_body) + "\r\n";
datastr += ".\r\n";
send(sock, datastr.c_str(), datastr.length(), 0);
recv(sock, receive_buff_, 1024, 0);//正常回复250
if (receive_buff_[0] != '2' || receive_buff_[1] != '5' || receive_buff_[2] != '0')return false;
OutputDebugString(receive_buff_);
sprintf_s(send_buff_, "quit\r\n");
send(sock, send_buff_, strlen(send_buff_), 0);
recv(sock, receive_buff_, 1024, 0);
if (receive_buff_[0] != '2' || receive_buff_[1] != '2' || receive_buff_[2] != '1')return false;
OutputDebugString(receive_buff_);//正常回复221,即使这样,如果DATA没加from,reply-to,to,timestr发送也经常不成功
closesocket(sock);
WSACleanup();
return true;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。