【问题标题】:cannot send fileupload.postedfile.inputstream as an attachment - ERROR: cannot convert system.io.stream to string无法将 fileupload.postedfile.inputstream 作为附件发送 - 错误:无法将 system.io.stream 转换为字符串
【发布时间】:2019-07-05 18:28:12
【问题描述】:

我需要发送一个 pdf 作为附件。我正在使用 html 模板作为邮件正文。我的问题是尝试发送附件时显示的错误如下。

email.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream), pdffilename);

FileUpload1.PostedFile.InputStream 是错误码。

我的完整代码如下所示:

private void send_job_notification(string name, string app_email, string job_id, string job_title, string experience,string pdffilename)
    {
        string to = "aaa@bbb.com";
        SmtpClient smtp_server = new SmtpClient();
        MailMessage email = new MailMessage("aaa@bbb.com", to);
        smtp_server.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp_server.UseDefaultCredentials = false;
        smtp_server.Credentials = new System.Net.NetworkCredential("aaa@bbb.com", "PASSWORD");
        smtp_server.Port = 25;
        smtp_server.EnableSsl = false;
        smtp_server.Host = "zzz.bbb.com";
        email.From = new MailAddress("aaa@bbb.com");
        email.To.Add(to);
        email.Subject = "New Job request submitted for company";
        email.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream), pdffilename);
        email.IsBodyHtml = true;
        string FilePath = Server.MapPath("~/emailnewtemplate.html");
        StreamReader str = new StreamReader(FilePath);
        string MailText = str.ReadToEnd();
        str.Close();
        string line1 = "<h4>Dear Management</h4>";
        string line2 = "<h4>A new job request has been receieved. The applicant details are as follows</h4>";
        string table_open = "<table class='table table-responsive-sm'>";
        string row_1 = "<tr><td>Name : </td><td> " + name + " </td></tr>";
        string row_2 = "<tr><td>Email : </td><td> " + app_email + " </td></tr>";
        string row_3 = "<tr><td>Job ID : </td><td> " + job_id + " </td></tr>";
        string row_4 = "<tr><td>Job Title : </td><td> " + job_title + " </td></tr>";
        string row_5 = "<tr><td>Experience : </td><td> " + experience + " </td></tr>";
        string table_close = "</table>";
        string line3 = table_open + row_1 + row_2 + row_3 + row_4 + row_5 + table_close;
        string htmlbody = line1 + line2 + line3;
        MailText = MailText.Replace("[#####HERE#####]", htmlbody);
        email.Body = MailText;
        smtp_server.Send(email);
   }

【问题讨论】:

标签: c# asp.net email


【解决方案1】:

我认为错误放置的括号是您的问题,看起来您尝试使用 Attachment 类的构造函数 Attachment(Stream, String) 但您只提供了流。只接受一个参数的Attachment 类的构造函数需要一个字符串,这就是错误消息所说的内容

更正为:

email.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, pdffilename));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 2017-08-17
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    相关资源
    最近更新 更多