Witam.
Chciałem napisać klasę do wysłania emaili. Jesli skopiuje dane z klasy i wysle to jako jeden kod to email się wysyła. Natomiast jak zrobiłem klase (możecie doradzić czy klasa ma dobrą idee? czy wypadałoby coś zmienic? ) to w miejscu gdzie jest treść emaila się zawiesza i potem wyskakuje timeout.
Kod klasy jest tutaj
#include<iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define HELO "HELO\r\n"
#define DATA "DATA\r\n"
#define QUIT "QUIT\r\n"
//struct hostent *hp, *gethostbyname();
//char *host_id=(char *)"moj_serv.com";
//char *from_id=(char *)"noreplay@moj_serv.com";
//char *to_id=(char *)"user1@moj_serv.com";
//char *sub=(char *)"Topic 3 \r\n";
//char wkstr[1000]="hello how r u ?\r\n";
class ml {
public:
char *host_id; //"moj_serv.com";
char *to_id;//"user1@moj_serv.com";
char *sub;//"Topic 3 \r\n";
char *body;//body
char *from;
char buf[BUFSIZ+1];
int len;
int sock;
struct sockaddr_in server;
struct hostent *hp;
void initialization() {
/*=====Create Socket=====*/
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock==-1) {
perror("opening stream socket");
exit(1);
} else
cout << "socket created\n";
/*=====Verify host=====*/
server.sin_family = AF_INET;
hp = gethostbyname(host_id);
if (hp==(struct hostent *) 0) {
fprintf(stderr, "%s: unknown host\n", host_id);
exit(2);
}
/*=====Connect to port 25 on remote host=====*/
memcpy((char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length);
server.sin_port=htons(25); /* SMTP PORT */
if (connect(sock, (struct sockaddr *) &server, sizeof server)==-1) {
perror("connecting stream socket");
exit(1);
} else
cout << "Connected\n";
}
void send_socket(char *s) {
/*=====Send a string to the socket=====*/
write(sock,s,strlen(s));
write(1,s,strlen(s));
printf("Client:%s\n",s);
}
void read_socket() {
//=====Read a string from the socket=====*/
len = read(sock,buf,BUFSIZ);
write(1,buf,len);
printf("Server:%s\n",buf);
}
void send_mail(char *from, char *to, char *sub, char *body) {
cout << "start" << endl;
set_host((char *)"inv-secure.com");
cout << "set host" << endl;
initialization();
cout << "init" << endl;
set_from(from);
cout << "set from" << endl;
set_recipment(to);
cout << "set recipment" << endl;
set_topic(sub);
cout << "topic" << endl;
set_body(body);
cout << "set body" << endl;
//start send
read_socket(); /* SMTP Server logon string */
cout << "read 1" << endl;
send_socket((char *)HELO); /* introduce ourselves */
cout << "send 1" << endl;
read_socket(); /*Read reply */
cout << "read 2" << endl;
send_socket((char *)"MAIL FROM: ");
send_socket(from);
send_socket((char *)"\r\n");
cout << "send 3" << endl;
read_socket(); /* Sender OK */
cout << "read 2" << endl;
send_socket((char *)"VRFY ");
send_socket(from);
send_socket((char *)"\r\n");
cout << "send 4" << endl;
read_socket(); // Sender OK */
cout << "read 3" << endl;
send_socket((char *)"RCPT TO: "); /*Mail to*/
send_socket(to);
send_socket((char *)"\r\n");
cout << "send 5" << endl;
read_socket(); // Recipient OK*/
cout << "read 4" << endl;
send_socket((char *)DATA);// body to follow*/
send_socket((char *)"Subject: ");
send_socket(sub);
cout << "send 6" << endl;
read_socket(); // Recipient OK*/
cout << "read 5" << endl;
send_socket(body);
send_socket((char *)".\r\n");
cout << "send 7" << endl;
read_socket();
cout << "read 6" << endl;
send_socket((char *)QUIT); /* quit */
cout << "send 8" << endl;
read_socket(); // log off */
//=====Close socket and finish=====*/
close(sock);
exit(0);
}
void set_host(char *s) {
host_id = s;
}
void set_from(char *s) {
from = s;
}
void set_recipment(char *s) {
to_id = s;
}
void set_topic(char *s) {
sub = s;
}
void set_body(char *s) {
body = s;
}
};
/*=====MAIN=====*/
int main() {
ml test;
test.send_mail((char *)"noreplay@moj_serv.com", (char *)"user1@moj_serv.com", (char *)"Topic 3 \r\n", (char *)"siema co slychac");
//57587
}
Kod na ktorym mi sie zawiesza to:
send_socket(body);
send_socket((char *)".\r\n");
cout << "send 7" << endl;
Jak zatem zmieniam kod z
char *body;//body
na
char body[100];//body
bo jak tak zmienie to działa mi właśnie w tej wersji bez klasy i się wysyła
to wtedy mam błąd
mail.cpp: In member function 'void ml::set_body(char*)':
mail.cpp:160: error: incompatible types in assignment of 'char*' to 'char [100]'
i nie wiem jak to zmienic na char[100] albo zamiast charow wszystko na string