如何解决带有文件附件的ASP电子邮件提交
| 我有以下代码,但无法正常工作-使用message.To
时出现错误,然后将其更改为message.To.Add
,但未成功。
我不知道ASP.net,我只是想让这个东西工作。任何帮助表示赞赏。
using System.Net.Mail;
protected void btnsubmit_Click(object sender,EventArgs e)
{
string body = \"\";
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
body = \"<table border=\'0\' align=\'center\' cellpadding=\'2\' style=\'border-collapse: collapse\' bordercolor=\'\'#111111\' width=\'100%\' id=\'AutoNumber1\'>\";
body = body + \"<tr><td width=\'100%\' align=\'center\' colspan=\'6\'><b>Photo Submission Form</b></td></tr>\";
body = body + \"<tr><td width=\'100%\' colspan=\'6\'> </td></tr>\";
body = body + \"<tr><td width=\'50%\' colspan=\'2\'>Name</td><td width=\'50%\' colspan=\'4\'><b>\" + name.Text + \"</b></td></tr>\";
body = body + \"<tr><td width=\'50%\' colspan=\'2\'>E-Mail</td><td width=\'50%\' colspan=\'4\'><b>\" + email.Text + \"</b></td></tr>\";
body = body + \"<tr><td width=\'50%\' colspan=\'2\'>Caption</td><td width=\'50%\' colspan=\'4\'><b>\" + caption.Text + \"</b></td></tr>\";
body = body + \"<tr><td width=\'50%\' colspan=\'2\'>Phone</td><td width=\'50%\' colspan=\'4\'><b>\" + phone.Text + \"</b></td></tr>\";
MailMessage message = new MailMessage();
Attachment myAttachment = new Attachment(FileUpload1.FileContent,fileName);
message.To.Add(new MailAddress(\"contact@xxxx.com\"));
message.From = New MailAddress(email.Text);
message.Subject = \"Photo Submission Form\";
message.BodyFormat = MailFormat.Html;
message.Body = body;
message.Attachments.Add(myAttachment);
SmtpMail.SmtpServer.Insert(0,\"\");
SmtpMail.Send(message);
RegisterStartupScript(\"startupScript\",\"<script language=JavaScript>alert(\'Message sent successfully.\');</script>\");
解决方法
The Attachment class is used with the MailMessage class. All messages
包括一个身体,其中包含
消息的内容。此外
身体,您可能想发送
其他文件。这些被发送为
附件,并表示为
附件实例。要添加
邮件附件,将其添加
到MailMessage.Attachments
采集。
Attachment content can be a String,Stream,or file name. You can
在附件中指定内容
通过使用任何附件
构造函数。
The MIME Content-Type header information for the attachment is
由ContentType表示
属性。 Content-Type标头
指定媒体类型和子类型
以及任何相关参数。采用
ContentType获取实例
与附件相关联。
The MIME Content-Disposition header is represented by the
ContentDisposition属性。的
Content-Disposition标头指定
演示文稿和文件时间戳记
用于附件。一种
Content-Disposition标头已发送
仅当附件是文件时。采用
ContentDisposition属性以获取
与关联的实例
附件。
The MIME Content-Transfer-Encoding header is represented by the
TransferEncoding属性。
public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = \"data.xls\";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
\"jane@contoso.com\",\"ben@contoso.com\",\"Quarterly data report.\",\"See the attached spreadsheet.\");
// 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);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try {
client.Send(message);
}
catch (Exception ex) {
//Handle...
}
data.Dispose();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。