導航:首頁 > 軟體知識 > c語言郵件程序如何發送附件

c語言郵件程序如何發送附件

發布時間:2022-12-31 20:32:51

1. 電子郵件怎麼發送附件 郵箱怎麼發附件

1、首先注冊郵箱後,打開注冊的郵箱。

2、點擊「寫信」按鈕,進入寫信界面。在寫信界面寫好郵件,注意寫清附件的內容。

3、添加附件。點擊「添加附件」,在彈出的對話框中找到附件存放的地址,選中目標附件後,點擊「打開」按鈕添加附件。

4、寫好郵件的主題。附件添加後,主題一欄會變成附件的文件名,此時需要根據具體的事宜,修改主題的內容。

5、添加收件人的郵箱地址。主題寫好之後就可以添加收件人的地址。

6、點擊「發送」按鈕,發送郵件。點擊郵件下方的「發送」按鈕,即可發送郵件。

2. C語言郵件程序 如何發送附件

C語言發送郵件 完整代碼:

#include<windows.h>
#include<stdio.h>
#include<WinSock.h>
#include<iostream>
usingnamespacestd;
#pragmacomment(lib,"ws2_32.lib")

structBase64Date6
{
unsignedintd4:6;
unsignedintd3:6;
unsignedintd2:6;
unsignedintd1:6;
};
//協議中加密部分使用的是base64方法
charConvertToBase64(charc6);
voidEncodeBase64(char*dbuf,char*buf128,intlen);
voidSendMail(char*email,char*body);
intOpenSocket(structsockaddr*addr);

intmain()
{
charEmailTo[]="[email protected]";
charEmailContents[]="From:"lucy"<[email protected]> "
"To:"dasiy"<[email protected]> "
"Subject:Hello "
"HelloWorld,HelloEmail!"
//"DATA "//告訴伺服器下面是郵件
//下面是郵件頭
"TO:[email protected] "
"FROM:[email protected] "
"SUBJECT:SMTP協議測試 "
"Date:2015-04-20 "
"X-Mailer:fice'smailer "
"MIMI-Version:1.0 "
"Content-Type:multipart/mixed;boundary="#BOUNDARY#" "//設置邊界值,區分郵件內容的兩個回車換行
"Content-Transfer-Encoding:7bit "
"Thisisamulti-partmessageinMIMEformat "
//發送郵件內容頭部信息
"--#BOUNDARY# "
"Content-Type:text/plain;charset=gb2312 "
"Content-Transfer-Encoding:printable "
//發送郵件的內容部分
"SMTP協議測試:發送附件/n----byfice2015.04.20 "
//發送附件頭部信息
"--#BOUNDARY# "
"Content-Type:text/plain;name=student.txt "
"Content-Transfer-Encoding:base64 "
"Content-Disposition:attachment;filename="test.txt" ";
SendMail(EmailTo,EmailContents);
return0;
}

charConvertToBase64(charuc)
{
if(uc<26)
{
return'A'+uc;
}
if(uc<52)
{
return'a'+(uc-26);
}
if(uc<62)
{
return'0'+(uc-52);
}
if(uc==62)
{
return'+';
}
return'/';
}

//base64的實現
voidEncodeBase64(char*dbuf,char*buf128,intlen)
{
structBase64Date6*ddd=NULL;
inti=0;
charbuf[256]={0};
char*tmp=NULL;
charcc='';

memset(buf,0,256);
strcpy_s(buf,256,buf128);
for(i=1;i<=len/3;i++)
{
tmp=buf+(i-1)*3;
cc=tmp[2];
tmp[2]=tmp[0];
tmp[0]=cc;
ddd=(structBase64Date6*)tmp;
dbuf[(i-1)*4+0]=ConvertToBase64((unsignedint)ddd->d1);
dbuf[(i-1)*4+1]=ConvertToBase64((unsignedint)ddd->d2);
dbuf[(i-1)*4+2]=ConvertToBase64((unsignedint)ddd->d3);
dbuf[(i-1)*4+3]=ConvertToBase64((unsignedint)ddd->d4);
}
if(len%3==1)
{
tmp=buf+(i-1)*3;
cc=tmp[2];
tmp[2]=tmp[0];
tmp[0]=cc;
ddd=(structBase64Date6*)tmp;
dbuf[(i-1)*4+0]=ConvertToBase64((unsignedint)ddd->d1);
dbuf[(i-1)*4+1]=ConvertToBase64((unsignedint)ddd->d2);
dbuf[(i-1)*4+2]='=';
dbuf[(i-1)*4+3]='=';
}
if(len%3==2)
{
tmp=buf+(i-1)*3;
cc=tmp[2];
tmp[2]=tmp[0];
tmp[0]=cc;
ddd=(structBase64Date6*)tmp;
dbuf[(i-1)*4+0]=ConvertToBase64((unsignedint)ddd->d1);
dbuf[(i-1)*4+1]=ConvertToBase64((unsignedint)ddd->d2);
dbuf[(i-1)*4+2]=ConvertToBase64((unsignedint)ddd->d3);
dbuf[(i-1)*4+3]='=';
}
return;
}
//發送郵件
voidSendMail(char*email,char*body)
{
intsockfd={0};
charbuf[1500]={0};
charrbuf[1500]={0};
charlogin[128]={0};
charpass[128]={0};
WSADATAWSAData;
structsockaddr_intheir_addr={0};
WSAStartup(MAKEWORD(2,2),&WSAData);
memset(&their_addr,0,sizeof(their_addr));

their_addr.sin_family=AF_INET;
their_addr.sin_port=htons(25);
hostent*hptr=gethostbyname("smtp.126.com");
memcpy(&their_addr.sin_addr.S_un.S_addr,hptr->h_addr_list[0],hptr->h_length);
printf("IPofsmpt.126.comis:%d:%d:%d:%d ",
their_addr.sin_addr.S_un.S_un_b.s_b1,
their_addr.sin_addr.S_un.S_un_b.s_b2,
their_addr.sin_addr.S_un.S_un_b.s_b3,
their_addr.sin_addr.S_un.S_un_b.s_b4);

//連接郵件伺服器,如果連接後沒有響應,則2秒後重新連接
sockfd=OpenSocket((structsockaddr*)&their_addr);
memset(rbuf,0,1500);
while(recv(sockfd,rbuf,1500,0)==0)
{
cout<<"reconnect..."<<endl;
Sleep(2);
sockfd=OpenSocket((structsockaddr*)&their_addr);
memset(rbuf,0,1500);
}

cout<<rbuf<<endl;

//EHLO
memset(buf,0,1500);
sprintf_s(buf,1500,"EHLOHYL-PC ");
send(sockfd,buf,strlen(buf),0);
memset(rbuf,0,1500);
recv(sockfd,rbuf,1500,0);
cout<<"EHLOREceive:"<<rbuf<<endl;

//AUTHLOGIN
memset(buf,0,1500);
sprintf_s(buf,1500,"AUTHLOGIN ");
send(sockfd,buf,strlen(buf),0);
memset(rbuf,0,1500);
recv(sockfd,rbuf,1500,0);
cout<<"AuthLoginReceive:"<<rbuf<<endl;

//USER
memset(buf,0,1500);
sprintf_s(buf,1500,"[email protected]");//你的郵箱賬號
memset(login,0,128);
EncodeBase64(login,buf,strlen(buf));
sprintf_s(buf,1500,"%s ",login);
send(sockfd,buf,strlen(buf),0);
cout<<"Base64UserName:"<<buf<<endl;
memset(rbuf,0,1500);
recv(sockfd,rbuf,1500,0);
cout<<"UserLoginReceive:"<<rbuf<<endl;

//PASSWORD
sprintf_s(buf,1500,"********");//你的郵箱密碼
memset(pass,0,128);
EncodeBase64(pass,buf,strlen(buf));
sprintf_s(buf,1500,"%s ",pass);
send(sockfd,buf,strlen(buf),0);
cout<<"Base64Password:"<<buf<<endl;

memset(rbuf,0,1500);
recv(sockfd,rbuf,1500,0);
cout<<"SendPasswordReceive:"<<rbuf<<endl;

//MAILFROM
memset(buf,0,1500);
sprintf_s(buf,1500,"MAILFROM:<[email protected]> ");
send(sockfd,buf,strlen(buf),0);
memset(rbuf,0,1500);
recv(sockfd,rbuf,1500,0);
cout<<"setMailFromReceive:"<<rbuf<<endl;

//RCPTTO第一個收件人
sprintf_s(buf,1500,"RCPTTO:<%s> ",email);
send(sockfd,buf,strlen(buf),0);
memset(rbuf,0,1500);
recv(sockfd,rbuf,1500,0);
cout<<"TellSendtoReceive:"<<rbuf<<endl;

//DATA准備開始發送郵件內容
sprintf_s(buf,1500,"DATA ");
send(sockfd,buf,strlen(buf),0);
memset(rbuf,0,1500);
recv(sockfd,rbuf,1500,0);
cout<<"SendMailPrepareReceive:"<<rbuf<<endl;

//發送郵件內容, . 內容結束標記
sprintf_s(buf,1500,"%s . ",body);
send(sockfd,buf,strlen(buf),0);//發送郵件,不帶附件的內容

//用二進制方式讀取附件文件內容並轉為base64格式
FILE*pf=fopen("D:\test.txt","rb");
fseek(pf,0,SEEK_END);
intfilelen=ftell(pf);
char*filebuf=(char*)malloc(filelen);
char*filebase64=(char*)malloc(filelen*2);
memset(filebase64,0,filelen*2);
fseek(pf,0,SEEK_SET);
fread(filebuf,1,filelen,pf);
EncodeBase64(filebase64,filebuf,filelen);

send(sockfd,filebase64,strlen(filebase64),0);//發送郵件的內容
memset(rbuf,0,1500);
recv(sockfd,rbuf,1500,0);
cout<<"SendMailReceive:"<<rbuf<<endl;

//QUIT
sprintf_s(buf,1500,"QUIT ");
send(sockfd,buf,strlen(buf),0);
memset(rbuf,0,1500);
recv(sockfd,rbuf,1500,0);
cout<<"QuitReceive:"<<rbuf<<endl;

//清理工作
closesocket(sockfd);
WSACleanup();
return;
}
//打開TCPSocket連接
intOpenSocket(structsockaddr*addr)
{
intsockfd=0;
sockfd=socket(PF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
cout<<"Opensockfd(TCP)error!"<<endl;
exit(-1);
}
if(connect(sockfd,addr,sizeof(structsockaddr))<0)
{
cout<<"Connectsockfd(TCP)error!"<<endl;
exit(-1);
}
returnsockfd;
}

3. 郵件怎麼發附件

打開163郵箱的「寫信」窗口,點擊帶有回形針標志的「添加附件」按鈕即可在你的本地文件中找到要發的文件(郵箱是不能添加文件夾的,如果是文件夾需要將文件夾壓縮再添加),下面為圖示步驟:

1.打開163郵箱,點擊「寫信」。

4. 怎樣把電子郵件以附件形式發送!

1、雙擊打開電腦桌面的QQ郵箱,進入QQ郵箱。/mail.qq.com/cgi-bin/loginpage"target="_blank"title="QQ郵箱官網">QQ郵箱官網

/iknow-pic.cdn.bcebos.com/94cad1c8a786c917dde56c61c73d70cf3ac75787"target="_blank"title="點擊查看大圖"class="illustration_alink">/iknow-pic.cdn.bcebos.com/94cad1c8a786c917dde56c61c73d70cf3ac75787?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="//www.wxbohuan.com/pic_94cad1c8a786c917dde56c61c73d70cf3ac75787"/>
2、在進入官網後在頁面上輸入賬號密碼登錄QQ郵箱。
/iknow-pic.cdn.bcebos.com/279759ee3d6d55fba9c94f7e63224f4a20a4dd5e"target="_blank"title="點擊查看大圖"class="illustration_alink">/iknow-pic.cdn.bcebos.com/279759ee3d6d55fba9c94f7e63224f4a20a4dd5e?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="//www.wxbohuan.com/pic_279759ee3d6d55fba9c94f7e63224f4a20a4dd5e"/>
3、在進入QQ郵箱後點擊寫信選項,進入寫郵件界面中。
/iknow-pic.cdn.bcebos.com/d043ad4bd11373f0566e56c7aa0f4bfbfbed0410"target="_blank"title="點擊查看大圖"class="illustration_alink">/iknow-pic.cdn.bcebos.com/d043ad4bd11373f0566e56c7aa0f4bfbfbed0410?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="//www.wxbohuan.com/pic_d043ad4bd11373f0566e56c7aa0f4bfbfbed0410"/>
4、在寫郵件界面中將文本輸入完成後點擊頁面上的添加附件選項,將需要發送的文件添加到郵箱的附件中去。
/iknow-pic.cdn.bcebos.com/58ee3d6d55fbb2fb5804604a414a20a44623dcbe"target="_blank"title="點擊查看大圖"class="illustration_alink">/iknow-pic.cdn.bcebos.com/58ee3d6d55fbb2fb5804604a414a20a44623dcbe?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="//www.wxbohuan.com/pic_58ee3d6d55fbb2fb5804604a414a20a44623dcbe"/>
5、點擊發送按鍵即可。
/iknow-pic.cdn.bcebos.com/4034970a304e251f09cedca0a986c9177f3e53a8"target="_blank"title="點擊查看大圖"class="illustration_alink">/iknow-pic.cdn.bcebos.com/4034970a304e251f09cedca0a986c9177f3e53a8?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="//www.wxbohuan.com/pic_4034970a304e251f09cedca0a986c9177f3e53a8"/>

5. 如何創建帶附件的郵件並發送它使用 MAPI / VC + +

本文演示了如何製作和發送消息。它還演示如何將附件添加到要發送的消息。
更多信息
下面的代碼創建 MAPI 會話、 登錄到私有消息存儲區,創建一封新郵件、 郵件上, 設置屬性,然後將其發送。

該郵件的收件人,從 Exchange 或 Outlook 郵件客戶端上顯示的典型的地址對話框中選擇。

編譯下面的代碼使用在 鏈接 選項卡上的 項目設置 對話框 忽略所有默認庫 選項。必須也使用了以下庫:
Mapi32.lib
Version.lib
Edkdebug.lib
Edkmapi.lib
Edkutils.lib
Addrlkup.lib
Edkguid.lib
Rulecls.lib
Msvcrt.lib
#include <edk.h>
HRESULT SetMessageProp ( LPMAPISESSION m_pSession,
LPMESSAGE lpMessage);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pszCmd,
int nCmdShow)
{
// Load strings, and register window classes.
LPMDB lpStore = NULL;
LPMAPISESSION lpSession = NULL;
LPMESSAGE lpMessage = NULL;
HRESULT hr = NULL;
ULONG cbEntryID = 0;
LPENTRYID lpEntryID = NULL;
LPCIID lpInterface = NULL;
ULONG ulFlags = MAPI_BEST_ACCESS;
ULONG ulObjType = 0;
LPMAPIFOLDER lpFolder = NULL;
ULONG cbEIDFolder = 0;
LPENTRYID lpEIDFolder = NULL;
CHAR szFolder[MAX_PATH + 1] = {0};

hr = MAPIInitialize(NULL);
if (FAILED(hr))
{
return 1;
}
hr = MAPILogonEx(0,"", NULL,
MAPI_LOGON_UI | MAPI_NEW_SESSION | MAPI_EXPLICIT_PROFILE,
&lpSession);

if (FAILED(hr))
{
MessageBox(NULL,"MAPI Logon failed",NULL,MB_OK);
goto cleanup;
}

if (FAILED(hr = HrOpenExchangePrivateStore(lpSession,&lpStore)))
{
MessageBox(NULL,"Message Store Not Opened",NULL,MB_OK);
goto cleanup;
}

strcpy(szFolder, "Top of Information Store\\Outbox");
hr = HrMAPIOpenFolderEx (lpStore,'\\',szFolder,&lpFolder);
if (FAILED(hr))
{
MessageBox(NULL,"Outbox Could Not Be Opened",NULL,MB_OK);
goto cleanup;
}

if (FAILED(hr = lpFolder->CreateMessage(NULL, MAPI_DEFERRED_ERRORS, &lpMessage)))
{
MessageBox(NULL,"CreateMessage Fail",NULL,MB_OK);
goto cleanup;
}

if (FAILED(hr = SetMessageProp(lpSession, lpMessage)))
goto cleanup;

cleanup:

if ( NULL != lpMessage )
{
lpMessage ->Release ( );
lpMessage = NULL;
}

if (NULL != lpFolder)
{
lpFolder->Release();
lpFolder = NULL;
}
if (lpSession)
{
lpSession->Logoff(0, 0, 0);
ULRELEASE(lpSession);
}
MAPIUninitialize();
return 0;
}

HRESULT SetMessageProp ( LPMAPISESSION lpSession,
LPMESSAGE lpMessage)
{
HRESULT hr = S_OK;
enum {SUBJECT, CLASS, BODY, MSG_DEL, MSG_PROPS };
LPADRBOOK lpAdrBook = NULL;
LPADRLIST lpAdrList = NULL;
LPATTACH pAtt = NULL;
ULONG ulAttNum;

// Display Address Dialog

if (FAILED(hr = lpSession->OpenAddressBook(0L,NULL,0,&lpAdrBook)))
goto Quit;

LPSTR rglpszDestTitles[1];
ULONG rgulDestComps[1];
ULONG m_hWnd1;
rglpszDestTitles[0] = "To";
rgulDestComps[0] = MAPI_TO;
ADRPARM adrparm;
adrparm.cbABContEntryID = 0;
adrparm.lpABContEntryID = NULL;
adrparm.ulFlags = DIALOG_MODAL;
adrparm.lpReserved = NULL;
adrparm.ulHelpContext = 0;
adrparm.lpszHelpFileName = NULL;
adrparm.lpfnABSDI = NULL;
adrparm.lpfnDismiss = NULL;
adrparm.lpvDismissContext = NULL;
adrparm.lpszCaption = "MAPI Dialog";
adrparm.lpszNewEntryTitle = "For this test dialog";
adrparm.lpszDestWellsTitle = "Selected Recipients:";
adrparm.cDestFields = 0xffffffff;
adrparm.nDestFieldFocus = 0;
adrparm.lppszDestTitles = NULL;
adrparm.lpulDestComps = NULL;
adrparm.lpContRestriction = NULL;
adrparm.lpHierRestriction = NULL;
m_hWnd1 = 0;

if (FAILED(hr = lpAdrBook->Address(&m_hWnd1,&adrparm,&lpAdrList)))
goto Quit;

// Message properties tag array
SPropValue lpPropValueArray2[MSG_PROPS];
lpPropValueArray2[SUBJECT].ulPropTag = PR_SUBJECT;
lpPropValueArray2[SUBJECT].Value.lpszA = "Testing IPM With Attachments";

// TO DO:Change this to your custom form class

lpPropValueArray2[CLASS].ulPropTag = PR_MESSAGE_CLASS;
lpPropValueArray2[CLASS].Value.lpszA = "IPM.Note";
lpPropValueArray2[BODY].ulPropTag = PR_BODY;
lpPropValueArray2[BODY].Value.lpszA = "Custom Text For Message";
lpPropValueArray2[MSG_DEL].ulPropTag = PR_DELETE_AFTER_SUBMIT;
lpPropValueArray2[MSG_DEL].Value.b = TRUE;

if (FAILED(hr = lpMessage->ModifyRecipients(MODRECIP_ADD,lpAdrList)))
goto Quit;

if (FAILED(hr = lpMessage->CreateAttach( NULL, (ULONG)0, &ulAttNum, &pAtt)))
goto Quit;
{

enum {METHOD,RENDERING,PATH, FILENAME, DISPLAYNAME, NUM_ATT_PROPS};
SPropValue spvAttach[NUM_ATT_PROPS];
spvAttach[METHOD].ulPropTag = PR_ATTACH_METHOD;
spvAttach[METHOD].Value.l = ATTACH_BY_REFERENCE;

spvAttach[RENDERING].ulPropTag = PR_RENDERING_POSITION;
spvAttach[RENDERING].Value.l = -1;

spvAttach[PATH].ulPropTag = PR_ATTACH_PATHNAME;
spvAttach[PATH].Value.lpszA ="\\\\<server>\\<share>\\code.txt";

spvAttach[FILENAME].ulPropTag = PR_ATTACH_FILENAME;
spvAttach[FILENAME].Value.lpszA = "code.txt";

spvAttach[DISPLAYNAME].ulPropTag = PR_DISPLAY_NAME;
spvAttach[DISPLAYNAME].Value.lpszA = "Great code.txt";

// Save the properties we have set on the attachment
if (FAILED(hr = pAtt ->SetProps(
NUM_ATT_PROPS,
(LPSPropValue)&spvAttach,
NULL)))
goto Quit;

if (FAILED(hr = pAtt->SaveChanges(0)))
goto Quit;

}

if (FAILED(hr = lpMessage->SetProps(MSG_PROPS,lpPropValueArray2,NULL)))
goto Quit;

if (FAILED(hr = lpMessage->SubmitMessage(0L)))
goto Quit;

// Clean up and release all objects no longer needed in this
// method. Be sure to return the value of hr to the caller.

Quit:

if ( NULL != lpAdrBook )
{
lpAdrBook ->Release ( );
lpAdrBook = NULL;
}

if ( NULL != lpAdrList )
{
MAPIFreeBuffer(lpAdrList);
lpAdrList = NULL;
}

if ( NULL != pAtt )
{
pAtt ->Release ( );
pAtt = NULL;
}

return hr;

}

6. 郵件附件怎麼發送

就是把文件以word形式保存,再用附件發送即可。

具體操作步驟如下:

一、第一步先輸入郵箱的賬號和密碼登錄上需要發送郵件的郵箱,如下圖所示。

閱讀全文

與c語言郵件程序如何發送附件相關的資料

熱點內容
怎麼找代理商資料 瀏覽:760
襄陽中考志願填了多久才能有錄取信息 瀏覽:430
國產紅酒如何代理 瀏覽:452
超市賣農產品需要什麼資料 瀏覽:672
程序員需要什麼稱號 瀏覽:900
爆款產品是怎麼出貨的 瀏覽:235
銀行放貸需要什麼程序 瀏覽:960
蘇州珍島技術有限公司怎麼樣 瀏覽:538
全程陪同領導信息怎麼發 瀏覽:815
有什麼識別菌的小程序 瀏覽:850
附近人如何群發信息 瀏覽:653
福建玉器市場什麼地方 瀏覽:202
如何強行停止某程序 瀏覽:752
9針數據線介面是什麼 瀏覽:950
棗庄技術員證書怎麼考有用嗎 瀏覽:636
有正負的波動數據用什麼分析 瀏覽:310
湛江有哪些水產品種 瀏覽:386
數學十幾減九的數學信息是什麼 瀏覽:876
產品銷售分享會怎麼開頭 瀏覽:382
錯版幣怎麼鑒定拍賣交易 瀏覽:701