我有以下代码,但它不起作用 - 使用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>");
答案 0 :(得分:0)
The Attachment class is used with the MailMessage class. All messages
包含一个Body,其中包含 消息的内容。此外 身体,你可能想发送 其他文件。这些是作为发送 附件和表示为 附件实例。添加一个 附加到邮件消息,添加它 到MailMessage.Attachments 集合。
Attachment content can be a String, Stream, or file name. You can
指定附件中的内容 通过使用任何附件 构造
由ContentType表示的The MIME Content-Type header information for the attachment is
属性。 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();
}