我需要发送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);
}
答案 0 :(得分:0)
我认为您的问题是放置在错误的括号中,您似乎尝试使用Attachment
类的构造函数Attachment(Stream, String),但仅提供了流。仅接受一个参数的Attachment
类的构造函数期望错误消息说的是一个字符串
更正为:
email.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, pdffilename));