导航:首页 > 软件知识 > 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语言邮件程序如何发送附件相关的资料

热点内容
产品销售分享会怎么开头 浏览:380
错版币怎么鉴定拍卖交易 浏览:699
信息化建设是一个什么样的工作 浏览:666
怎么看塑料产品能不能降解 浏览:49
如何整合金融资产交易所 浏览:972
物联网发展核心技术是什么 浏览:492
什么是电网企业代理购电 浏览:302
玉石交易大厅系统开发需要多少钱 浏览:685
医疗信息交换标准是什么意思 浏览:937
如何提高叉车技术 浏览:97
欠钱最少多少可以走法律程序 浏览:583
哪个行业小程序比较多 浏览:16
楚留香新衣服为什么不能交易 浏览:958
招投标信息在哪些地方发布 浏览:72
如何将程序员说的高大上 浏览:390
海鲜鱿鱼仔零食批发市场都有哪里 浏览:973
什么叫aig技术 浏览:505
上海阿里法拍房信息在哪里查询 浏览:899
新产品商标怎么处理 浏览:66
房产交易二套商贷额度是多少 浏览:571