Dateianhang Form c #
-
27-09-2019 - |
Frage
im ein Web-Formular erstellen, die ein Benutzer eine Datei in doc uplaod, docxorpdf Format anschließend auf das Formular zusammen mit der angehängten Datei an eine E-Mail-Adresse geschrieben wird, ihave erfolgreich über die Entsendung von Formular an die E-Mail-Adresse umgesetzt, aber nicht wissen, wie die Datei mit ihm befestigen ... plz help
public void ProcessRequest(HttpContext context)
{
string template = context.Request["template"];
string responseHtml = BuiltTemplateHtml(context.Request, template, "response", false);
string reuestEmailHtml = BuiltTemplateHtml(context.Request, template, "request_email", false);
string contactEmail = GetTagsInnerText(reuestEmailHtml, "to", 0);
string contactName = GetTagsInnerText(reuestEmailHtml, "toname", 0);
string responEmailHtml = BuiltTemplateHtml(context.Request, template, "response_email", true, "contactName", contactName, "contactEmail", contactEmail);
sendEmail(reuestEmailHtml);
sendEmail(responEmailHtml);
context.Response.ContentType = "text/html";
context.Response.Write(responseHtml);
SaveAttachments(context, reuestEmailHtml);
}
private void SaveAttachments(HttpContext context, string settingFile)
{
if (context.Request.Files.Count > 0)
{
string fileNameformat = GetTagsInnerText(settingFile, "fileNameformat", 0);
string[] savefiles = GetTagsInnerText(settingFile, "savefiles", 0).Split('|', ',');
string[] allowextensions = GetTagsInnerText(settingFile, "allowextensions", 0).Split('|', ',');
string path = cleanPath(fileNameformat);
MailMessage mail = new MailMessage();
// attachment code here
}
}
Lösung
Überprüfen Sie diese aus,
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
und prüfen Sie diesen Link auf Msdn für weitere Informationen, http://msdn.microsoft.com/ en-us / library / system.net.mail.mailmessage.attachments.aspx
Andere Tipps
Die Mailmessage Klasse hat eine < a href = "http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx" rel = "nofollow"> Anhänge Eigenschaft, die hinzuzufügen verwendet werden könnte, Anlagen zu der Nachricht.