求助一段发送邮件的程序代码! - 技术问答

求助一段发送邮件的程序代码! - 技术问答,第1张

<?php //例子 if(send_mail('duanpanzi@163.com','发信标题11','发信内容11')=="") { echo "发送成功!<br>" } else{ echo "发送失败!<br>" } /*$url = " http://mail.163.com "$url2 = " http://mail.126.com "$lines_array = file($url) $lines_string = implode('', $lines_array) eregi("(.*)", $lines_string, $head) echo $head[0] echo $head[1] echo $head[2]*/ function send_mail($to, $subject = 'No subject', $body) { $loc_host = "skyaspnet"//发信计算机名,可随意 $smtp_acc = "duanpanzi@163.com"//Smtp认证的用户名,类似fish1240@fishcat.com.cn,或者fish1240 $smtp_pass="61D1223yan2009"//Smtp认证的密码,一般等同pop3密码 $smtp_host="smtp.163.com"//SMTP服务器地址,类似 smtp.tom.com $from="duanpanzi@163.com"//发信人Email地址,你的发信信箱地址 $headers = "Content-Type: text/plaincharset=\"gb2312\"\r\nContent-Transfer-Encoding: base64" $lb="\r\n"//linebreak $hdr = explode($lb,$headers)//解析后的hdr if($body) {$bdy = preg_replace("/^\./","..",explode($lb,$body))}//解析后的Body $smtp = array( //1、EHLO,期待返回220或者250 array("EHLO ".$loc_host.$lb,"220,250","HELO error: "), //2、发送Auth Login,期待返回334 array("AUTH LOGIN".$lb,"334","AUTH error:"), //3、发送经过Base64编码的用户名,期待返回334 array(base64_encode($smtp_acc).$lb,"334","AUTHENTIFICATION error : "), //4、发送经过Base64编码的密码,期待返回235 array(base64_encode($smtp_pass).$lb,"235","AUTHENTIFICATION error : ")) //5、发送Mail From,期待返回250 $smtp[] = array("MAIL FROM: <".$from.">".$lb,"250","MAIL FROM error: ") //6、发送Rcpt To。期待返回250 $smtp[] = array("RCPT TO: <".$to.">".$lb,"250","RCPT TO error: ") //7、发送DATA,期待返回354 $smtp[] = array("DATA".$lb,"354","DATA error: ") //8.0、发送From $smtp[] = array("From: ".$from.$lb,"","") //8.2、发送To $smtp[] = array("To: ".$to.$lb,"","") //8.1、发送标题 $smtp[] = array("Subject: ".$subject.$lb,"","") //8.3、发送其他Header内容 foreach($hdr as $h) {$smtp[] = array($h.$lb,"","")} //8.4、发送一个空行,结束Header发送 $smtp[] = array($lb,"","") //8.5、发送信件主体 if($bdy) {foreach($bdy as $b) {$smtp[] = array(base64_encode($b.$lb).$lb,"","")}} //9、发送“.”表示信件结束,期待返回250 $smtp[] = array(".".$lb,"250","DATA(end)error: ") //10、发送Quit,退出,期待返回221 $smtp[] = array("QUIT".$lb,"221","QUIT error: ")//打开smtp服务器端口 $fp = @fsockopen($smtp_host, 25) if (!$fp) echo "<b>Error:</b>Cannot conect to ".$smtp_host."<br>" while($result = @fgets($fp, 1024)){if(substr($result,3,1) == " ") { break}} $result_str="" //发送smtp数组中的命令/数据 foreach($smtp as $req){ //发送信息 @fputs($fp, $req[0]) //如果需要接收服务器返回信息,则 if($req[1]){ //接收信息 while($result = @fgets($fp, 1024)){ if(substr($result,3,1) == " ") { break} } if (!strstr($req[1],substr($result,0,3))){ $result_str.=$req[2].$result."<br>" } } } //关闭连接 @fclose($fp) return $result_str } ?>

protected void SendSuccess()

{

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage()

message.From = new MailAddress("test@gmail.com", "someone")//必须是提供smtp服务的邮件服务器

message.To.Add(new MailAddress("test@yahoo.com.cn"))

message.Subject = "测试邮件"

message.CC.Add(new MailAddress("test@126.com"))

message.Bcc.Add(new MailAddress("test@126.com"))

message.IsBodyHtml = true

message.BodyEncoding = System.Text.Encoding.UTF8

message.Body = "邮件发送测试"

message.Priority = System.Net.Mail.MailPriority.High

SmtpClient client = new SmtpClient("smtp.gmail.com", 587)// 587//Gmail使用的端口

client.Credentials = new System.Net.NetworkCredential("test@gmail.com", "password")//这里是申请的邮箱和密码

client.EnableSsl = true//必须经过ssl加密

try

{

client.Send(message)

Response.Write("邮件已经成功发送到" + message.To.ToString() + "<br>")

}

catch (Exception ee)

{

Response.Write(ee.Message + "<br>" /* + ee.InnerException.Message*/ )

}

}

收邮件

using LumiSoft.Net.POP3.Client

using LumiSoft.Net.Mail

……

public IList<Mail_Message>ReceiveMail()

{

IList<Mail_Message>mailList = new List<Mail_Message>()

using (POP3_Client client = new POP3_Client())

{

client.Connect("pop.gmail.com",995,true)

client.Authenticate("zw.seaman", "zw_seaman", false)

POP3_ClientMessageCollection coll = client.Messages

for (int i = 0i <coll.Counti++)

{

POP3_ClientMessage message = coll[i]

Mail_Message mm = Mail_Message.ParseFromByte(coll[i].MessageToByte())

mailList.Add(mm)

}

}

return mailList

}

protected void Page_Load(object sender, EventArgs e)

{

IList<Mail_Message>mailList = new ZMail.Mail().ReceiveMail()

foreach (Mail_Message mail in mailList)

{

StringBuilder sb = new StringBuilder()

sb.Append(mail.From.ToString()).Append(" 发送给 ")

sb.Append(mail.To.ToString()).Append("<br/>")

sb.Append(mail.Subject).Append("<br/>")

sb.Append(mail.BodyHtmlText).Append("<hr/>")

Response.Write(sb.ToString())

}

}

C语言发送邮件 完整代码:

#include <windows.h>

#include <stdio.h>

#include <WinSock.h>

#include <iostream>

using namespace std

#pragma  comment(lib, "ws2_32.lib")

struct Base64Date6

{

    unsigned int d4:6

    unsigned int d3:6

    unsigned int d2:6

    unsigned int d1:6

}

// 协议中加密部分使用的是base64方法

char ConvertToBase64  (char   c6)

void EncodeBase64     (char   *dbuf, char *buf128, int len)

void SendMail         (char   *email,char *body)

int  OpenSocket       (struct sockaddr *addr)

int main()

{

    char EmailTo[]         = "xushiladeng@qq.com"

    char EmailContents[]   = "From: \"lucy\"<xushiladeng@qq.com>\r\n"

        "To: \"dasiy\"<xushiladeng@qq.com>\r\n"

        "Subject: Hello\r\n\r\n"

        "Hello World, Hello Email!"

//        "DATA\r\n" //告诉服务器下面是邮件

        //下面是邮件头

        "TO: xushiladeng@qq.com\r\n"

        "FROM: xushiladeng@qq.com\r\n"

        "SUBJECT: SMTP协议测试\r\n"

        "Date:2015-04-20\r\n"

        "X-Mailer:fice's mailer\r\n"

        "MIMI-Version:1.0\r\n"

        "Content-Type:multipart/mixedboundary=\"#BOUNDARY#\"\r\n\r\n"//设置边界值,区分邮件内容的 两个回车换行

        "Content-Transfer-Encoding:7bit\r\n\r\n"

        "This is a multi-part message in MIME format\r\n\r\n"

        //发送邮件内容头部信息

        "--#BOUNDARY#\r\n"

        "Content-Type: text/plaincharset=gb2312\r\n"

        "Content-Transfer-Encoding:printable\r\n\r\n"

        //发送邮件的内容部分

        "SMTP协议测试:发送附件/n----by fice 2015.04.20\r\n"

        //发送附件头部信息

        "--#BOUNDARY#\r\n"

        "Content-Type:text/plainname=student.txt\r\n"

        "Content-Transfer-Encoding:base64\r\n"

        "Content-Disposition:attachmentfilename=\"test.txt\"\r\n\r\n"

    SendMail(EmailTo, EmailContents)

    return 0

}

char ConvertToBase64(char uc)

{

    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的实现

void EncodeBase64(char *dbuf, char *buf128, int len)

{

    struct Base64Date6 *ddd      = NULL

    int           i        = 0

    char          buf[256] = {0}

    char          *tmp     = NULL

    char          cc       = '\0'

    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             = (struct Base64Date6 *)tmp

        dbuf[(i-1)*4+0] = ConvertToBase64((unsigned int)ddd->d1)

        dbuf[(i-1)*4+1] = ConvertToBase64((unsigned int)ddd->d2)

        dbuf[(i-1)*4+2] = ConvertToBase64((unsigned int)ddd->d3)

        dbuf[(i-1)*4+3] = ConvertToBase64((unsigned int)ddd->d4)

    }

    if(len % 3 == 1)

    {

        tmp             = buf + (i-1)*3

        cc              = tmp[2]

        tmp[2]          = tmp[0]

        tmp[0]          = cc

        ddd             = (struct Base64Date6 *)tmp

        dbuf[(i-1)*4+0] = ConvertToBase64((unsigned int)ddd->d1)

        dbuf[(i-1)*4+1] = ConvertToBase64((unsigned int)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             = (struct Base64Date6 *)tmp

        dbuf[(i-1)*4+0] = ConvertToBase64((unsigned int)ddd->d1)

        dbuf[(i-1)*4+1] = ConvertToBase64((unsigned int)ddd->d2)

        dbuf[(i-1)*4+2] = ConvertToBase64((unsigned int)ddd->d3)

        dbuf[(i-1)*4+3] = '='

    }

    return

}

// 发送邮件

void SendMail(char *email, char *body)

{

    int     sockfd     = {0}

    char    buf[1500]  = {0}

    char    rbuf[1500] = {0}

    char    login[128] = {0}

    char    pass[128]  = {0}

    WSADATA WSAData

    struct sockaddr_in their_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("IP of smpt.126.com is : %d:%d:%d:%d\n", 

        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((struct sockaddr *)&their_addr)

    memset(rbuf, 0, 1500)

    while(recv(sockfd, rbuf, 1500, 0) == 0)

    {

        cout<<"reconnect..."<<endl

        Sleep(2)

        sockfd = OpenSocket((struct sockaddr *)&their_addr)

        memset(rbuf, 0, 1500)

    }

    cout<<rbuf<<endl

    // EHLO

    memset(buf, 0, 1500)

    sprintf_s(buf, 1500, "EHLO HYL-PC\r\n")

    send(sockfd, buf, strlen(buf), 0)

    memset(rbuf, 0, 1500)

    recv(sockfd, rbuf, 1500, 0)

    cout<<"EHLO REceive: "<<rbuf<<endl

    // AUTH LOGIN

    memset(buf, 0, 1500)

    sprintf_s(buf, 1500, "AUTH LOGIN\r\n")

    send(sockfd, buf, strlen(buf), 0)

    memset(rbuf, 0, 1500)

    recv(sockfd, rbuf, 1500, 0)

    cout<<"Auth Login Receive: "<<rbuf<<endl

    // USER

    memset(buf, 0, 1500)

    sprintf_s(buf, 1500, "xushiladeng@qq.com")//你的邮箱账号

    memset(login, 0, 128)

    EncodeBase64(login, buf, strlen(buf))

    sprintf_s(buf, 1500, "%s\r\n", login)

    send(sockfd, buf, strlen(buf), 0)

    cout<<"Base64 UserName: "<<buf<<endl

    memset(rbuf, 0, 1500)

    recv(sockfd, rbuf, 1500, 0)

    cout<<"User Login Receive: "<<rbuf<<endl

    // PASSWORD

    sprintf_s(buf, 1500, "********")//你的邮箱密码

    memset(pass, 0, 128)

    EncodeBase64(pass, buf, strlen(buf))

    sprintf_s(buf, 1500, "%s\r\n", pass)

    send(sockfd, buf, strlen(buf), 0)

    cout<<"Base64 Password: "<<buf<<endl

    memset(rbuf, 0, 1500)

    recv(sockfd, rbuf, 1500, 0)

    cout<<"Send Password Receive: "<<rbuf<<endl

    // MAIL FROM

    memset(buf, 0, 1500)

    sprintf_s(buf, 1500, "MAIL FROM: <xushiladeng@qq.com>\r\n")

    send(sockfd, buf, strlen(buf), 0)

    memset(rbuf, 0, 1500)

    recv(sockfd, rbuf, 1500, 0)

    cout<<"set Mail From Receive: "<<rbuf<<endl

    // RCPT TO 第一个收件人

    sprintf_s(buf, 1500, "RCPT TO:<%s>\r\n", email)

    send(sockfd, buf, strlen(buf), 0)

    memset(rbuf, 0, 1500)

    recv(sockfd, rbuf, 1500, 0)

    cout<<"Tell Sendto Receive: "<<rbuf<<endl

    // DATA 准备开始发送邮件内容

    sprintf_s(buf, 1500, "DATA\r\n")

    send(sockfd, buf, strlen(buf), 0)

    memset(rbuf, 0, 1500)

    recv(sockfd, rbuf, 1500, 0)

    cout<<"Send Mail Prepare Receive: "<<rbuf<<endl

    // 发送邮件内容,\r\n.\r\n内容结束标记

    sprintf_s(buf, 1500, "%s\r\n.\r\n", body)

    send(sockfd, buf, strlen(buf), 0)//发送邮件,不带附件的内容

    //用二进制方式读取附件文件内容并转为base64格式  

    FILE* pf = fopen("D:\\test.txt","rb")

    fseek(pf,0,SEEK_END)

    int filelen = 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<<"Send Mail Receive: "<<rbuf<<endl

    // QUIT

    sprintf_s(buf, 1500, "QUIT\r\n")

    send(sockfd, buf, strlen(buf), 0)

    memset(rbuf, 0, 1500)

    recv(sockfd, rbuf, 1500, 0)

    cout<<"Quit Receive: "<<rbuf<<endl

    //清理工作

    closesocket(sockfd)

    WSACleanup()

    return

}

// 打开TCP Socket连接

int OpenSocket(struct sockaddr *addr)

{

    int sockfd = 0

    sockfd=socket(PF_INET, SOCK_STREAM, 0)

    if(sockfd < 0)

    {

        cout<<"Open sockfd(TCP) error!"<<endl

        exit(-1)

    }

    if(connect(sockfd, addr, sizeof(struct sockaddr)) < 0)

    {

        cout<<"Connect sockfd(TCP) error!"<<endl

        exit(-1)

    }

    return sockfd

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8140493.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存