加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

带有文件附件的ASP电子邮件上交

发布时间:2023-02-25 10:47:09 所属栏目:Asp教程 来源:
导读:如何解决带有文件附件的ASP电子邮件提交?
| 我有以下代码,但无法正常工作-使用
message.To
时出现错误,然后将其更改为
message.To.Add
,但未成功。 我不知道ASP.net,我只是想让这个东西工作。任何帮助表示
如何解决带有文件附件的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\'>&nbsp;</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(
               \",\"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();
        }

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章