C++ email

I found this code and i am getting a error in line 34 with "fopen" and i would also like to know if this works

error:
 
  source.cpp(34): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#include <windowsx.h>
#include <time.h>

/*If you don't know the mail exchange server for an address for the following
"nslookup -querytype=mx gmail.com" but replace gmail.com with the domain for
whatever email address you want. YOU MUST CHANGE  THESE SETTINGS OR
IT WILL NOT WORK!!! */

#define BUFSIZE 800
#define waittime 500
#define cmailserver "gmail-smtp-in.l.google.com"
#define cemailto "test@gmail.com"
#define cemailfrom "test@gmail.com"
#define LogLength 100
#define SMTPLog "smtp.log"
#define cemailsubject "Logged"

int MailIt(char *mailserver, char *emailto, char *emailfrom, char *emailsubject, char *emailmessage) {

	SOCKET sockfd;
	WSADATA wsaData;
	FILE *smtpfile;

#define bufsize 300
	int bytes_sent;   /* Sock FD */
	int err;
	struct hostent *host;   /* info from gethostbyname */
	struct sockaddr_in dest_addr;   /* Host Address */
	char line[1000];
	char *Rec_Buf = (char*)malloc(bufsize + 1);
	smtpfile = fopen(SMTPLog, "a+");
	if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR) {
		fputs("WSAStartup failed", smtpfile);
		WSACleanup();
		return -1;
	}
	if ((host = gethostbyname(mailserver)) == NULL) {
		perror("gethostbyname");
		exit(1);
	}
	memset(&dest_addr, 0, sizeof(dest_addr));
	memcpy(&(dest_addr.sin_addr), host->h_addr, host->h_length);

	/* Prepare dest_addr */
	dest_addr.sin_family = host->h_addrtype;  /* AF_INET from gethostbyname */
	dest_addr.sin_port = htons(25); /* PORT defined above */

	/* Get socket */

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		exit(1);
	}
	/* Connect !*/
	fputs("Connecting....\n", smtpfile);

	if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) == -1){
		perror("connect");
		exit(1);
	}

	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "helo me.somepalace.com\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "MAIL FROM:<");
	strncat(line, emailfrom, strlen(emailfrom));
	strncat(line, ">\n", 3);
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "RCPT TO:<");
	strncat(line, emailto, strlen(emailto));
	strncat(line, ">\n", 3);
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "DATA\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	Sleep(waittime);
	strcpy(line, "To:");
	strcat(line, emailto);
	strcat(line, "\n");
	strcat(line, "From:");
	strcat(line, emailfrom);
	strcat(line, "\n");
	strcat(line, "Subject:");
	strcat(line, emailsubject);
	strcat(line, "\n");
	strcat(line, emailmessage);
	strcat(line, "\r\n.\r\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "quit\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	fclose(smtpfile);
#ifdef WIN32
	closesocket(sockfd);
	WSACleanup();
#else
	close(sockfd);
#endif
}
The error simply says fopen is unsafe. There is fopen_s which is better. Use it instead.
that doesnt work though, now i have more errors

Add the indicated define, _CRT_SECURE_NO_WARNINGS, to your project or source file.

As long as you don't feed fopen a null or invalid pointer it is perfectly safe.
There still is a error with fopen
I get no errors when I compile the above code with VS2010.
I do get a warning that there is no return statement at line 126.

What errors are you getting after adding _CRT_SECURE_NO_WARNINGS as cire suggested?

it wont work in 2013.
So post the errors you're getting. Second request.
Last edited on
error:
1
2
source.cpp(35): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\stdio.h(211) : see declaration of 'fopen'
Did you add a define for _CRT_SECURE_NO_WARNINGS as cire suggested?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#include <windowsx.h>
#include <time.h>

/*If you don't know the mail exchange server for an address for the following
"nslookup -querytype=mx gmail.com" but replace gmail.com with the domain for
whatever email address you want. YOU MUST CHANGE  THESE SETTINGS OR
IT WILL NOT WORK!!! */

#define BUFSIZE 800
#define waittime 500
#define cmailserver "gmail-smtp-in.l.google.com"
#define cemailto "test@gmail.com"
#define cemailfrom "test@gmail.com"
#define LogLength 100
#define SMTPLog "smtp.log"
#define cemailsubject "Logged"
#define _CRT_SECURE_NO_WARNINGS

int MailIt(char *mailserver, char *emailto, char *emailfrom, char *emailsubject, char *emailmessage) {

	SOCKET sockfd;
	WSADATA wsaData;
	FILE *smtpfile;

#define bufsize 300
	int bytes_sent;   /* Sock FD */
	int err;
	struct hostent *host;   /* info from gethostbyname */
	struct sockaddr_in dest_addr;   /* Host Address */
	char line[1000];
	char *Rec_Buf = (char*)malloc(bufsize + 1);
	smtpfile = fopen(SMTPLog, "a+");
	if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR) {
		fputs("WSAStartup failed", smtpfile);
		WSACleanup();
		return -1;
	}
	if ((host = gethostbyname(mailserver)) == NULL) {
		perror("gethostbyname");
		exit(1);
	}
	memset(&dest_addr, 0, sizeof(dest_addr));
	memcpy(&(dest_addr.sin_addr), host->h_addr, host->h_length);

	/* Prepare dest_addr */
	dest_addr.sin_family = host->h_addrtype;  /* AF_INET from gethostbyname */
	dest_addr.sin_port = htons(25); /* PORT defined above */

	/* Get socket */

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		exit(1);
	}
	/* Connect !*/
	fputs("Connecting....\n", smtpfile);

	if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) == -1){
		perror("connect");
		exit(1);
	}

	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "helo me.somepalace.com\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "MAIL FROM:<");
	strncat(line, emailfrom, strlen(emailfrom));
	strncat(line, ">\n", 3);
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "RCPT TO:<");
	strncat(line, emailto, strlen(emailto));
	strncat(line, ">\n", 3);
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "DATA\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	Sleep(waittime);
	strcpy(line, "To:");
	strcat(line, emailto);
	strcat(line, "\n");
	strcat(line, "From:");
	strcat(line, emailfrom);
	strcat(line, "\n");
	strcat(line, "Subject:");
	strcat(line, emailsubject);
	strcat(line, "\n");
	strcat(line, emailmessage);
	strcat(line, "\r\n.\r\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "quit\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	fclose(smtpfile);
#ifdef WIN32
	closesocket(sockfd);
	WSACleanup();
#else
	close(sockfd);
#endif
}
Move your define before the includes.
i have more errors now

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#include <windowsx.h>
#include <time.h>

/*If you don't know the mail exchange server for an address for the following
"nslookup -querytype=mx gmail.com" but replace gmail.com with the domain for
whatever email address you want. YOU MUST CHANGE  THESE SETTINGS OR
IT WILL NOT WORK!!! */

#define BUFSIZE 800
#define waittime 500
#define cmailserver "gmail-smtp-in.l.google.com"
#define cemailto "test@gmail.com"
#define cemailfrom "test@gmail.com"
#define LogLength 100
#define SMTPLog "smtp.log"
#define cemailsubject "Logged"

int MailIt(char *mailserver, char *emailto, char *emailfrom, char *emailsubject, char *emailmessage) {

	SOCKET sockfd;
	WSADATA wsaData;
	FILE *smtpfile;

#define bufsize 300
	int bytes_sent;   /* Sock FD */
	int err;
	struct hostent *host;   /* info from gethostbyname */
	struct sockaddr_in dest_addr;   /* Host Address */
	char line[1000];
	char *Rec_Buf = (char*)malloc(bufsize + 1);
	smtpfile = fopen(SMTPLog, "a+");
	if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR) {
		fputs("WSAStartup failed", smtpfile);
		WSACleanup();
		return -1;
	}
	if ((host = gethostbyname(mailserver)) == NULL) {
		perror("gethostbyname");
		exit(1);
	}
	memset(&dest_addr, 0, sizeof(dest_addr));
	memcpy(&(dest_addr.sin_addr), host->h_addr, host->h_length);

	/* Prepare dest_addr */
	dest_addr.sin_family = host->h_addrtype;  /* AF_INET from gethostbyname */
	dest_addr.sin_port = htons(25); /* PORT defined above */

	/* Get socket */

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		exit(1);
	}
	/* Connect !*/
	fputs("Connecting....\n", smtpfile);

	if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) == -1){
		perror("connect");
		exit(1);
	}

	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "helo me.somepalace.com\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "MAIL FROM:<");
	strncat(line, emailfrom, strlen(emailfrom));
	strncat(line, ">\n", 3);
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "RCPT TO:<");
	strncat(line, emailto, strlen(emailto));
	strncat(line, ">\n", 3);
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "DATA\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	Sleep(waittime);
	strcpy(line, "To:");
	strcat(line, emailto);
	strcat(line, "\n");
	strcat(line, "From:");
	strcat(line, emailfrom);
	strcat(line, "\n");
	strcat(line, "Subject:");
	strcat(line, emailsubject);
	strcat(line, "\n");
	strcat(line, emailmessage);
	strcat(line, "\r\n.\r\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	strcpy(line, "quit\n");
	fputs(line, smtpfile);
	bytes_sent = send(sockfd, line, strlen(line), 0);
	Sleep(waittime);
	err = recv(sockfd, Rec_Buf, bufsize, 0); Rec_Buf[err] = '\0';
	fputs(Rec_Buf, smtpfile);
	fclose(smtpfile);
#ifdef WIN32
	closesocket(sockfd);
	WSACleanup();
#else
	close(sockfd);
#endif
}

errors:
1
2
3
4
5
6
7
8
9
10
11
12
1
1>Source.obj : error LNK2019: unresolved external symbol _closesocket@4 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt@@YAHPAD0000@Z)
1>Source.obj : error LNK2019: unresolved external symbol _connect@12 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt@@YAHPAD0000@Z)
1>Source.obj : error LNK2019: unresolved external symbol _htons@4 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt@@YAHPAD0000@Z)
1>Source.obj : error LNK2019: unresolved external symbol _recv@16 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt@@YAHPAD0000@Z)
1>Source.obj : error LNK2019: unresolved external symbol _send@16 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt@@YAHPAD0000@Z)
1>Source.obj : error LNK2019: unresolved external symbol _socket@12 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt@@YAHPAD0000@Z)
1>Source.obj : error LNK2019: unresolved external symbol _gethostbyname@4 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt@@YAHPAD0000@Z)
1>Source.obj : error LNK2019: unresolved external symbol _WSAStartup@8 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt@@YAHPAD0000@Z)
1>Source.obj : error LNK2019: unresolved external symbol _WSACleanup@0 referenced in function "int __cdecl MailIt(char *,char *,char *,char *,char *)" (?MailIt@@YAHPAD0000@Z)
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\kyle\documents\visual studio 2013\Projects\sending email\Debug\sending email.exe : fatal error LNK1120: 10 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You need to link against the appropriate library (ws2_32.lib) and you probably want a main function somewhere.
can u post code with your change and have it working in vs2013
He didn't change the code. You have to link the library. Google it.
Topic archived. No new replies allowed.