我发现了这个电子邮件程序,我无法运行它。我正在使用visual studio 2013 c ++。如果你知道的话,我也会接受任何其他程序。我需要的是能够将其放入我现有的代码中以通过电子邮件将号码发送到Gmail帐户。我的错误是:
错误:
1>------ Build started: Project: sending email, Configuration: Debug Win32 ------
1>cl : Command line warning D9007: '/Gm' requires '/Zi or /ZI'; option ignored
1> Source.cpp
1>c:\users\kyle\documents\visual studio 2013\projects\sending email\sending email\source.cpp(127): warning C4715: 'MailIt' : not all control paths return a value
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 ==========
代码:
#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 "kmtompkinscode@gmail.com"
#define cemailfrom "kmtompkinscode@gmail.com"
#define LogLength 100
#define SMTPLog "smtp.log"
#define cemailsubject "pin"
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
}
答案 0 :(得分:0)
我想问题是在project setting->C/C++->General
处理警告时错误设置为“是”。
要解决此问题,您可以将其设置为“否”,然后在project setting->C/C++->Preprocessor
Preprocessor definitions
中添加_CRT_SECURE_NO_WARNINGS
修改强>