C++ SMTP编译错误
我有这个SMTP的登录认证代码,我正试图在Ubuntu 13.04下编译它 但是当我试图用g++-o命令行编译程序时,我遇到了一个编译器错误C++ SMTP编译错误,c++,compilation,compiler-errors,smtp,C++,Compilation,Compiler Errors,Smtp,我有这个SMTP的登录认证代码,我正试图在Ubuntu 13.04下编译它 但是当我试图用g++-o命令行编译程序时,我遇到了一个编译器错误 mailogin.cpp: In function ‘bool smtp_send(std::string, int, std::string, std::string, std::vector<std::basic_string<char> >, std::string, std::string, std::string)’: m
mailogin.cpp: In function ‘bool smtp_send(std::string, int, std::string, std::string, std::vector<std::basic_string<char> >, std::string, std::string, std::string)’:
mailogin.cpp:148:17: error: ‘login_name’ was not declared in this scope
mailogin.cpp:148:37: error: ‘login_pass’ was not declared in this scope
mailogin.cpp:150:34: error: ‘memset’ was not declared in this scope
mailogin.cpp:152:61: error: ‘memcpy’ was not declared in this scope
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:在函数“bool smtp_send(std::string,int,std::string,std::string,std::vector,std::string,std::string,std::string)”中:
mailogin.cpp:148:17:错误:“登录名”未在此作用域中声明
mailogin.cpp:148:37:错误:未在此作用域中声明“login\u pass”
mailogin.cpp:150:34:错误:“memset”未在此作用域中声明
mailogin.cpp:152:61:错误:“memcpy”未在此作用域中声明
mailogin.cpp:190:1:错误:输入末尾应为“}”
mailogin.cpp:190:1:错误:输入末尾应为“}”
mailogin.cpp:190:1:错误:输入末尾应为“}”
mailogin.cpp:190:1:错误:输入末尾应为“}”
mailogin.cpp:190:1:错误:输入末尾应为“}”
mailogin.cpp:190:1:错误:输入末尾应为“}”
有人能帮我吗
这是我的密码
#include <string>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <vector>
using std::string;
using std::vector;
#define safe_send( sd, b ) if( sd_write(sd, b) <= 0 ){ close(sd); return false; }
#define safe_read( sd, b ) if( (buffer = sd_read(sd)) == "" ){ \
close(sd); return false; \
} \
else if( buffer.find("220") == string::npos && \
buffer.find("235") == string::npos && \
buffer.find("250") == string::npos && \
buffer.find("334") == string::npos && \
buffer.find("354") == string::npos ){ \
printf( "WARNING: %s\n", buffer.c_str() ); \
int sd_create( char *host, int port ){
int sd = socket( AF_INET, SOCK_STREAM, 0 );
if( sd <= 0 ){
return -1;
}
struct sockaddr_in server;
hostent * resol = gethostbyname( host );
if( resol ){
bzero( &server, sizeof(server) );
server.sin_family = AF_INET;
server.sin_port = htons(port);
bcopy( resol->h_addr, &(server.sin_addr.s_addr), resol->h_length );
}
if( connect( sd, (struct sockaddr*)&server, sizeof(server) ) != 0 ){
return -1;
}
return sd;
}
string sd_read( int sd ){
string buffer("");
unsigned char c;
bool isEOL(false), isEOF(false);
while( !isEOL && !isEOF ){
if( recv( sd, &c, sizeof(unsigned char), 0 ) < 1 ){
isEOF = true;
}
else if( c == '\n' ){
isEOL = true;
}
else if( c != '\r' ){
buffer += c;
}
}
return buffer;
}
int sd_write( int sd, string buffer ){
return write( sd, buffer.c_str(), buffer.size() );
}
string base64( unsigned char *data, unsigned int size ){
static const char b64_charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string ret;
unsigned char block_3[3];
unsigned char block_4[4];
unsigned char * str = data;
int i = 0,
j = 0;
while( size-- ){
block_3[i++] = *(str++);
if( i == 3 ){
block_4[0] = (block_3[0] & 0xfc) >> 2;
block_4[1] = ((block_3[0] & 0x03) << 4) + ((block_3[1] & 0xf0) >> 4);
block_4[2] = ((block_3[1] & 0x0f) << 2) + ((block_3[2] & 0xc0) >> 6);
block_4[3] = block_3[2] & 0x3f;
for(i = 0; (i <4) ; i++){
ret += b64_charset[block_4[i]];
}
i = 0;
}
}
if(i){
for(j = i; j < 3; j++){
block_3[j] = '\0';
}
block_4[0] = (block_3[0] & 0xfc) >> 2;
block_4[1] = ((block_3[0] & 0x03) << 4) + ((block_3[1] & 0xf0) >> 4);
block_4[2] = ((block_3[1] & 0x0f) << 2) + ((block_3[2] & 0xc0) >> 6);
block_4[3] = block_3[2] & 0x3f;
for(j = 0; (j < i + 1); j++){
ret += b64_charset[block_4[j]];
}
while((i++ < 3)){
ret += '=';
}
}
return ret;
}
bool smtp_send( string server_name, int port, string from, string subject, vector<string> receivers, string message, string login, string password )
{
int i, j, sd;
string buffer("");
sd = sd_create( (char *)server_name.c_str(), port );
if ( sd == -1 ){
return false;
}
safe_send( sd, "EHLO emoticode_mailer\r\n" );
// read until last '250 .*' reached
while( (buffer = sd_read(sd)) != "" ){
if( buffer.find("220") == string::npos &&
buffer.find("235") == string::npos &&
buffer.find("250") == string::npos &&
buffer.find("334") == string::npos &&
buffer.find("354") == string::npos ){
printf( "WARNING: %s\n", buffer.c_str() );
}
else if( buffer.find( "250 " ) != string::npos ){
break;
}
}
int auth_len = login_name.size() + login_pass.size() + 2;
unsigned char * b64auth = new unsigned char[auth_len];
memset( b64auth, 0x00, auth_len );
memcpy( &b64auth[1], login_name.c_str(), login_name.size() );
memcpy( &b64auth[2 + login_name.size()], login_pass.c_str(), login_pass.size() );
string hash = base64( b64auth, auth_len );
delete [] b64auth;
safe_send( sd, "AUTH PLAIN " + hash + "\r\n" );
safe_read( sd, buffer );
safe_send( sd, "MAIL FROM: <" + from + ">\r\n" );
safe_read( sd, buffer );
for( i = 0; i < receivers.size(); ++i ){
safe_send( sd, "RCPT TO: <" + receivers[i] + ">\r\n" );
safe_read( sd, buffer );
}
safe_send( sd, "DATA\r\n" );
safe_read( sd, buffer );
safe_send( sd, "Subject: " + subject + "\r\n" );
safe_send( sd, "From: <" + from + ">\r\n" );
safe_send( sd, "To: <" + receivers[0] + ">\r\n" );
safe_send( sd, "Content-Type: text/plain\r\n" );
safe_send( sd, "Mime-Version: 1.0\r\n" );
safe_send( sd, "X-Mailer: Emoticode smtp_send\r\n" );
safe_send( sd, "Content-Transfer-Encoding: 7bit\r\n\r\n" );
safe_send( sd, message );
safe_send( sd, "\r\n.\r\n" );
safe_read( sd, buffer );
safe_send( sd, "RSET\r\n" );
safe_read( sd, buffer );
safe_send( sd, "QUIT\r\n" );
close(sd);
return true;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用std::string;
使用std::vector;
#定义安全发送(sd,b)if(sd_写入(sd,b)h_长度);
}
if(connect(sd,(struct sockaddr*)和服务器,sizeof(服务器))!=0){
返回-1;
}
返回sd;
}
字符串sd_读取(整数sd){
字符串缓冲区(“”);
无符号字符c;
bool-isEOL(假)、isEOF(假);
而(!isEOL&!isEOF){
if(recv(sd,&c,sizeof(unsigned char),0)<1){
isEOF=真;
}
else如果(c=='\n'){
isEOL=真;
}
如果(c!='\r'),则为else{
缓冲区+=c;
}
}
返回缓冲区;
}
int-sd_写入(int-sd,字符串缓冲区){
返回写操作(sd,buffer.c_str(),buffer.size());
}
字符串base64(无符号字符*数据,无符号整数大小){
静态常量字符b64u字符集[]=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789+/”;
字符串ret;
无符号字符块_3[3];
无符号字符块_4[4];
无符号字符*str=数据;
int i=0,
j=0;
而(大小--){
块3[i++]=*(str++);
如果(i==3){
块4[0]=(块3[0]&0xfc)>>2;
块_4[1]=((块_3[0]&0x03)>4);
块4[2]=((块3[1]&0x0f)>6);
块_4[3]=块_3[2]&0x3f;
对于(i=0;(i>2;
块_4[1]=((块_3[0]&0x03)>4);
块4[2]=((块3[1]&0x0f)>6);
块_4[3]=块_3[2]&0x3f;
对于(j=0;(j
你没有声明登录名和登录密码。这不应该是登录名和密码吗?你把它们作为参数传递给函数smtp发送?
吗?你能发布相应的代码吗?单从错误消息就很难看出哪里出了问题。我刚刚发布了我的报价,thx伴侣:)