I have the following code but it does not work - I get errors when using a message.To
then I changed it to message.To.Add
but without any success.
I don't know ASP.net, I just want this thing to work. Any help is appreciated.
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('M开发者_开发问答essage sent successfully.');</script>");
The Attachment class is used with the MailMessage class. All messages
include a Body, which contains the content of the message. In addition to the body, you might want to send additional files. These are sent as attachments and are represented as Attachment instances. To add an attachment to a mail message, add it to the MailMessage.Attachments collection.
Attachment content can be a String, Stream, or file name. You can
specify the content in an attachment by using any of the Attachment constructors.
The MIME Content-Type header information for the attachment is
represented by the ContentType property. The Content-Type header specifies the media type and subtype and any associated parameters. Use ContentType to get the instance associated with an attachment.
The MIME Content-Disposition header is represented by the
ContentDisposition property. The Content-Disposition header specifies the presentation and file time stamps for an attachment. A Content-Disposition header is sent only if the attachment is a file. Use the ContentDisposition property to get the instance associated with an attachment.
The MIME Content-Transfer-Encoding header is represented by the
TransferEncoding property.
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();
}
精彩评论