C# 发送邮件(WinForm、Asp.Net)

注意:如果是Asp.Net的话,需要在Web.config里配置<httpRuntime maxRequestLength="120240" executionTimeout="180" enable="true"/>,如果上传大文件(出现页面错误,就请把maxRequestLength设置大点)
 
 if (this.fuFile.PostedFile.ContentLength > 10240)
                {
                    Alert.ShowInTop("<br>操作失败【提示:附件大小不成超过10MB】!", "操作提示", MessageBoxIcon.Error);
                    return;
                }
                MailMessage myEmail = new MailMessage();
                myEmail.From = new MailAddress("support@fanxu.com");
                myEmail.To.Add(txtEmail2.Text.Trim());
                myEmail.Subject = txtSubject.Text.Trim();
                myEmail.IsBodyHtml = false;
                //附件   
                if (this.fuFile.PostedFile.ContentLength != 0)
                {
                    try
                    {
                        //获取所有文件(包括子文件夹中的文件)
                        string[] files = System.IO.Directory.GetFiles(Server.MapPath(@"/TempFile/"), "*.*", System.IO.SearchOption.AllDirectories);
                        foreach (string file in files)
                        {
                            //删除文件
                            System.IO.File.Delete(file);
                        }
                    }
                    catch (Exception)
                    {
                        
                    }
                    string upFileName = this.fuFile.PostedFile.FileName;
                    string[] strTemp = upFileName.Split('.');
                    string upFileExp = strTemp[strTemp.Length - 1].ToString();
                    ServerFileName = Server.MapPath(@"/TempFile/file." + upFileExp);
                    this.fuFile.PostedFile.SaveAs(ServerFileName);
                    myEmail.Attachments.Add(new Attachment(ServerFileName));

                }

                myEmail.Body = txtBody.Text.Trim();
                myEmail.BodyEncoding = Encoding.UTF8;
                myEmail.Priority = MailPriority.High;

                SmtpClient smtp = new SmtpClient("邮件名");
                smtp.Credentials = new NetworkCredential("用户名", "密码");
                smtp.Port = 25;
                smtp.EnableSsl = false;
                smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
              // for(
                smtp.Send(myEmail);

转载于:https://www.cnblogs.com/whz881027/articles/2105989.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值