• C#发送邮件附件


    public string SendOutEmail(string fileName, string name, string eamil, string title, string content, string category, int OfferId)
    {

    //获取时间

    var thisDate = DateTime.Now;
    var year = thisDate.ToString("yyyy");
    var month = thisDate.ToString("MM");
    var day = thisDate.ToString("dd");
    // 文件名
    var thisName = Guid.NewGuid();
    //组合文件路径
    var xunilujing = "/UploadFile/" + year + "/" + month + "/" + day;
    var path = HttpContext.Current.Server.MapPath("~" + xunilujing);
    // 获取订单信息
    var obj = new OfferInfoController().GetInfo("", out _, OfferId); //获取 obj下的name
    // 获取最新的订单数据
    var monitoring = new OfferMonitoringEntryController().GetListByOfferIdMaxMonitoring("", out _, OfferId);
    var orgList = monitoring.Where(it => it.EntryCode == "enyUpload").ToList();
    var org = orgList[0].EntryValue;
    var OldPath = org.ToString().Substring(org.LastIndexOf("/") + 1);
    var NewPath = path + "\" + OldPath;
    var str = NewPath.Substring(0, NewPath.LastIndexOf("\"));
    var OssUrl = OssController.UploadToOss("", out fileName, NewPath, OldPath);
    SmtpClient client = new SmtpClient(SiteSetting.MailServer);
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential(SiteSetting.MailAccount, SiteSetting.MailPassword);
    UserInfoController userContro = new UserInfoController();

    var cmObj = userContro.GetInfo("", out _, obj.CMUserId, "", isAddLog: false);

    var bsObj = userContro.GetInfo("", out _, obj.BSUserId, "", isAddLog: false);
    MailAddress from = new MailAddress(SiteSetting.MailAccount, name, Encoding.UTF8);//初始化发件人
    var deadlineObj = monitoring.Find(m => m.EntryCode == "enyCMexpectationDate");
    string deadline = deadlineObj != null ? deadlineObj.EntryValue : "";
    MailAddress to = new MailAddress(bsObj.UserEmail, "", Encoding.UTF8);//初始化收件人
    var enyCompany = monitoring.Where(it => it.EntryCode == "enyCompany").ToList();
    var enyCompanys = enyCompany[0].EntryValue;
    var enyProductName = monitoring.Where(it => it.EntryCode == "enyProductName").ToList();
    var enyProductNames = enyProductName[0].EntryValue;
    var enyVNB_1 = monitoring.Where(it => it.EntryCode == "enyVNB_1").ToList();
    var enyVNB_1s = enyVNB_1[0].EntryValue;
    string EmailHtmls = "";
    string mailBody = "<table>";
    mailBody += "<tr>";
    mailBody += "<td>We have provided the non-binding terms sheet to Client.";
    mailBody += "</td>";
    mailBody += "<tr>";
    mailBody += "<td>Company:";
    mailBody += enyCompanys;
    mailBody += "</td>";
    mailBody += "</tr>";
    mailBody += "<tr>";
    mailBody += "<td>ProductName:";
    mailBody += obj.ProductName;
    mailBody += "</td>";
    mailBody += "</tr>";
    mailBody += "<br/>";
    mailBody += "<br/>";
    mailBody += "<br/>";
    mailBody += "<tr>";
    mailBody += "<td>Regards,";
    mailBody += cmObj.UserCode;
    mailBody += "</td>";
    mailBody += "</tr>";
    mailBody += "<br/>";
    mailBody += "</table>";
    EmailHtmls = mailBody;

    //设置邮件内容
    MailMessage message = new MailMessage(from, to);
    message.Body = EmailHtmls;
    message.BodyEncoding = Encoding.UTF8;
    message.Subject = title.Trim();
    message.SubjectEncoding = Encoding.UTF8;
    message.IsBodyHtml = true;

    if (!string.IsNullOrEmpty(NewPath))
    {
    //附件
    Attachment myAttachment = new System.Net.Mail.Attachment(NewPath, MediaTypeNames.Application.Octet);

    //MIME协议下的一个对象,用以设置附件的创建时间,修改时间以及读取时间
    ContentDisposition disposition = myAttachment.ContentDisposition;
    disposition.CreationDate = File.GetCreationTime(NewPath);
    disposition.ModificationDate = File.GetLastWriteTime(NewPath);
    disposition.ReadDate = File.GetLastAccessTime(NewPath);

    //用smtpclient对象里attachments属性,添加上面设置好的myattachment
    message.Attachments.Add(myAttachment);
    }

    try
    {
    client.Send(message);
    return "邮件发送成功!";
    }
    catch (Exception ex)
    {
    Log.Error(MethodBase.GetCurrentMethod(), ex);
    return "邮件发送失败!" + ex.Message;
    }
    }

  • 相关阅读:
    【NOIP2017】蚯蚓
    【CF407B】Long Path
    【NOIP2017】奶酪
    【NOIP2018】赛道修建(正解)
    【NOIP2018】旅行
    【SDOI2010】地精部落
    【NOIP2017】逛公园
    百度云网盘进行注销操作
    百度超级会员租借.我租给你。
    如何在dos中运行java中的jar包
  • 原文地址:https://www.cnblogs.com/MNCnblogs/p/14024991.html
Copyright © 2020-2023  润新知