从文件上传控件发送包括附件的邮件 [英] sending mail including attachment from file upload control

查看:66
本文介绍了从文件上传控件发送包括附件的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在抓住文件上传控件之后

hi
可以请任何人帮我解决如何向某人发送邮件包括该附件

解决方案

请关注此链接。它会对你有所帮助。



http://www.aspsnippets.com/articles/Attach-files-to-email-without-storing-on-disk-using-ASP。 Net-FileUpload-Control.aspx [ ^ ]



或者看看这个,

这里'我将解释如何将文件附加到电子邮件而不将其保存在磁盘上

您可以参考我的文章,了解如何以$发送电子邮件b
$ b使用C#中的System.Net类发送SMTP电子邮件,并使用VB.Net中的System.Net类发送SMTP电子邮件

使用FileUpload Control上传文件时收到文件在httppostedfile

属性中。

将HttpPostedFile附加到电子邮件中。您需要将其转换为Stream。

下面的代码将发布的文件附加到电子邮件中。请注意,txtAttachment是FileUpload控件

< asp:FileUpload ID =txtAttachmentrunat =server/>

C#

if(txtAttachment.PostedFile!= null)

{

try

{

string strFileName =

System.IO.Path.GetFileName(txtAttachment.PostedFile.FileName);

附件attachFile =

new Attachment(txtAttachment.PostedFile.InputStream,strFileName);

mm.Attachments.Add(attachFile);

}

catch

{



}

}


两种情况



1.您在Host中运行您的网站。



2.您在另一台机器上运行您的网站( Client )。





1.



使用不同的浏览器你会遇到问题



(假设chrome只显示文件名 路径。)





2.



例如:在客户端机器文件路径(C:\\\ atdf.txt)

服务器你可能不包含 C:\\\ atdf.txt 文件。





解决方案:

更好的事情是将文件上传到服务器。

并从该路径发送。

删除文件后发送电子邮件。



(建议使用不同的程序删除)。


将文件复制到服务器。

附加邮件。

发送邮件。

发送后删除文件。

使用名称空间

 使用 System.Net.Mime; 
使用 System.Text;
使用 System.Net.Mail;
使用系统;





和方法



 private Boolean SendMailUsingGmail(String mailTo,String mailText,String attachmentFileWithPath)
{
Boolean sendMailResult;
try
{
SmtpClient smtpServer = new SmtpClient();
smtpServer.Credentials = new System.Net.NetworkCredential(mailFromId,mailFromPassword);
smtpServer.Port = 25;
smtpServer.Host =smtp.gmail.com;
MailMessage sampleMail = new MailMessage();
sampleMail.From = new MailAddress(mailFromId);
sampleMail.Subject =Mail;
string file = attachmentFileWithPath; //文件路径
附件数据=新附件(文件,MediaTypeNames.Application.Zip);
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);
//将文件附件添加到此电子邮件中。
sampleMail.Attachments.Add(data);
sampleMail.To.Add(mailTo);
sampleMail.Body = mailText;
smtpServer.Send(sampleMail);
sendMailResult = true;
返回sendMailResult;
}
catch(exception ex)
{
string exceptionString = ex.Message;
sendMailResult = false;
返回sendMailResult;
}
最后
{
}

}


hi after clcikcing the file upload control can anybody please help me in how to send a mail to someperson incluidng that attachment

解决方案

Please follow this link. It will help you lot.

http://www.aspsnippets.com/articles/Attach-files-to-email-without-storing-on-disk-using-ASP.Net-FileUpload-Control.aspx[^]

Or take a look at this ,

 Here’ I’ll explain how to attach files to email without saving them on the disk

You can refer my articles on how to send emails in

Send SMTP Emails using System.Net Class in C#   and Send SMTP Emails using System.Net Class in VB.Net

When the File is uploaded using FileUpload Control the file is received in the httppostedfile

property.

To attach the HttpPostedFile  to the email. You will need to convert it to Stream.

The code below attaches a posted file to email. Note that txtAttachment is  a FileUpload control

<asp:FileUpload ID="txtAttachment" runat="server" />

C#

if (txtAttachment.PostedFile != null)

{

    try

    {

        string strFileName =

        System.IO.Path.GetFileName(txtAttachment.PostedFile.FileName);

        Attachment attachFile =

        new Attachment(txtAttachment.PostedFile.InputStream, strFileName);

        mm.Attachments.Add(attachFile);

    }

    catch

    {

 

    }

}


Two Situations

1. You are running your website in Host.

2. You are running your website in Another machine(Client).


1.

Using different browsers you will get problem

(suppose chrome displays only filename insted of path.)


2.

eg: In your client machine filepath (C:\asdf.txt)
server you may not contain C:\asdf.txt file.


Solution:
Better thing upload file into Server.
and send from that path.
delete file after sent email.

(suggesition use different program for delete).


Copy file to server.
Attach to mail.
Send mail.
Delete file after sending.
use namespaces

using System.Net.Mime;
using System.Text;
using System.Net.Mail;
using System;



and method

private Boolean SendMailUsingGmail(String mailTo, String mailText, String attachmentFileWithPath)
 {
     Boolean sendMailResult;
     try
     {
         SmtpClient smtpServer = new SmtpClient();
         smtpServer.Credentials = new System.Net.NetworkCredential(mailFromId, mailFromPassword);
         smtpServer.Port = 25;
         smtpServer.Host = "smtp.gmail.com";
         MailMessage sampleMail = new MailMessage();
         sampleMail.From = new MailAddress(mailFromId);
         sampleMail.Subject = "Mail";
         string file = attachmentFileWithPath; //file path
         Attachment data = new Attachment(file, MediaTypeNames.Application.Zip);
         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.
         sampleMail.Attachments.Add(data);
         sampleMail.To.Add(mailTo);
         sampleMail.Body = mailText;
         smtpServer.Send(sampleMail);
         sendMailResult = true;
         return sendMailResult;
     }
     catch (Exception ex)
     {
         string exceptionString = ex.Message;
         sendMailResult = false;
         return sendMailResult;
     }
     finally
     {
     }

 }


这篇关于从文件上传控件发送包括附件的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆